#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*/ serv;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int listen(int, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sapi_inet_tcp_server_create(char *ip, int port, int n_conns) {
  int ret;
  struct sockaddr_in serv = {.sin_family = AF_INET,
                             .sin_addr.s_addr = inet_addr(ip),
                             .sin_port = htons(port)};
  int sock;
  int opt = 1;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    return -1;
  ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  if (ret < 0)
    goto err_sockopt;
  ret = bind(sock, (struct sockaddr *)&serv, sizeof(serv));
  if (ret < 0)
    goto err_bind;
  ret = listen(sock, n_conns);
  if (ret < 0)
    goto err_listen;
  return sock;
err_listen:
err_bind:
err_sockopt:
  close(sock);
  return -1;
}


x86 assembly (clang, -O0):
<sapi_inet_tcp_server_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x30(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11de <sapi_inet_tcp_server_create+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <sapi_inet_tcp_server_create+0x108>
mov    -0x2c(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1214 <sapi_inet_tcp_server_create+0xa4>
jmp    1269 <sapi_inet_tcp_server_create+0xf9>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1237 <sapi_inet_tcp_server_create+0xc7>
jmp    1264 <sapi_inet_tcp_server_create+0xf4>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1070 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1254 <sapi_inet_tcp_server_create+0xe4>
jmp    125f <sapi_inet_tcp_server_create+0xef>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1278 <sapi_inet_tcp_server_create+0x108>
jmp    1264 <sapi_inet_tcp_server_create+0xf4>
jmp    1269 <sapi_inet_tcp_server_create+0xf9>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sapi_inet_tcp_server_create>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edx,%r14d
mov    %rdi,%rbx
mov    %esi,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x18(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     121b <sapi_inet_tcp_server_create+0xab>
mov    %eax,%ebp
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1214 <sapi_inet_tcp_server_create+0xa4>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %eax,%eax
js     1214 <sapi_inet_tcp_server_create+0xa4>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <listen@plt>
test   %eax,%eax
js     1214 <sapi_inet_tcp_server_create+0xa4>
mov    %ebp,%ebx
jmp    121b <sapi_inet_tcp_server_create+0xab>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
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
define i32 @sapi_inet_tcp_server_create(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%13 = load i32, i32* %6
%14 = call i32 @htons(i32 %13)
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
%17 = load i8*, i8** %5
%18 = call i32 @inet_addr(i8* %17)
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
%20 = load i32, i32* @AF_INET
store i32 %20, i32* %19
store i32 1, i32* %11
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %3
store i32 -1, i32* %4
br label %56
27: ; preds = %3
%28 = load i32, i32* %10
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i32* %11, i32 4)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %27
br label %53
35: ; preds = %27
%36 = load i32, i32* %10
%37 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%38 = call i32 @bind(i32 %36, %struct.sockaddr* %37, i32 12)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %35
br label %52
42: ; preds = %35
%43 = load i32, i32* %10
%44 = load i32, i32* %7
%45 = call i32 @listen(i32 %43, i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %42
br label %51
49: ; preds = %42
%50 = load i32, i32* %10
store i32 %50, i32* %4
br label %56
51: ; preds = %48
br label %52
52: ; preds = %51, %41
br label %53
53: ; preds = %52, %34
%54 = load i32, i32* %10
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %4
br label %56
56: ; preds = %53, %49, %26
%57 = load i32, i32* %4
ret i32 %57
}

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
define i32 @sapi_inet_tcp_server_create(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%8 = tail call i32 @htons(i32 %1)
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%10 = tail call i32 @inet_addr(i8* %0)
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
%12 = load i32, i32* @AF_INET
store i32 %12, i32* %11
%13 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %12, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %31, label %17
17: ; preds = %3
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = call i32 @setsockopt(i32 %15, i32 %18, i32 %19, i32* %5, i32 4)
%21 = icmp slt i32 %20, 0
br i1 %21, label %29, label %22
22: ; preds = %17
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i32 @bind(i32 %15, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = call i32 @listen(i32 %15, i32 %2)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22, %26, %17
%30 = call i32 @close(i32 %15)
br label %31
31: ; preds = %26, %3, %29
%32 = phi i32 [ -1, %29 ], [ -1, %3 ], [ %15, %26 ]
ret i32 %32
}