#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*/ sin;
typedef int /*<<< orphan*/ on;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned short);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int start_listener(unsigned short port) {
  int fd;
  struct sockaddr_in sin;
  int on = 1;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    goto out1;
  }
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(port);
  sin.sin_addr.s_addr = INADDR_ANY;
  if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    perror("bind");
    goto out2;
  }
  if (listen(fd, 1) < 0) {
    perror("listen");
    goto out2;
  }
  fprintf(stderr, "Listening on port %d\n", port);
  return fd;
out2:
  close(fd);
out1:
  return -1;
}


x86 assembly (clang, -O0):
<start_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11dc <start_listener+0x4c>
lea    0xe2e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b4 <start_listener+0x124>
mov    -0xc(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movzwl -0x6(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1260 <start_listener+0xd0>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ac <start_listener+0x11c>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    1288 <start_listener+0xf8>
lea    0xd8e(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ac <start_listener+0x11c>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
movzwl -0x6(%rbp),%edx
call   1070 <fprintf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <start_listener+0x12b>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_listener>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
movl   $0x1,0x4(%rsp)
mov    0x2e35(%rip),%r15
mov    (%r15),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     126b <start_listener+0xdb>
mov    %eax,%ebx
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
movzwl %bp,%ebp
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1279 <start_listener+0xe9>
mov    %ebx,%edi
mov    $0x1,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     1282 <start_listener+0xf2>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    %ebp,%edx
call   1070 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1295 <start_listener+0x105>
lea    0xd87(%rip),%rdi
jmp    1289 <start_listener+0xf9>
lea    0xd83(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    125e <start_listener+0xce>

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

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
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@stderr = global i32 0
@.str.3 = [22 x i8] c"Listening on port %d\0A\00"
define i32 @start_listener(i16 zeroext %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 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %36
12: ; preds = %1
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %8, i32 %13, i32 %14, i32* %3, i32 4)
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i16 zeroext %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i64 @bind(i32 %8, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %12
%27 = call i64 @listen(i32 %8, i32 1)
%28 = icmp slt i64 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i16 zeroext %0)
br label %36
32: ; preds = %26, %12
%33 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), %26 ]
%34 = call i32 @perror(i8* %33)
%35 = call i32 @close(i32 %8)
br label %36
36: ; preds = %10, %32, %29
%37 = phi i32 [ %8, %29 ], [ -1, %32 ], [ -1, %10 ]
ret i32 %37
}