#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 /*<<< orphan*/);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, TYPE_1__ *);
int listen(int, 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;
  }
  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);
  if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    anetSetError(err, "bind: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  if (listen(s, 5) == -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    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1213 <anetTcpServer+0x73>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xdfe(%rip),%rsi
call   1070 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <anetTcpServer+0x1ca>
mov    -0x24(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    127c <anetTcpServer+0xdc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%edx
lea    0xda9(%rip),%rsi
call   1070 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <anetTcpServer+0x1ca>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     12bc <anetTcpServer+0x11c>
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <inet_aton@plt>
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1312 <anetTcpServer+0x172>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xd30(%rip),%rsi
call   1070 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <anetTcpServer+0x1ca>
mov    -0x24(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1364 <anetTcpServer+0x1c4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%edx
lea    0xce8(%rip),%rsi
call   1070 <anetSetError@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136a <anetTcpServer+0x1ca>
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    $0x20,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x1,0xc(%rsp)
mov    0x2e1d(%rip),%r12
mov    (%r12),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1284 <anetTcpServer+0xe4>
mov    %eax,%ebx
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12a5 <anetTcpServer+0x105>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
test   %r15,%r15
je     1246 <anetTcpServer+0xa6>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1080 <inet_aton@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     12bc <anetTcpServer+0x11c>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e7 <anetTcpServer+0x147>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb1(%rip),%rsi
jmp    12d1 <anetTcpServer+0x131>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd67(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1070 <anetSetError@plt>
jmp    12e2 <anetTcpServer+0x142>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd52(%rip),%rsi
jmp    12d1 <anetTcpServer+0x131>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd58(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1070 <anetSetError@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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
@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 = [10 x i8] c"bind: %s\0A\00"
@.str.3 = [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 @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 %74
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 @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 %74
33: ; preds = %20
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %34, i32* %35
%36 = load i32, i32* %6
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* @INADDR_ANY
%40 = call i32 @htonl(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i32 %40, i32* %42
%43 = load i8*, i8** %7
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %33
%46 = load i8*, i8** %7
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%48 = call i32 @inet_aton(i8* %46, %struct.TYPE_2__* %47)
br label %49
49: ; preds = %45, %33
%50 = load i32, i32* %8
%51 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%52 = call i32 @bind(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %61
54: ; preds = %49
%55 = load i8*, i8** %5
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 @anetSetError(i8* %55, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %4
br label %74
61: ; preds = %49
%62 = load i32, i32* %8
%63 = call i32 @listen(i32 %62, i32 5)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %72
65: ; preds = %61
%66 = load i8*, i8** %5
%67 = load i32, i32* @errno
%68 = call i32 @strerror(i32 %67)
%69 = call i32 @anetSetError(i8* %66, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
store i32 -1, i32* %4
br label %74
72: ; preds = %61
%73 = load i32, i32* %8
store i32 %73, i32* %4
br label %74
74: ; preds = %72, %65, %54, %26, %15
%75 = load i32, i32* %4
ret i32 %75
}

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 = [10 x i8] c"bind: %s\0A\00"
@.str.3 = [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 @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %55
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 @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 %55
26: ; preds = %16
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 %1)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %32, i32* %33
%34 = icmp eq i8* %2, null
br i1 %34, label %38, label %35
35: ; preds = %26
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%37 = call i32 @inet_aton(i8* %2, %struct.TYPE_2__* %36)
br label %38
38: ; preds = %35, %26
%39 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%40 = call i32 @bind(i32 %10, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = call i32 @close(i32 %10)
br label %55
47: ; preds = %38
%48 = call i32 @listen(i32 %10, i32 5)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i32, i32* @errno
%52 = call i32 @strerror(i32 %51)
%53 = call i32 @anetSetError(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = call i32 @close(i32 %10)
br label %55
55: ; preds = %47, %50, %42, %21, %12
%56 = phi i32 [ -1, %12 ], [ -1, %21 ], [ -1, %42 ], [ -1, %50 ], [ %10, %47 ]
ret i32 %56
}