/bf.c
-
Star
(137)
You must be signed in to star a gist -
Fork
(8)
You must be signed in to fork a gist
-
-
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;} |
Og code using an online C beautifier: https://codebeautify.org/c-formatter-beautifier/y255f5fa5
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;
}
Awesome!!! 🥇 🥇 🥇
But can you explain it? pls!!!
By the way, this can be collected in TCCC. If you agree, please check the collaborate invitation.
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
setsr
as a pointer tos
(i.e., beginning of array).*d, c
declaresd
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
andb
are parameters.
a
is treated like anint
b
is treated likechar **
in some places
Initial pointer setup
```c
char *v = 1[d = b];
```This means:
d = b
assignsb
tod
1[d]
is the same asd[1]
(pointer syntax)- So
v = d[1]
Assuming
b
ischar **argv
, thenv = 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 ofc
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 ifb
is nonzero and expression doesn't short-circuitIn loop body:
```c
main(!c, &a);
```
Recursively callsmain
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 pointerr
)- Uses recursion, pointer arithmetic, and the
syscall()
to possibly output charactersThis 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
usingsyscall(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?
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
setsr
as a pointer tos
(i.e., beginning of array).*d, c
declaresd
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
andb
are parameters.
a
is treated like anint
b
is treated likechar **
in some places
Initial pointer setup
char *v = 1[d = b];This means:
d = b
assignsb
tod
1[d]
is the same asd[1]
(pointer syntax)- So
v = d[1]
Assuming
b
ischar **argv
, thenv = 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 ofc
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 ifb
is nonzero and expression doesn't short-circuitIn 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 pointerr
)- Uses recursion, pointer arithmetic, and the
syscall()
to possibly output charactersThis 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
usingsyscall(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 \```
Cheers!