Skip to content

Instantly share code, notes, and snippets.

@lifthrasiir
Created September 25, 2010 09:36
Show Gist options
  • Save lifthrasiir/596667 to your computer and use it in GitHub Desktop.
Save lifthrasiir/596667 to your computer and use it in GitHub Desktop.
/* The world's smallest Brainfuck interpreter in C, by Kang Seonghoon
* http://j.mearie.org/post/1181041789/brainfuck-interpreter-in-2-lines-of-c */
s[99],*r=s,*d,c;main(a,b){char*v=1[d=b];for(;c=*v++%93;)for(b=c&2,b=c%7?a&&(c&17
?c&1?(*r+=b-1):(r+=b-1):syscall(4-!b,b,r,1),0):v;b&&c|a**r;v=d)main(!c,&a);d=v;}
@RYSF13
Copy link

RYSF13 commented Jul 27, 2025

By the way, this can be collected in TCCC. If you agree, please check the collaborate invitation.

@multitrack-collector
Copy link

Awesome!!! 🥇 🥇 🥇 But can you explain it? pls!!!

I did some C tutorials so I'll take a shot.
The code is heavily obfuscated and most variables are untyped. Hell even main is untyped. SO I asked a GPT and it spat out this

This is an example of obfuscated C code — it’s intentionally written in a cryptic, unreadable way using obscure pointer arithmetic, operator overloading, and terse expressions. Let's break it down to figure out what it’s doing.


Step-by-step Breakdown

Declaration:

s[99], *r = s, *d, c;
  • s[99] declares an array of 99 integers.
  • *r = s sets r as a pointer to s (i.e., beginning of array).
  • *d, c declares d as a pointer, c as an int.

So s is the data buffer, r is the current pointer into it, d will be a pointer, c is a char/int used in the main loop.


Main function signature

```c
main(a, b)
```

Old-style K&R function declaration. a and b are parameters.

  • a is treated like an int
  • b is treated like char ** in some places

Initial pointer setup

```c
char *v = 1[d = b];
```

This means:

  • d = b assigns b to d
  • 1[d] is the same as d[1] (pointer syntax)
  • So v = d[1]

Assuming b is char **argv, then v = argv[1]

So v now points to the first command-line argument string


Outer for loop

```c
for (; c = *v++ % 93;)
```

  • This reads each character from v (i.e. the command-line argument)
  • Takes ASCII value mod 93
  • Stores result in c
  • Continues loop while c is non-zero

Inner for loop

```c
for (b = c & 2, b = c % 7 ? a && (c & 17 ?
c & 1 ? ( * r += b - 1) : (r += b - 1) : syscall(4 - !b, b, r, 1), 0) : v;
b && c | a ** r;
v = d)
main(!c, &a);
```

This is where things get dense. Here's a breakdown:

  • b = c & 2 extracts bit 1 of c

  • b = c % 7 ? ... : v:

    • If c % 7 is true:
      • If a and (c & 17):
        • If c & 1: *r += b - 1
        • Else: r += b - 1
      • Else: syscall(4 - !b, b, r, 1)
    • Then comma operator evaluates to 0 (meaning no change to b)
  • The loop condition:

    • b && (c | a ** r) — continues looping if b is nonzero and expression doesn't short-circuit
  • In loop body:
    ```c
    main(!c, &a);
    ```
    Recursively calls main

  • After loop: d = v;


Summary of What It Does

This is a small ASCII-art or brainf*ck-style interpreter, often used in programming contests or obfuscation competitions like the IOCCC.

It:

  • Reads characters from command-line input
  • Interprets them as a kind of bytecode
  • Manipulates a data tape (s[] via pointer r)
  • Uses recursion, pointer arithmetic, and the syscall() to possibly output characters

This kind of code is often used to execute a tiny self-contained language or generate output from a compressed string.


Likely Behavior When Run

If you compile and run this with:

```bash
./a.out 'encoded_string'
```

