#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_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ n;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int socket_listen_tcp(unsigned port) {
  int fd, n = 1;
  struct sockaddr_in addr;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n));
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  addr.sin_port = htons(port);
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    goto fail;
  }
  if (listen(fd, 10) < 0) {
    goto fail;
  }
  return fd;
fail:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<socket_listen_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11c3 <socket_listen_tcp+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1275 <socket_listen_tcp+0xf5>
mov    -0xc(%rbp),%edi
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    123f <socket_listen_tcp+0xbf>
jmp    1266 <socket_listen_tcp+0xe6>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    125b <socket_listen_tcp+0xdb>
jmp    1266 <socket_listen_tcp+0xe6>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1275 <socket_listen_tcp+0xf5>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_listen_tcp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movl   $0x1,0xc(%rsp)
mov    0x2e42(%rip),%r12
mov    (%r12),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     124f <socket_listen_tcp+0xcf>
mov    %eax,%ebp
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1248 <socket_listen_tcp+0xc8>
mov    %ebp,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     1248 <socket_listen_tcp+0xc8>
mov    %ebp,%ebx
jmp    124f <socket_listen_tcp+0xcf>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_LOOPBACK = global i32 0
define i32 @socket_listen_tcp(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* @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 %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 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_LOOPBACK
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %4
%28 = bitcast %struct.sockaddr_in* %6 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 %32
31: ; preds = %12
br label %39
32: ; preds = %12
%33 = load i32, i32* %4
%34 = call i64 @listen(i32 %33, i32 10)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %32
br label %39
37: ; preds = %32
%38 = load i32, i32* %4
store i32 %38, i32* %2
br label %42
39: ; preds = %36, %31
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %2
br label %42
42: ; preds = %39, %37, %11
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_LOOPBACK = global i32 0
define i32 @socket_listen_tcp(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* @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 %30, 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 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_LOOPBACK
%18 = call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %8, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %10
%26 = call i64 @listen(i32 %8, i32 10)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25, %10
%29 = call i32 @close(i32 %8)
br label %30
30: ; preds = %25, %1, %28
%31 = phi i32 [ -1, %28 ], [ -1, %1 ], [ %8, %25 ]
ret i32 %31
}