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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setnonblocking(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              unsigned int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int CreateTcpListenSocket() {
  int sockfd;
  struct sockaddr_in localAddr;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("End at: %d, ", 128);
    perror("create socket fail");
    return -1;
  }
  setnonblocking(sockfd);
  bzero(&localAddr, sizeof(localAddr));
  localAddr.sin_family = AF_INET;
  localAddr.sin_port = htons(6868);
  localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  unsigned int optval;
  optval = 0x1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, 4);
  if (bind(sockfd, (struct sockaddr *)&localAddr, sizeof(struct sockaddr)) ==
      -1) {
    perror("bind error");
    printf("End at: %d\n", 159);
    return -1;
  }
  if (listen(sockfd, 100) == -1) {
    perror("listen error");
    printf("End at: %d\n", 166);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<CreateTcpListenSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11f6 <CreateTcpListenSocket+0x56>
lea    0xe2c(%rip),%rdi
mov    $0x80,%esi
call   1050 <printf@plt>
lea    0xe28(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <CreateTcpListenSocket+0x149>
mov    -0x8(%rbp),%edi
call   1090 <setnonblocking@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1ad4,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2d70(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    12a4 <CreateTcpListenSocket+0x104>
lea    0xd9e(%rip),%rdi
call   10a0 <perror@plt>
lea    0xd9d(%rip),%rdi
mov    $0x9f,%esi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <CreateTcpListenSocket+0x149>
mov    -0x8(%rbp),%edi
mov    $0x64,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e3 <CreateTcpListenSocket+0x143>
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
lea    0xd5e(%rip),%rdi
mov    $0xa6,%esi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e9 <CreateTcpListenSocket+0x149>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateTcpListenSocket>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e30(%rip),%r15
mov    (%r15),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     126b <CreateTcpListenSocket+0xcb>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <setnonblocking@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    $0x1ad4,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     128a <CreateTcpListenSocket+0xea>
mov    %ebx,%edi
mov    $0x64,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1298 <CreateTcpListenSocket+0xf8>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd8e(%rip),%rdi
mov    $0x80,%esi
call   1050 <printf@plt>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12b7 <CreateTcpListenSocket+0x117>
mov    $0x9f,%ebx
lea    0xd8a(%rip),%rdi
jmp    12a4 <CreateTcpListenSocket+0x104>
mov    $0xa6,%ebx
lea    0xd93(%rip),%rdi
call   10a0 <perror@plt>
lea    0xd7b(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    125f <CreateTcpListenSocket+0xbf>

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 = [13 x i8] c"End at: %d, \00"
@.str.1 = [19 x i8] c"create socket fail\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [12 x i8] c"End at: %d\0A\00"
@.str.4 = [13 x i8] c"listen error\00"
define i32 @CreateTcpListenSocket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 128)
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %44
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i32 @setnonblocking(i32 %13)
%15 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 6868)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
store i32 1, i32* %4
%24 = load i32, i32* %2
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEADDR
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %4, i32 4)
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %12
%33 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 159)
store i32 -1, i32* %1
br label %44
35: ; preds = %12
%36 = load i32, i32* %2
%37 = call i32 @listen(i32 %36, i32 100)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 166)
store i32 -1, i32* %1
br label %44
42: ; preds = %35
%43 = load i32, i32* %2
store i32 %43, i32* %1
br label %44
44: ; preds = %42, %39, %32, %9
%45 = load i32, i32* %1
ret i32 %45
}

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 = [13 x i8] c"End at: %d, \00"
@.str.1 = [19 x i8] c"create socket fail\00"
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [11 x i8] c"bind error\00"
@.str.3 = [12 x i8] c"End at: %d\0A\00"
@.str.4 = [13 x i8] c"listen error\00"
define i32 @CreateTcpListenSocket() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 128)
%10 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %38
11: ; preds = %0
%12 = tail call i32 @setnonblocking(i32 %6)
%13 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 6868)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %6, i32 %22, i32 %23, i32* %2, i32 4)
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i32 @bind(i32 %6, %struct.sockaddr* %25, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %31, label %28
28: ; preds = %11
%29 = call i32 @listen(i32 %6, i32 100)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %36
31: ; preds = %28, %11
%32 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %11 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), %28 ]
%33 = phi i32 [ 159, %11 ], [ 166, %28 ]
%34 = call i32 @perror(i8* %32)
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %36
36: ; preds = %31, %28
%37 = phi i32 [ %6, %28 ], [ -1, %31 ]
br label %38
38: ; preds = %36, %8
%39 = phi i32 [ -1, %8 ], [ %37, %36 ]
ret i32 %39
}