It would process each character, updating memory and maybe writing output to stdout using syscall(4, ...) — which is a direct write system call on Unix/Linux.


TL;DR

This code:

  • Takes a command-line string
  • Interprets each character with custom logic
  • Modifies a buffer
  • Possibly outputs characters via syscall
  • Uses heavy recursion and obfuscation tricks

It’s likely an interpreter or decoder, possibly producing ASCII art or executing some esoteric code. Would you like me to deobfuscate and rewrite it clearly?

@multitrack-collector
Copy link

multitrack-collector commented Jul 31, 2025

A lot of pointer arithmatic and untyped variables. Maybe gcc -m32 means 32-bit ints? I guess I have to phone ask the GPT to answer this one.

This is an example of obfuscated C code — it’s intentionally written in a cryptic, unreadable way using obscure pointer arithmetic, operator overloading, and terse expressions. Let's break it down to figure out what it’s doing.


Step-by-step Breakdown

Declaration:

s[99], *r = s, *d, c;
  • s[99] declares an array of 99 integers.
  • *r = s sets r as a pointer to s (i.e., beginning of array).
  • *d, c declares d as a pointer, c as an int.

So s is the data buffer, r is the current pointer into it, d will be a pointer, c is a char/int used in the main loop.


Main function signature

main(a, b)

Old-style K&R function declaration. a and b are parameters.

  • a is treated like an int
  • b is treated like char ** in some places

Initial pointer setup

char *v = 1[d = b];

This means:

  • d = b assigns b to d
  • 1[d] is the same as d[1] (pointer syntax)
  • So v = d[1]

Assuming b is char **argv, then v = argv[1]

So v now points to the first command-line argument string


Outer for loop

for (; c = *v++ % 93;)
  • This reads each character from v (i.e. the command-line argument)
  • Takes ASCII value mod 93
  • Stores result in c
  • Continues loop while c is non-zero

Inner for loop

for (b = c & 2, b = c % 7 ? a && (c & 17 ?
    c & 1 ? ( * r += b - 1) : (r += b - 1) : syscall(4 - !b, b, r, 1), 0) : v;
     b && c | a ** r;
     v = d)
    main(!c, &a);

This is where things get dense. Here's a breakdown:

  • b = c & 2 extracts bit 1 of c

  • b = c % 7 ? ... : v:

    • If c % 7 is true:
      • If a and (c & 17):
        • If c & 1: *r += b - 1
        • Else: r += b - 1
      • Else: syscall(4 - !b, b, r, 1)
    • Then comma operator evaluates to 0 (meaning no change to b)
  • The loop condition:

    • b && (c | a ** r) — continues looping if b is nonzero and expression doesn't short-circuit
  • In loop body:

    main(!c, &a);

    Recursively calls main

  • After loop: d = v;


Summary of What It Does

This is a small ASCII-art or brainf*ck-style interpreter, often used in programming contests or obfuscation competitions like the IOCCC.

It:

  • Reads characters from command-line input
  • Interprets them as a kind of bytecode
  • Manipulates a data tape (s[] via pointer r)
  • Uses recursion, pointer arithmetic, and the syscall() to possibly output characters

This kind of code is often used to execute a tiny self-contained language or generate output from a compressed string.


Likely Behavior When Run

If you compile and run this with:

./a.out 'encoded_string'

It would process each character, updating memory and maybe writing output to stdout using syscall(4, ...) — which is a direct write system call on Unix/Linux.


TL;DR

This code:

  • Takes a command-line string
  • Interprets each character with custom logic
  • Modifies a buffer
  • Possibly outputs characters via syscall
  • Uses heavy recursion and obfuscation tricks

It’s likely an interpreter or decoder, possibly producing ASCII art or executing some esoteric code. Would you like me to deobfuscate and rewrite it clearly?

Edit: had to get GPT to reveal the original markdown ina code block so it had to escape the ``` with \```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment