#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int TCPServer(int port) {
  int sockfd;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("sock error");
    return -1;
  }
  struct sockaddr_in local;
  bzero(&local, sizeof(struct sockaddr_in));
  local.sin_family = AF_INET;
  local.sin_port = htons(port);
  local.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockfd, (struct sockaddr *)&local, sizeof(struct sockaddr_in)) ==
      -1) {
    perror("bind error");
    return -1;
  }
  if (listen(sockfd, 32) == -1) {
    perror("listen error");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<TCPServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11a9 <TCPServer+0x49>
lea    0xe68(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <TCPServer+0xe0>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    120c <TCPServer+0xac>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <TCPServer+0xe0>
mov    -0xc(%rbp),%edi
mov    $0x20,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
jne    123a <TCPServer+0xda>
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <TCPServer+0xe0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<TCPServer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e75(%rip),%r15
mov    (%r15),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11f0 <TCPServer+0x90>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     11f9 <TCPServer+0x99>
mov    %ebx,%edi
mov    $0x20,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
je     1202 <TCPServer+0xa2>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe09(%rip),%rdi
jmp    1209 <TCPServer+0xa9>
lea    0xe0b(%rip),%rdi
jmp    1209 <TCPServer+0xa9>
lea    0xe0d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11e3 <TCPServer+0x83>

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 = [11 x i8] c"sock error\00"
@INADDR_ANY = global i32 0
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
define i32 @TCPServer(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 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
13: ; preds = %1
%14 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %3
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %4
%24 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
29: ; preds = %13
%30 = load i32, i32* %4
%31 = call i32 @listen(i32 %30, i32 32)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %37
35: ; preds = %29
%36 = load i32, i32* %4
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %33, %27, %11
%38 = load i32, i32* %2
ret i32 %38
}

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