#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int accept_connection(int port) {
  int bound;
  int sock;
  struct sockaddr_in sin;
  int on = 1;
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons(5900 + port);
  bound = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  if (bound < 0) {
    perror("socket");
    exit(1);
  }
  setsockopt(bound, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  if (bind(bound, (struct sockaddr *)&sin, sizeof(struct sockaddr_in))) {
    perror("bind");
    exit(1);
  }
  listen(bound, 1);
  sock = accept(bound, NULL, 0);
  close(bound);
  return sock;
}


x86 assembly (clang, -O0):
<accept_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
add    $0x170c,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    120e <accept_connection+0x7e>
lea    0xe01(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
mov    0x2d97(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1263 <accept_connection+0xd3>
lea    0xdb3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accept_connection>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movl   $0x1,0x4(%rsp)
mov    0x2e3b(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
add    $0x170c,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1249 <accept_connection+0xb9>
mov    %eax,%ebx
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    1252 <accept_connection+0xc2>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdb0(%rip),%rdi
jmp    1259 <accept_connection+0xc9>
lea    0xdae(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [5 x i8] c"bind\00"
define i32 @accept_connection(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @INADDR_ANY
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %8, i32* %9
%10 = add nsw i32 %0, 5900
%11 = tail call i32 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = load i32, i32* @IPPROTO_IP
%16 = tail call i32 @socket(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %16, i32 %22, i32 %23, i32* %3, i32 4)
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %16, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = call i32 @listen(i32 %16, i32 1)
%33 = call i32 @accept(i32 %16, i32* null, i32 0)
%34 = call i32 @close(i32 %16)
ret i32 %33
}