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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int start_tcp_server(int port) {
  int server_socket;
  struct sockaddr_in server_addr;
  server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (server_socket <= 0)
    return -1;
  bzero((char *)&server_addr, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = INADDR_ANY;
  server_addr.sin_port = htons(port);
  if (bind(server_socket, (struct sockaddr *)&server_addr,
           sizeof(server_addr)) < 0) {
    close(server_socket);
    return -1;
  }
  if (listen(server_socket, 5) != 0) {
    close(server_socket);
    return -1;
  }
  return server_socket;
}


x86 assembly (clang, -O0):
<start_tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     11a4 <start_tcp_server+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <start_tcp_server+0xd5>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1204 <start_tcp_server+0xa4>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <start_tcp_server+0xd5>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
je     122f <start_tcp_server+0xcf>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <start_tcp_server+0xd5>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e73(%rip),%r12
mov    (%r12),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jle    11fc <start_tcp_server+0x9c>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11f0 <start_tcp_server+0x90>
mov    %ebp,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
test   %rax,%rax
je     11f9 <start_tcp_server+0x99>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11fc <start_tcp_server+0x9c>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x10,%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
@IPPROTO_TCP = global i32 0
@INADDR_ANY = global i32 0
define i32 @start_tcp_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = load i32, i32* @IPPROTO_TCP
%9 = call i32 @socket(i32 %6, i32 %7, i32 %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %40
13: ; preds = %1
%14 = bitcast %struct.sockaddr_in* %5 to i8*
%15 = call i32 @bzero(i8* %14, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%20 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* %3
%22 = call i32 @htons(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %13
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %2
br label %40
31: ; preds = %13
%32 = load i32, i32* %4
%33 = call i64 @listen(i32 %32, i32 5)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %2
br label %40
38: ; preds = %31
%39 = load i32, i32* %4
store i32 %39, i32* %2
br label %40
40: ; preds = %38, %35, %28, %12
%41 = load i32, i32* %2
ret i32 %41
}

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