#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_tunnel(int port) {
  int tunnel = -1;
  int listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in addr = {};
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  if (bind(listen_socket, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("Binding for tunnel\n");
    return -1;
  }
  if (listen(listen_socket, 5) < 0) {
    perror("Listen for tunnel\n");
    return -1;
  }
  puts("Listening for tunnel");
  tunnel = accept(listen_socket, NULL, NULL);
  puts("Tunnel connected\n");
  return tunnel;
}


x86 assembly (clang, -O0):
<get_tunnel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe0d(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    122e <get_tunnel+0x9e>
lea    0xded(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <get_tunnel+0xfd>
mov    -0x10(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    125d <get_tunnel+0xcd>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <get_tunnel+0xfd>
lea    0xdcd(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10a0 <accept@plt>
mov    %eax,-0xc(%rbp)
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tunnel>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2e57(%rip),%r14
mov    (%r14),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe3a(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1223 <get_tunnel+0xa3>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
js     122c <get_tunnel+0xac>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
mov    %eax,%ebx
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde0(%rip),%rdi
jmp    1233 <get_tunnel+0xb3>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1218 <get_tunnel+0x98>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [20 x i8] c"Binding for tunnel\0A\00"
@.str.2 = [19 x i8] c"Listen for tunnel\0A\00"
@.str.3 = [21 x i8] c"Listening for tunnel\00"
@.str.4 = [18 x i8] c"Tunnel connected\0A\00"
define i32 @get_tunnel(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 -1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %5
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* %3
%15 = call i32 @htons(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %5
%21 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%22 = call i64 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %1
%25 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
26: ; preds = %1
%27 = load i32, i32* %5
%28 = call i64 @listen(i32 %27, i32 5)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
32: ; preds = %26
%33 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%34 = load i32, i32* %5
%35 = call i32 @accept(i32 %34, i32* null, i32* null)
store i32 %35, i32* %4
%36 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %32, %30, %24
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [20 x i8] c"Binding for tunnel\0A\00"
@.str.2 = [19 x i8] c"Listen for tunnel\0A\00"
@.str.3 = [21 x i8] c"Listening for tunnel\00"
@.str.4 = [18 x i8] c"Tunnel connected\0A\00"
define i32 @get_tunnel(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i32 %0)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i64 @bind(i32 %6, %struct.sockaddr* %14, i32 12)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %28
19: ; preds = %1
%20 = call i64 @listen(i32 %6, i32 5)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %28
24: ; preds = %19
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @accept(i32 %6, i32* null, i32* null)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %24, %22, %17
%29 = phi i32 [ -1, %17 ], [ -1, %22 ], [ %26, %24 ]
ret i32 %29
}