#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__ uint16_t;
struct sockaddr_in {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int assert(char *, int /*<<< orphan*/);
int /*<<< orphan*/ bind(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printConsole(char *, unsigned int);
int /*<<< orphan*/ setPort(struct sockaddr_in *, scalar_t__);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int /*<<< orphan*/ socket(int /*<<< orphan*/, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int tcpListen(struct sockaddr_in *addr, uint16_t port) {
  int fd = assert("socket", socket(AF_INET, SOCK_STREAM, 0));
  if (fd != -1) {
    int opt = 1;
    if (-1 != assert("setsockopt", setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
                                              (char *)&opt, sizeof opt)) &&
        -1 != assert("bind", bind(fd, setPort(addr, port), sizeof *addr)) &&
        -1 != assert("listen", listen(fd, 3))) {
      printConsole("Listening to port %u.\n", (unsigned int)port);
    } else {
      close(fd);
      fd = -1;
    }
  }
  return fd;
}


x86 assembly (clang, -O0):
<tcpListen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%esi
lea    0xe4e(%rip),%rdi
call   1050 <assert@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12a0 <tcpListen+0x120>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%esi
lea    0xe0f(%rip),%rdi
call   1050 <assert@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     128c <tcpListen+0x10c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <setPort@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%esi
mov    $0x4,%edx
call   1080 <bind@plt>
mov    %eax,%esi
lea    0xddb(%rip),%rdi
call   1050 <assert@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     128c <tcpListen+0x10c>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   1070 <listen@plt>
mov    %eax,%esi
lea    0xdb6(%rip),%rdi
call   1050 <assert@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     128c <tcpListen+0x10c>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xd9c(%rip),%rdi
call   1060 <printConsole@plt>
jmp    129b <tcpListen+0x11b>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    12a0 <tcpListen+0x120>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpListen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
lea    0xe52(%rip),%rdi
mov    %eax,%esi
call   1050 <assert@plt>
cmp    $0xffffffff,%eax
je     1262 <tcpListen+0xe2>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0xe14(%rip),%rdi
mov    %eax,%esi
call   1050 <assert@plt>
cmp    $0xffffffff,%eax
je     125b <tcpListen+0xdb>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <setPort@plt>
mov    %ebp,%edi
mov    %eax,%esi
mov    $0x4,%edx
call   1080 <bind@plt>
lea    0xdf3(%rip),%rdi
mov    %eax,%esi
call   1050 <assert@plt>
cmp    $0xffffffff,%eax
je     125b <tcpListen+0xdb>
mov    %ebp,%edi
mov    $0x3,%esi
call   1070 <listen@plt>
lea    0xdd9(%rip),%rdi
mov    %eax,%esi
call   1050 <assert@plt>
cmp    $0xffffffff,%eax
je     125b <tcpListen+0xdb>
lea    0xdcd(%rip),%rdi
mov    %r14d,%esi
call   1060 <printConsole@plt>
jmp    1267 <tcpListen+0xe7>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [23 x i8] c"Listening to port %u.\0A\00"
define i32 @tcpListen(%struct.sockaddr_in* %0, i64 %1) {
%3 = alloca %struct.sockaddr_in*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store %struct.sockaddr_in* %0, %struct.sockaddr_in** %3
store i64 %1, i64* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = call i32 @assert(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %42
13: ; preds = %2
store i32 1, i32* %6
%14 = load i32, i32* %5
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = bitcast i32* %6 to i8*
%18 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i8* %17, i32 4)
%19 = call i32 @assert(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = icmp ne i32 -1, %19
br i1 %20, label %21, label %38
21: ; preds = %13
%22 = load i32, i32* %5
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%24 = load i64, i64* %4
%25 = call i32 @setPort(%struct.sockaddr_in* %23, i64 %24)
%26 = call i32 @bind(i32 %22, i32 %25, i32 4)
%27 = call i32 @assert(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = icmp ne i32 -1, %27
br i1 %28, label %29, label %38
29: ; preds = %21
%30 = load i32, i32* %5
%31 = call i32 @listen(i32 %30, i32 3)
%32 = call i32 @assert(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = icmp ne i32 -1, %32
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i64, i64* %4
%36 = trunc i64 %35 to i32
%37 = call i32 @printConsole(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %41
38: ; preds = %29, %21, %13
%39 = load i32, i32* %5
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %5
br label %41
41: ; preds = %38, %34
br label %42
42: ; preds = %41, %2
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [23 x i8] c"Listening to port %u.\0A\00"
define i32 @tcpListen(%struct.sockaddr_in* %0, i64 %1) {
%3 = alloca i32
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = tail call i32 @assert(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %32, label %9
9: ; preds = %2
%10 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_REUSEADDR
%13 = call i32 @setsockopt(i32 %7, i32 %11, i32 %12, i8* %10, i32 4)
%14 = call i32 @assert(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %28, label %16
16: ; preds = %9
%17 = call i32 @setPort(%struct.sockaddr_in* %0, i64 %1)
%18 = call i32 @bind(i32 %7, i32 %17, i32 4)
%19 = call i32 @assert(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %28, label %21
21: ; preds = %16
%22 = call i32 @listen(i32 %7, i32 3)
%23 = call i32 @assert(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = trunc i64 %1 to i32
%27 = call i32 @printConsole(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %26)
br label %30
28: ; preds = %21, %16, %9
%29 = call i32 @close(i32 %7)
br label %30
30: ; preds = %28, %25
%31 = phi i32 [ %7, %25 ], [ -1, %28 ]
br label %32
32: ; preds = %30, %2
%33 = phi i32 [ %31, %30 ], [ -1, %2 ]
ret i32 %33
}