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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
int listen(int, unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setfdreuseaddr(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_tcp_socket(unsigned int port, unsigned int backlog) {
  struct sockaddr_in addr;
  int sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (sockfd == -1)
    do {
      perror("CreateTcpSocket:Create Socket Failure");
      exit(EXIT_FAILURE);
    } while (0);
  setfdreuseaddr(sockfd);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(port);
  addr.sin_family = AF_INET;
  int ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret != 0)
    do {
      perror("CreateTcpSocket:Bind Socket Failure");
      exit(EXIT_FAILURE);
    } while (0);
  ret = listen(sockfd, backlog);
  if (ret != 0)
    do {
      perror("CreateTcpSocket:Listen Socket Failure");
      exit(EXIT_FAILURE);
    } while (0);
  return sockfd;
}


x86 assembly (clang, -O0):
<create_tcp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d3 <create_tcp_socket+0x53>
jmp    11b9 <create_tcp_socket+0x39>
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <setfdreuseaddr@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1240 <create_tcp_socket+0xc0>
jmp    1226 <create_tcp_socket+0xa6>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1060 <listen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1277 <create_tcp_socket+0xf7>
jmp    125d <create_tcp_socket+0xdd>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_tcp_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1209 <create_tcp_socket+0x89>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <setfdreuseaddr@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
jne    1212 <create_tcp_socket+0x92>
mov    %ebp,%edi
mov    %r14d,%esi
call   1060 <listen@plt>
test   %eax,%eax
jne    121b <create_tcp_socket+0x9b>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
jmp    1222 <create_tcp_socket+0xa2>
lea    0xe0d(%rip),%rdi
jmp    1222 <create_tcp_socket+0xa2>
lea    0xe28(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [38 x i8] c"CreateTcpSocket:Create Socket Failure\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@.str.1 = [36 x i8] c"CreateTcpSocket:Bind Socket Failure\00"
@.str.2 = [38 x i8] c"CreateTcpSocket:Listen Socket Failure\00"
define i32 @create_tcp_socket(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %2
br label %14
14: ; preds = %13
%15 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; No predecessors!
br label %19
19: ; preds = %18, %2
%20 = load i32, i32* %6
%21 = call i32 @setfdreuseaddr(i32 %20)
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %3
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %19
br label %37
37: ; preds = %36
%38 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; No predecessors!
br label %42
42: ; preds = %41, %19
%43 = load i32, i32* %6
%44 = load i32, i32* %4
%45 = call i32 @listen(i32 %43, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %42
br label %49
49: ; preds = %48
%50 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; No predecessors!
br label %54
54: ; preds = %53, %42
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [38 x i8] c"CreateTcpSocket:Create Socket Failure\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@.str.1 = [36 x i8] c"CreateTcpSocket:Bind Socket Failure\00"
@.str.2 = [38 x i8] c"CreateTcpSocket:Listen Socket Failure\00"
define i32 @create_tcp_socket(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = tail call i32 @setfdreuseaddr(i32 %7)
%15 = load i32, i32* @INADDR_ANY
%16 = tail call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 %0)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%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 i32 @bind(i32 %7, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %13
%26 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %13
%30 = call i32 @listen(i32 %7, i32 %1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %29
ret i32 %7
}