#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 */
typedef int /*<<< orphan*/ val;
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*/ s_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ TCP_NODELAY;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int start_tcp_server(int port) {
  struct sockaddr_in s_addr;
  int sockfd;
  int val = 1;
  bzero((char *)&s_addr, sizeof(s_addr));
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Could not open Socket!");
    return -1;
  }
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) == -1) {
    perror("Could not set socket option (SO_REUSEADDR)!");
    return -1;
  }
  if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1) {
    perror("Could not set socket option (TCP_NO_DELAY)!");
    return -1;
  }
  s_addr.sin_family = AF_INET;
  s_addr.sin_port = htons(port);
  s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sockfd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr)) < 0) {
    perror("Unable to bind to port");
    return -1;
  }
  if (listen(sockfd, 1) < 0) {
    perror("Not able to Listen on socket");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<start_tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11dd <start_tcp_server+0x5d>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <start_tcp_server+0x177>
mov    -0x1c(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1222 <start_tcp_server+0xa2>
lea    0xe06(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <start_tcp_server+0x177>
mov    -0x1c(%rbp),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1267 <start_tcp_server+0xe7>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <start_tcp_server+0x177>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    12c2 <start_tcp_server+0x142>
lea    0xdbe(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <start_tcp_server+0x177>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    12f1 <start_tcp_server+0x171>
lea    0xda6(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <start_tcp_server+0x177>
mov    -0x1c(%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   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2e30(%rip),%r14
mov    (%r14),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1270 <start_tcp_server+0xf0>
mov    %eax,%ebx
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1279 <start_tcp_server+0xf9>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1282 <start_tcp_server+0x102>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     128b <start_tcp_server+0x10b>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    129c <start_tcp_server+0x11c>
lea    0xe18(%rip),%rdi
jmp    1292 <start_tcp_server+0x112>
lea    0xd89(%rip),%rdi
jmp    1292 <start_tcp_server+0x112>
lea    0xd97(%rip),%rdi
jmp    1292 <start_tcp_server+0x112>
lea    0xdba(%rip),%rdi
jmp    1292 <start_tcp_server+0x112>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Could not open Socket!\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [44 x i8] c"Could not set socket option (SO_REUSEADDR)!\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.2 = [44 x i8] c"Could not set socket option (TCP_NO_DELAY)!\00"
@INADDR_ANY = global i32 0
@.str.3 = [23 x i8] c"Unable to bind to port\00"
@.str.4 = [29 x i8] c"Not able to Listen on socket\00"
define i32 @start_tcp_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %6
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = call i32 @bzero(i8* %7, i32 12)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
15: ; preds = %1
%16 = load i32, i32* %5
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %6, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
23: ; preds = %15
%24 = load i32, i32* %5
%25 = load i32, i32* @IPPROTO_TCP
%26 = load i32, i32* @TCP_NODELAY
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %6, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
31: ; preds = %23
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* %3
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* @INADDR_ANY
%38 = call i32 @htonl(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %5
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i64 @bind(i32 %41, %struct.sockaddr* %42, i32 4)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %31
%46 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
47: ; preds = %31
%48 = load i32, i32* %5
%49 = call i64 @listen(i32 %48, i32 1)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
53: ; preds = %47
%54 = load i32, i32* %5
store i32 %54, i32* %2
br label %55
55: ; preds = %53, %51, %45, %29, %21, %13
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"Could not open Socket!\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [44 x i8] c"Could not set socket option (SO_REUSEADDR)!\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.2 = [44 x i8] c"Could not set socket option (TCP_NO_DELAY)!\00"
@INADDR_ANY = global i32 0
@.str.3 = [23 x i8] c"Unable to bind to port\00"
@.str.4 = [29 x i8] c"Not able to Listen on socket\00"
define i32 @start_tcp_server(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 = call i32 @bzero(i8* %4, i32 12)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %45
13: ; preds = %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %9, i32 %14, i32 %15, i32* %3, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %45
20: ; preds = %13
%21 = load i32, i32* @IPPROTO_TCP
%22 = load i32, i32* @TCP_NODELAY
%23 = call i32 @setsockopt(i32 %9, i32 %21, i32 %22, i32* %3, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %45
27: ; preds = %20
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %0)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%36 = call i64 @bind(i32 %9, %struct.sockaddr* %35, i32 4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %27
%39 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %45
40: ; preds = %27
%41 = call i64 @listen(i32 %9, i32 1)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %40, %43, %38, %25, %18, %11
%46 = phi i32 [ -1, %11 ], [ -1, %18 ], [ -1, %25 ], [ -1, %38 ], [ -1, %43 ], [ %9, %40 ]
ret i32 %46
}