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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ close(int);
int createSocket(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int tcpListen(char *, int, struct sockaddr *, int);
scalar_t__ tcpNonBlock(char *, int);
int /*<<< orphan*/ writeLog(int, char *, char *);
int tcpServer(char *err, int port, char *bindaddr) {
  int s;
  struct sockaddr_in sa;
  if ((s = createSocket(err, AF_INET)) == -1)
    return -1;
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bindaddr && inet_aton(bindaddr, &sa.sin_addr) == 0) {
    close(s);
    return -1;
  }
  if ((tcpNonBlock(err, s)) < 0) {
    writeLog(1, "%s", "set socket nonBlock failed!");
    return -1;
  }
  if (tcpListen(err, s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
    return -1;
  return s;
}


x86 assembly (clang, -O0):
<tcpServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1030 <createSocket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11cd <tcpServer+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <tcpServer+0x115>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123b <tcpServer+0xab>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <inet_aton@plt>
cmp    $0x0,%rax
jne    123b <tcpServer+0xab>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <tcpServer+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   10a0 <tcpNonBlock@plt>
cmp    $0x0,%rax
jge    1275 <tcpServer+0xe5>
mov    $0x1,%edi
lea    0xda3(%rip),%rsi
lea    0xd9f(%rip),%rdx
call   1040 <writeLog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <tcpServer+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    $0xc,%ecx
call   1090 <tcpListen@plt>
cmp    $0xffffffff,%eax
jne    129f <tcpServer+0x10f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <tcpServer+0x115>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpServer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%r12
mov    0x2e3b(%rip),%r13
mov    0x0(%r13),%esi
call   1030 <createSocket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1263 <tcpServer+0xd3>
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
test   %r15,%r15
je     1214 <tcpServer+0x84>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   10b0 <inet_aton@plt>
test   %rax,%rax
je     125c <tcpServer+0xcc>
mov    %r12,%rdi
mov    %ebp,%esi
call   10a0 <tcpNonBlock@plt>
test   %rax,%rax
js     1242 <tcpServer+0xb2>
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %ebp,%esi
mov    $0xc,%ecx
call   1090 <tcpListen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebp
mov    %ebp,%r14d
jmp    1263 <tcpServer+0xd3>
lea    0xdb7(%rip),%rsi
lea    0xdb3(%rip),%rdx
mov    $0x1,%edi
call   1040 <writeLog@plt>
jmp    1263 <tcpServer+0xd3>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
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
@INADDR_ANY = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [28 x i8] c"set socket nonBlock failed!\00"
define i32 @tcpServer(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @AF_INET
%12 = call i32 @createSocket(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %52
15: ; preds = %3
%16 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %6
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i8*, i8** %7
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %36
28: ; preds = %15
%29 = load i8*, i8** %7
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%31 = call i64 @inet_aton(i8* %29, %struct.TYPE_2__* %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %4
br label %52
36: ; preds = %28, %15
%37 = load i8*, i8** %5
%38 = load i32, i32* %8
%39 = call i64 @tcpNonBlock(i8* %37, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @writeLog(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %52
43: ; preds = %36
%44 = load i8*, i8** %5
%45 = load i32, i32* %8
%46 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%47 = call i32 @tcpListen(i8* %44, i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %43
store i32 -1, i32* %4
br label %52
50: ; preds = %43
%51 = load i32, i32* %8
store i32 %51, i32* %4
br label %52
52: ; preds = %50, %49, %41, %33, %14
%53 = load i32, i32* %4
ret i32 %53
}

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
@INADDR_ANY = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [28 x i8] c"set socket nonBlock failed!\00"
define i32 @tcpServer(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = tail call i32 @createSocket(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %35, label %9
9: ; preds = %3
%10 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 %1)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = icmp eq i8* %2, null
br i1 %18, label %25, label %19
19: ; preds = %9
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%21 = call i64 @inet_aton(i8* %2, %struct.TYPE_2__* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @close(i32 %7)
br label %35
25: ; preds = %19, %9
%26 = call i64 @tcpNonBlock(i8* %0, i32 %7)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @writeLog(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %35
30: ; preds = %25
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @tcpListen(i8* %0, i32 %7, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i32 %32, -1
%34 = select i1 %33, i32 -1, i32 %7
br label %35
35: ; preds = %30, %3, %28, %23
%36 = phi i32 [ -1, %23 ], [ -1, %28 ], [ -1, %3 ], [ %34, %30 ]
ret i32 %36
}