#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 /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char const *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int net_create_socket(char *, int /*<<< orphan*/);
int net_listen(char *, int, struct sockaddr *, int);
int /*<<< orphan*/ net_set_error(char *, char *);
int net_tcp_server(char *err, const char *addr, int port) {
  int sock;
  struct sockaddr_in sa;
  if ((sock = net_create_socket(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 (addr && inet_aton(addr, &sa.sin_addr) == 0) {
    net_set_error(err, "Invalid bind address");
    close(sock);
    return -1;
  }
  if (net_listen(err, sock, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
    return -1;
  }
  return sock;
}


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

x86 assembly (clang, -O2):
<net_tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    0x2e4a(%rip),%r13
mov    0x0(%r13),%esi
call   1090 <net_create_socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     123b <net_tcp_server+0xbb>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r12d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0x8(%rsp)
test   %r15,%r15
je     1206 <net_tcp_server+0x86>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
je     1225 <net_tcp_server+0xa5>
lea    0x8(%rsp),%rdx
mov    %rbp,%rdi
mov    %ebx,%esi
mov    $0xc,%ecx
call   10a0 <net_listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%r14d
jmp    123b <net_tcp_server+0xbb>
lea    0xdd4(%rip),%rsi
mov    %rbp,%rdi
call   1070 <net_set_error@plt>
mov    %ebx,%edi
call   1060 <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 @net_tcp_server(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @AF_INET
%12 = call i32 @net_create_socket(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* %7
%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** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %38
28: ; preds = %15
%29 = load i8*, i8** %6
%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 @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 @net_listen(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 @net_tcp_server(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = tail call i32 @net_create_socket(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 %2)
%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* %1, 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* %1, %struct.TYPE_2__* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @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 @net_listen(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
}