#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;
};

/* 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;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listenOn(int port) {
  int sockfd;
  int yes = 1;
  struct sockaddr_in host_addr;
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
    return -1;
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    return -1;
  host_addr.sin_family = AF_INET;
  host_addr.sin_port = htons(port);
  host_addr.sin_addr.s_addr = INADDR_ANY;
  memset(&(host_addr.sin_zero), '\0', 8);
  if (bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) ==
      -1)
    return -1;
  if (listen(sockfd, 1023) == -1)
    return -1;
  return sockfd;
}


x86 assembly (clang, -O0):
<listenOn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11a3 <listenOn+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <listenOn+0xfd>
mov    -0xc(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11dc <listenOn+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <listenOn+0xfd>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1235 <listenOn+0xd5>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <listenOn+0xfd>
mov    -0xc(%rbp),%edi
mov    $0x3ff,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    1257 <listenOn+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <listenOn+0xfd>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenOn>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movl   $0x1,0xc(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     1227 <listenOn+0xc7>
mov    %eax,%ebx
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1227 <listenOn+0xc7>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     1227 <listenOn+0xc7>
mov    %ebx,%edi
mov    $0x3ff,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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