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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ anetSetError(char *, char *, ...);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int anetTcpServer(char *err, int port, char *bindaddr) {
  int s, on = 1;
  struct sockaddr_in sa;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    anetSetError(err, "socket: %s\n", strerror(errno));
    return -1;
  }
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
    anetSetError(err, "setsockopt SO_REUSEADDR: %s\n", strerror(errno));
    close(s);
    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) {
    if (inet_aton(bindaddr, &sa.sin_addr) == 0) {
      anetSetError(err, "Invalid bind address\n");
      close(s);
      return -1;
    }
  }
  if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    anetSetError(err, "bind: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  if (listen(s, 511) == -1) {
    anetSetError(err, "listen: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<anetTcpServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1225 <anetTcpServer+0x75>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1080 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <anetTcpServer+0x217>
mov    -0x24(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1290 <anetTcpServer+0xe0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xd97(%rip),%rsi
mov    $0x0,%al
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <anetTcpServer+0x217>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1315 <anetTcpServer+0x165>
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    1310 <anetTcpServer+0x160>
mov    -0x10(%rbp),%rdi
lea    0xd34(%rip),%rsi
mov    $0x0,%al
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <anetTcpServer+0x217>
jmp    1315 <anetTcpServer+0x165>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    136d <anetTcpServer+0x1bd>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xced(%rip),%rsi
mov    $0x0,%al
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <anetTcpServer+0x217>
mov    -0x24(%rbp),%edi
mov    $0x1ff,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    13c1 <anetTcpServer+0x211>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c2e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xca3(%rip),%rsi
mov    $0x0,%al
call   1080 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <anetTcpServer+0x217>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetTcpServer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    0x2e0d(%rip),%r12
mov    (%r12),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a7 <anetTcpServer+0xf7>
mov    %eax,%ebx
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12ca <anetTcpServer+0x11a>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,(%rsp)
test   %r15,%r15
je     126b <anetTcpServer+0xbb>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     131d <anetTcpServer+0x16d>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12e1 <anetTcpServer+0x131>
mov    %ebx,%edi
mov    $0x1ff,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    130e <anetTcpServer+0x15e>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda4(%rip),%rsi
jmp    12f6 <anetTcpServer+0x146>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd44(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <anetSetError@plt>
jmp    1309 <anetTcpServer+0x159>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd2d(%rip),%rsi
jmp    12f6 <anetTcpServer+0x146>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd49(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1080 <anetSetError@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd05(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <anetSetError@plt>
jmp    1302 <anetTcpServer+0x152>

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 = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [29 x i8] c"setsockopt SO_REUSEADDR: %s\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [22 x i8] c"Invalid bind address\0A\00"
@.str.3 = [10 x i8] c"bind: %s\0A\00"
@.str.4 = [12 x i8] c"listen: %s\0A\00"
define i32 @anetTcpServer(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 1, i32* %9
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 (i8*, i8*, ...) @anetSetError(i8* %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 -1, i32* %4
br label %82
20: ; preds = %3
%21 = load i32, i32* %8
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_REUSEADDR
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %9, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 (i8*, i8*, ...) @anetSetError(i8* %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %8
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %4
br label %82
33: ; preds = %20
%34 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i32, i32* %6
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %38, i32* %39
%40 = load i32, i32* @INADDR_ANY
%41 = call i32 @htonl(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %41, i32* %43
%44 = load i8*, i8** %7
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %57
46: ; preds = %33
%47 = load i8*, i8** %7
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%49 = call i64 @inet_aton(i8* %47, %struct.TYPE_2__* %48)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %46
%52 = load i8*, i8** %5
%53 = call i32 (i8*, i8*, ...) @anetSetError(i8* %52, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
store i32 -1, i32* %4
br label %82
56: ; preds = %46
br label %57
57: ; preds = %56, %33
%58 = load i32, i32* %8
%59 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%60 = call i32 @bind(i32 %58, %struct.sockaddr* %59, i32 12)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %69
62: ; preds = %57
%63 = load i8*, i8** %5
%64 = load i32, i32* @errno
%65 = call i32 @strerror(i32 %64)
%66 = call i32 (i8*, i8*, ...) @anetSetError(i8* %63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %65)
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
store i32 -1, i32* %4
br label %82
69: ; preds = %57
%70 = load i32, i32* %8
%71 = call i32 @listen(i32 %70, i32 511)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %80
73: ; preds = %69
%74 = load i8*, i8** %5
%75 = load i32, i32* @errno
%76 = call i32 @strerror(i32 %75)
%77 = call i32 (i8*, i8*, ...) @anetSetError(i8* %74, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %76)
%78 = load i32, i32* %8
%79 = call i32 @close(i32 %78)
store i32 -1, i32* %4
br label %82
80: ; preds = %69
%81 = load i32, i32* %8
store i32 %81, i32* %4
br label %82
82: ; preds = %80, %73, %62, %51, %26, %15
%83 = load i32, i32* %4
ret i32 %83
}

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 = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [29 x i8] c"setsockopt SO_REUSEADDR: %s\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [22 x i8] c"Invalid bind address\0A\00"
@.str.3 = [10 x i8] c"bind: %s\0A\00"
@.str.4 = [12 x i8] c"listen: %s\0A\00"
define i32 @anetTcpServer(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 (i8*, i8*, ...) @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %60
16: ; preds = %3
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i32 @setsockopt(i32 %10, i32 %17, i32 %18, i32* %4, i32 4)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 (i8*, i8*, ...) @anetSetError(i8* %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 @close(i32 %10)
br label %60
26: ; preds = %16
%27 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %28, i32* %29
%30 = call i32 @htons(i32 %1)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, 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* %5, i64 0, i32 0, i32 0
store i32 %33, i32* %34
%35 = icmp eq i8* %2, null
br i1 %35, label %43, label %36
36: ; preds = %26
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%38 = call i64 @inet_aton(i8* %2, %struct.TYPE_2__* %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 (i8*, i8*, ...) @anetSetError(i8* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @close(i32 %10)
br label %60
43: ; preds = %36, %26
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%45 = call i32 @bind(i32 %10, %struct.sockaddr* %44, i32 12)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = load i32, i32* @errno
%49 = call i32 @strerror(i32 %48)
%50 = call i32 (i8*, i8*, ...) @anetSetError(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = call i32 @close(i32 %10)
br label %60
52: ; preds = %43
%53 = call i32 @listen(i32 %10, i32 511)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %52
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 (i8*, i8*, ...) @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %57)
%59 = call i32 @close(i32 %10)
br label %60
60: ; preds = %52, %55, %47, %40, %21, %12
%61 = phi i32 [ -1, %12 ], [ -1, %21 ], [ -1, %40 ], [ -1, %47 ], [ -1, %55 ], [ %10, %52 ]
ret i32 %61
}