#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 anetCreateSocket(char *, int /*<<< orphan*/);
int anetListen(char *, int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ kr_net_set_error(char *, char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int kr_net_tcp_server(char *err, int port, char *bindaddr) {
  int s;
  struct sockaddr_in sa;
  if ((s = anetCreateSocket(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) {
    kr_net_set_error(err, "invalid bind address");
    close(s);
    return -1;
  }
  if (anetListen(err, s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
    return -1;
  return s;
}


x86 assembly (clang, -O0):
<kr_net_tcp_server>:
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    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1080 <anetCreateSocket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11bd <kr_net_tcp_server+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <kr_net_tcp_server+0xeb>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123b <kr_net_tcp_server+0xbb>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    123b <kr_net_tcp_server+0xbb>
mov    -0x10(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   10a0 <kr_net_set_error@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <kr_net_tcp_server+0xeb>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0x30(%rbp),%rdx
mov    $0xc,%ecx
call   1030 <anetListen@plt>
cmp    $0xffffffff,%eax
jne    1265 <kr_net_tcp_server+0xe5>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <kr_net_tcp_server+0xeb>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kr_net_tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r12
mov    0x2e4b(%rip),%r13
mov    0x0(%r13),%esi
call   1080 <anetCreateSocket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1239 <kr_net_tcp_server+0xb9>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
test   %r15,%r15
je     1204 <kr_net_tcp_server+0x84>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     1223 <kr_net_tcp_server+0xa3>
lea    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %ebx,%esi
mov    $0xc,%ecx
call   1030 <anetListen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%r14d
jmp    1239 <kr_net_tcp_server+0xb9>
lea    0xdd6(%rip),%rsi
mov    %r12,%rdi
call   10a0 <kr_net_set_error@plt>
mov    %ebx,%edi
call   1070 <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 = [21 x i8] c"invalid bind address\00"
define i32 @kr_net_tcp_server(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 @anetCreateSocket(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 %47
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 %38
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 %38
33: ; preds = %28
%34 = load i8*, i8** %5
%35 = call i32 @kr_net_set_error(i8* %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* %8
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %4
br label %47
38: ; preds = %28, %15
%39 = load i8*, i8** %5
%40 = load i32, i32* %8
%41 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%42 = call i32 @anetListen(i8* %39, i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %38
store i32 -1, i32* %4
br label %47
45: ; preds = %38
%46 = load i32, i32* %8
store i32 %46, i32* %4
br label %47
47: ; preds = %45, %44, %33, %14
%48 = load i32, i32* %4
ret i32 %48
}

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 = [21 x i8] c"invalid bind address\00"
define i32 @kr_net_tcp_server(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 @anetCreateSocket(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %31, 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 %26, 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 %26
23: ; preds = %19
%24 = call i32 @kr_net_set_error(i8* %0, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @close(i32 %7)
br label %31
26: ; preds = %19, %9
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = call i32 @anetListen(i8* %0, i32 %7, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
%30 = select i1 %29, i32 -1, i32 %7
br label %31
31: ; preds = %26, %3, %23
%32 = phi i32 [ -1, %23 ], [ -1, %3 ], [ %30, %26 ]
ret i32 %32
}