#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 int /*<<< orphan*/ yes;
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ a;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int listen_socket(int listen_port) {
  struct sockaddr_in a;
  int s;
  int yes;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    fprintf(stderr, "socket error \n");
    return -1;
  }
  yes = 1;
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) ==
      -1) {
    fprintf(stderr, "setsockopt error \n");
    close(s);
    return -1;
  }
  memset(&a, 0, sizeof(a));
  a.sin_port = htons(listen_port);
  a.sin_family = AF_INET;
  if (bind(s, (struct sockaddr *)&a, sizeof(a)) == -1) {
    fprintf(stderr, "bind error\n");
    close(s);
    return -1;
  }
  printf("accepting connections on port %d\n", listen_port);
  listen(s, 10);
  return s;
}


x86 assembly (clang, -O0):
<listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e1 <listen_socket+0x51>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <listen_socket+0x13a>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    123e <listen_socket+0xae>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <listen_socket+0x13a>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a8 <listen_socket+0x118>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <listen_socket+0x13a>
mov    -0x8(%rbp),%esi
lea    0xd7c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e3d(%rip),%r15
mov    (%r15),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1248 <listen_socket+0xb8>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     125f <listen_socket+0xcf>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     1271 <listen_socket+0xe1>
lea    0xdfb(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
jmp    1292 <listen_socket+0x102>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1080 <fprintf@plt>
jmp    128d <listen_socket+0xfd>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
jmp    1281 <listen_socket+0xf1>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"socket error \0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [19 x i8] c"setsockopt error \0A\00"
@.str.2 = [12 x i8] c"bind error\0A\00"
@.str.3 = [34 x i8] c"accepting connections on port %d\0A\00"
define i32 @listen_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %48
14: ; preds = %1
store i32 1, i32* %6
%15 = load i32, i32* %5
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = bitcast i32* %6 to i8*
%19 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, i8* %18, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* %5
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %2
br label %48
26: ; preds = %14
%27 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 8)
%28 = load i32, i32* %3
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %5
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 8)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %26
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %2
br label %48
42: ; preds = %26
%43 = load i32, i32* %3
%44 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %5
%46 = call i32 @listen(i32 %45, i32 10)
%47 = load i32, i32* %5
store i32 %47, i32* %2
br label %48
48: ; preds = %42, %37, %21, %11
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"socket error \0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [19 x i8] c"setsockopt error \0A\00"
@.str.2 = [12 x i8] c"bind error\0A\00"
@.str.3 = [34 x i8] c"accepting connections on port %d\0A\00"
define i32 @listen_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %38
13: ; preds = %1
store i32 1, i32* %3
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %8, i32 %14, i32 %15, i8* %5, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @close(i32 %8)
br label %38
22: ; preds = %13
%23 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%24 = call i32 @htons(i32 %0)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%29 = call i32 @bind(i32 %8, %struct.sockaddr* %28, i32 8)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @close(i32 %8)
br label %38
35: ; preds = %22
%36 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%37 = call i32 @listen(i32 %8, i32 10)
br label %38
38: ; preds = %35, %31, %18, %10
%39 = phi i32 [ -1, %10 ], [ -1, %18 ], [ -1, %31 ], [ %8, %35 ]
ret i32 %39
}