#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 */

/* Variables and functions */
int /*<<< orphan*/ MSG_WAITALL;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ comm_handshake(int);
int /*<<< orphan*/ dotunneling(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *, int);
int recv(int, int *, int, int /*<<< orphan*/);
int send(int, int *, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void docomm(int sock) {
  int cmd;
  int numbytes;
  comm_handshake(sock);
  cmd = 0x00000001;
  if ((numbytes = send(sock, &cmd, sizeof cmd, 0)) == -1) {
    return;
  }
  if ((numbytes = recv(sock, &cmd, sizeof cmd, 0)) == -1) {
    return;
  }
  switch (cmd) {
  case 0x00000003:
    fprintf(stderr, "Help request acked\n");
    break;
  default:
    fprintf(stderr, "What?!? %x\n", cmd);
    close(sock);
    return;
    break;
  }
  for (;;) {
    if ((numbytes = recv(sock, &cmd, sizeof cmd, MSG_WAITALL)) == -1) {
      fprintf(stderr, "Error? %s\n", strerror(errno));
      continue;
    }
    switch (cmd) {
    case 0x00000002: {
      int outport;
      if ((numbytes = recv(sock, &outport, sizeof outport, MSG_WAITALL)) ==
          -1) {
        fprintf(stderr, "Error? %s\n", strerror(errno));
        continue;
      }
      if (numbytes == 0) {
        fprintf(stderr, "Connection closed...\n");
        return;
      }
      printf("Got a request to tunnel local(%d) to remote\n", outport);
      dotunneling(sock, outport);
    }
    }
  }
  close(sock);
}


x86 assembly (clang, -O0):
<docomm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1080 <comm_handshake@plt>
movl   $0x1,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11be <docomm+0x3e>
jmp    1343 <docomm+0x1c3>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <docomm+0x62>
jmp    1343 <docomm+0x1c3>
mov    -0x8(%rbp),%eax
sub    $0x3,%eax
jne    120f <docomm+0x8f>
jmp    11f3 <docomm+0x73>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1236 <docomm+0xb6>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
jmp    1343 <docomm+0x1c3>
jmp    123b <docomm+0xbb>
mov    -0x4(%rbp),%edi
mov    0x2d83(%rip),%rax
mov    (%rax),%ecx
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    1294 <docomm+0x114>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xd98(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    123b <docomm+0xbb>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
jne    133e <docomm+0x1be>
jmp    12a5 <docomm+0x125>
mov    -0x4(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%ecx
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    12fe <docomm+0x17e>
mov    0x2d0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2cfa(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xd2e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    123b <docomm+0xbb>
cmpl   $0x0,-0xc(%rbp)
jne    1324 <docomm+0x1a4>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1343 <docomm+0x1c3>
mov    -0x10(%rbp),%esi
lea    0xd13(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1090 <dotunneling@plt>
jmp    123b <docomm+0xbb>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<docomm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
call   1080 <comm_handshake@plt>
movl   $0x1,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0xffffffff,%eax
je     12b8 <docomm+0x138>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     12b8 <docomm+0x138>
mov    (%rsp),%edx
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
cmp    $0x3,%edx
jne    128a <docomm+0x10a>
lea    0xe17(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2dd1(%rip),%r12
mov    %rsp,%r14
mov    0x2dd7(%rip),%r13
lea    0xe18(%rip),%r15
jmp    1231 <docomm+0xb1>
nopw   0x0(%rax,%rax,1)
mov    0x2dc9(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%edi
call   10a0 <strerror@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%r12),%ecx
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1210 <docomm+0x90>
cmpl   $0x2,(%rsp)
jne    1231 <docomm+0xb1>
mov    (%r12),%ecx
mov    %ebx,%edi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1210 <docomm+0x90>
test   %eax,%eax
je     12a1 <docomm+0x121>
mov    0x4(%rsp),%esi
lea    0xdc9(%rip),%rdi
call   1050 <printf@plt>
mov    0x4(%rsp),%esi
mov    %ebx,%edi
call   1090 <dotunneling@plt>
jmp    1231 <docomm+0xb1>
lea    0xd83(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12b8 <docomm+0x138>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"Help request acked\0A\00"
@.str.1 = [12 x i8] c"What?!? %x\0A\00"
@MSG_WAITALL = global i32 0
@.str.2 = [11 x i8] c"Error? %s\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"Connection closed...\0A\00"
@.str.4 = [45 x i8] c"Got a request to tunnel local(%d) to remote\0A\00"
define void @docomm(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @comm_handshake(i32 %6)
store i32 1, i32* %3
%8 = load i32, i32* %2
%9 = call i32 @send(i32 %8, i32* %3, i32 4, i32 0)
store i32 %9, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %1
br label %64
12: ; preds = %1
%13 = load i32, i32* %2
%14 = call i32 @recv(i32 %13, i32* %3, i32 4, i32 0)
store i32 %14, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %12
br label %64
17: ; preds = %12
%18 = load i32, i32* %3
switch i32 %18, label %22 [
i32 3, label %19
]
19: ; preds = %17
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i32, i32* %3
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = call i32 @close(i32 %26)
br label %64
28: ; preds = %19
br label %29
29: ; preds = %63, %46, %34, %28
%30 = load i32, i32* %2
%31 = load i32, i32* @MSG_WAITALL
%32 = call i32 @recv(i32 %30, i32* %3, i32 4, i32 %31)
store i32 %32, i32* %4
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %37)
br label %29
39: ; preds = %29
%40 = load i32, i32* %3
switch i32 %40, label %63 [
i32 2, label %41
]
41: ; preds = %39
%42 = load i32, i32* %2
%43 = load i32, i32* @MSG_WAITALL
%44 = call i32 @recv(i32 %42, i32* %5, i32 4, i32 %43)
store i32 %44, i32* %4
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %49)
br label %29
51: ; preds = %41
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %64
57: ; preds = %51
%58 = load i32, i32* %5
%59 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = load i32, i32* %2
%61 = load i32, i32* %5
%62 = call i32 @dotunneling(i32 %60, i32 %61)
br label %63
63: ; preds = %57, %39
br label %29
64: ; preds = %54, %22, %16, %11
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"Help request acked\0A\00"
@.str.1 = [12 x i8] c"What?!? %x\0A\00"
@MSG_WAITALL = global i32 0
@.str.2 = [11 x i8] c"Error? %s\0A\00"
@errno = global i32 0
@.str.3 = [22 x i8] c"Connection closed...\0A\00"
@.str.4 = [45 x i8] c"Got a request to tunnel local(%d) to remote\0A\00"
define void @docomm(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @comm_handshake(i32 %0)
store i32 1, i32* %2
%6 = call i32 @send(i32 %0, i32* %2, i32 4, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %50, label %8
8: ; preds = %1
%9 = call i32 @recv(i32 %0, i32* %2, i32 4, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %50, label %11
11: ; preds = %8
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 3
%14 = load i32, i32* @stderr
br i1 %13, label %15, label %18
15: ; preds = %11
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = bitcast i32* %3 to i8*
br label %21
18: ; preds = %11
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%20 = call i32 @close(i32 %0)
br label %50
21: ; preds = %33, %15
%22 = load i32, i32* @MSG_WAITALL
%23 = call i32 @recv(i32 %0, i32* %2, i32 4, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %33
30: ; preds = %21
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, 2
br i1 %32, label %34, label %33
33: ; preds = %30, %40, %25, %45
br label %21
34: ; preds = %30
%35 = load i32, i32* @MSG_WAITALL
%36 = call i32 @recv(i32 %0, i32* %3, i32 4, i32 %35)
switch i32 %36, label %40 [
i32 -1, label %45
i32 0, label %37
]
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %50
40: ; preds = %34
%41 = load i32, i32* %3
%42 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %3
%44 = call i32 @dotunneling(i32 %0, i32 %43)
br label %33
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %48)
br label %33
50: ; preds = %37, %8, %1, %18
ret void
}