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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int set_up_listener(int port) {
  int optval = 1;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = INADDR_ANY;
  memset(&sa.sin_zero, 0, 8);
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) ==
      -1) {
    perror("setsocketopt");
    return (-1);
  }
  if (bind(sockfd, (struct sockaddr *)&sa, sizeof(struct sockaddr)) == -1) {
    perror("bind");
    return (-1);
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen");
    return (-1);
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<set_up_listener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1216 <set_up_listener+0xa6>
lea    0xdfb(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <set_up_listener+0x10c>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1248 <set_up_listener+0xd8>
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <set_up_listener+0x10c>
mov    -0x10(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1276 <set_up_listener+0x106>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <set_up_listener+0x10c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_up_listener>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
movl   $0x1,0xc(%rsp)
mov    0x2e57(%rip),%r14
mov    (%r14),%edi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x1c(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1229 <set_up_listener+0xb9>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1232 <set_up_listener+0xc2>
mov    %ebx,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     123b <set_up_listener+0xcb>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd0(%rip),%rdi
jmp    1242 <set_up_listener+0xd2>
lea    0xdd4(%rip),%rdi
jmp    1242 <set_up_listener+0xd2>
lea    0xdd0(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    121e <set_up_listener+0xae>

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

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