#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*/ rv;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ handle_connection(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  static int sock;
  int rv, len;
  struct sockaddr_in addr;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    die("socket");
  rv = 1;
  rv = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &rv, sizeof(rv));
  if (rv == -1)
    die("setsockopt");
  addr.sin_family = AF_INET;
  addr.sin_port = htons(3345);
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  rv = bind(sock, (const struct sockaddr *)&addr, sizeof(addr));
  if (rv == -1)
    die("bind");
  rv = listen(sock, 10);
  if (rv == -1)
    die("listen");
  while (1) {
    len = sizeof(addr);
    rv = accept(sock, (struct sockaddr *)&addr, &len);
    if (rv == -1)
      die("accept");
    handle_connection(rv);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,0x2eae(%rip)
cmpl   $0xffffffff,0x2ea7(%rip)
jne    11d7 <main+0x47>
lea    0xe2e(%rip),%rdi
call   1090 <die@plt>
movl   $0x1,-0x8(%rbp)
mov    0x2e88(%rip),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    121e <main+0x8e>
lea    0xdee(%rip),%rdi
call   1090 <die@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0xd11,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e1e(%rip),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1275 <main+0xe5>
lea    0xda2(%rip),%rdi
call   1090 <die@plt>
mov    0x2df1(%rip),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    129e <main+0x10e>
lea    0xd7e(%rip),%rdi
call   1090 <die@plt>
jmp    12a3 <main+0x113>
movl   $0xc,-0xc(%rbp)
mov    0x2dbc(%rip),%edi
lea    -0x18(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    12d6 <main+0x146>
lea    0xd4d(%rip),%rdi
call   1090 <die@plt>
mov    -0x8(%rbp),%edi
call   1060 <handle_connection@plt>
jmp    12a3 <main+0x113>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e40(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,0x2eb4(%rip)
cmp    $0xffffffff,%eax
jne    11cf <main+0x3f>
lea    0xe3c(%rip),%rdi
call   1090 <die@plt>
mov    0x2e9d(%rip),%eax
movl   $0x1,0x8(%rsp)
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%edx
lea    0x8(%rsp),%rcx
mov    %eax,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,0x8(%rsp)
cmp    $0xffffffff,%eax
jne    1210 <main+0x80>
lea    0xdfc(%rip),%rdi
call   1090 <die@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0xd11,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    0x2e30(%rip),%edi
lea    0x10(%rsp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,0x8(%rsp)
cmp    $0xffffffff,%eax
jne    1260 <main+0xd0>
lea    0xdb7(%rip),%rdi
call   1090 <die@plt>
mov    0x2e06(%rip),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
mov    %eax,0x8(%rsp)
cmp    $0xffffffff,%eax
jne    1285 <main+0xf5>
lea    0xd97(%rip),%rdi
call   1090 <die@plt>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%rbx
lea    0xd88(%rip),%r15
jmp    12a7 <main+0x117>
nopl   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1060 <handle_connection@plt>
movl   $0xc,0xc(%rsp)
mov    0x2db7(%rip),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   10a0 <accept@plt>
mov    %eax,0x8(%rsp)
cmp    $0xffffffff,%eax
jne    12a0 <main+0x110>
mov    %r15,%rdi
call   1090 <die@plt>
mov    0x8(%rsp),%eax
jmp    12a0 <main+0x110>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@main.sock = internal global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@INADDR_LOOPBACK = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* @main.sock
%8 = load i32, i32* @main.sock
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
store i32 1, i32* %2
%13 = load i32, i32* @main.sock
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %2, i32 4)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 3345)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_LOOPBACK
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* @main.sock
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %21
%36 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %21
%38 = load i32, i32* @main.sock
%39 = call i32 @listen(i32 %38, i32 10)
store i32 %39, i32* %2
%40 = load i32, i32* %2
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %37
%43 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %37
br label %45
45: ; preds = %44, %53
store i32 12, i32* %3
%46 = load i32, i32* @main.sock
%47 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%48 = call i32 @accept(i32 %46, %struct.sockaddr* %47, i32* %3)
store i32 %48, i32* %2
%49 = load i32, i32* %2
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %45
%54 = load i32, i32* %2
%55 = call i32 @handle_connection(i32 %54)
br label %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@main.sock = internal unnamed_addr global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [11 x i8] c"setsockopt\00"
@INADDR_LOOPBACK = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [7 x i8] c"accept\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* @main.sock
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @main.sock
br label %14
14: ; preds = %11, %0
%15 = phi i32 [ %13, %11 ], [ %9, %0 ]
store i32 1, i32* %1
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i32* %1, i32 4)
store i32 %18, i32* %1
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %14
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 3345)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_LOOPBACK
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* @main.sock
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %1
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %22
%35 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %22
%37 = load i32, i32* @main.sock
%38 = call i32 @listen(i32 %37, i32 10)
store i32 %38, i32* %1
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
br label %43
43: ; preds = %42, %50
store i32 12, i32* %2
%44 = load i32, i32* @main.sock
%45 = call i32 @accept(i32 %44, %struct.sockaddr* %31, i32* %2)
store i32 %45, i32* %1
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* %1
br label %50
50: ; preds = %47, %43
%51 = phi i32 [ %49, %47 ], [ %45, %43 ]
%52 = call i32 @handle_connection(i32 %51)
br label %43
}