#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ failure();
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_socket(int port) {
  int sd;
  struct sockaddr_in addr;
  int enable = 1;
  sd = socket(PF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("cannot create socket");
    failure();
  }
  if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
    perror("cannot set SO_REUSEADDR");
    failure();
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
    perror("bind failed");
    failure();
  }
  if (listen(sd, 10) != 0) {
    perror("listen failed");
    failure();
  }
  return sd;
}


x86 assembly (clang, -O0):
<listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11cb <listen_socket+0x4b>
lea    0xe41(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1060 <failure@plt>
mov    -0x8(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    120c <listen_socket+0x8c>
lea    0xe15(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1060 <failure@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     126d <listen_socket+0xed>
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1060 <failure@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     1297 <listen_socket+0x117>
lea    0xdae(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   1060 <failure@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movl   $0x1,0xc(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     124d <listen_socket+0xcd>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1265 <listen_socket+0xe5>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    127d <listen_socket+0xfd>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    1292 <listen_socket+0x112>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdac(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1060 <failure@plt>
jmp    11b6 <listen_socket+0x36>
lea    0xda9(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1060 <failure@plt>
jmp    11e3 <listen_socket+0x63>
lea    0xda9(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1060 <failure@plt>
jmp    1231 <listen_socket+0xb1>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   1060 <failure@plt>
jmp    1242 <listen_socket+0xc2>

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

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