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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int bind(int, struct sockaddr_in *, 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 /*<<< orphan*/ set_error(char *, int, char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int tcp_socket(char *, int);
int tcp_server(char *addr, int port, char *ebuf, int ebuf_sz) {
  int server_fd;
  struct sockaddr_in sa;
  server_fd = tcp_socket(ebuf, ebuf_sz);
  if (server_fd == -1) {
    set_error(ebuf, ebuf_sz, "Failed to create serversocket %s %d",
              addr ? addr : "<Null>", port);
    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 != NULL && inet_aton(addr, &sa.sin_addr) == 0) {
    set_error(ebuf, ebuf_sz, "Invalid bind address %s", addr);
    close(server_fd);
    return -1;
  }
  if (bind(server_fd, &sa, sizeof(sa)) == -1) {
    set_error(ebuf, ebuf_sz, "Bind error: %s", strerror(errno));
    close(server_fd);
    return -1;
  }
  if (listen(server_fd, 1024) == -1) {
    set_error(ebuf, ebuf_sz, "Listen error: %s", strerror(errno));
    close(server_fd);
    return -1;
  }
  return server_fd;
}


x86 assembly (clang, -O0):
<tcp_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <tcp_socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    122e <tcp_server+0x8e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11f5 <tcp_server+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1205 <tcp_server+0x65>
lea    0xe28(%rip),%rax
mov    %rax,-0x50(%rbp)
jmp    1205 <tcp_server+0x65>
mov    -0x3c(%rbp),%esi
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0xde5(%rip),%rdx
mov    $0x0,%al
call   1070 <set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_server+0x1db>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12b5 <tcp_server+0x115>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    12b5 <tcp_server+0x115>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rcx
lea    0xd91(%rip),%rdx
mov    $0x0,%al
call   1070 <set_error@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_server+0x1db>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1317 <tcp_server+0x177>
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x60(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    %rax,%rcx
lea    0xd47(%rip),%rdx
mov    $0x0,%al
call   1070 <set_error@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_server+0x1db>
mov    -0x28(%rbp),%edi
mov    $0x400,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1375 <tcp_server+0x1d5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    0x2c8e(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x70(%rbp),%rdi
mov    -0x64(%rbp),%esi
mov    %rax,%rcx
lea    0xcf8(%rip),%rdx
mov    $0x0,%al
call   1070 <set_error@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    137b <tcp_server+0x1db>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebx
mov    %rdi,%rbp
mov    %rdx,%rdi
mov    %ecx,%esi
call   1030 <tcp_socket@plt>
cmp    $0xffffffff,%eax
je     1260 <tcp_server+0xc0>
mov    %eax,%r12d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,(%rsp)
test   %rbp,%rbp
je     121e <tcp_server+0x7e>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     12a7 <tcp_server+0x107>
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     1287 <tcp_server+0xe7>
mov    %r12d,%edi
mov    $0x400,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12cc <tcp_server+0x12c>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdf4(%rip),%rdx
jmp    129c <tcp_server+0xfc>
test   %rbp,%rbp
lea    0xdba(%rip),%rcx
cmovne %rbp,%rcx
lea    0xd8b(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebx,%r8d
xor    %eax,%eax
call   1070 <set_error@plt>
jmp    12c6 <tcp_server+0x126>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xda7(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
jmp    12b7 <tcp_server+0x117>
lea    0xd7d(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1070 <set_error@plt>
mov    %r12d,%edi
call   1080 <close@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x10,%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 }
@.str = [36 x i8] c"Failed to create serversocket %s %d\00"
@.str.1 = [7 x i8] c"<Null>\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [24 x i8] c"Invalid bind address %s\00"
@.str.3 = [15 x i8] c"Bind error: %s\00"
@errno = global i32 0
@.str.4 = [17 x i8] c"Listen error: %s\00"
define i32 @tcp_server(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8*, i8** %8
%13 = load i32, i32* %9
%14 = call i32 @tcp_socket(i8* %12, i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %29
17: ; preds = %4
%18 = load i8*, i8** %8
%19 = load i32, i32* %9
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = load i8*, i8** %6
br label %25
24: ; preds = %17
br label %25
25: ; preds = %24, %22
%26 = phi i8* [ %23, %22 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %24 ]
%27 = load i32, i32* %7
%28 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %18, i32 %19, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %26, i32 %27)
store i32 -1, i32* %5
br label %80
29: ; preds = %4
%30 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %31, i32* %32
%33 = load i32, i32* %7
%34 = call i32 @htons(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* @INADDR_ANY
%37 = call i32 @htonl(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i8*, i8** %6
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %54
42: ; preds = %29
%43 = load i8*, i8** %6
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%45 = call i64 @inet_aton(i8* %43, %struct.TYPE_2__* %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %42
%48 = load i8*, i8** %8
%49 = load i32, i32* %9
%50 = load i8*, i8** %6
%51 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %48, i32 %49, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i32, i32* %10
%53 = call i32 @close(i32 %52)
store i32 -1, i32* %5
br label %80
54: ; preds = %42, %29
%55 = load i32, i32* %10
%56 = call i32 @bind(i32 %55, %struct.sockaddr_in* %11, i32 12)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %66
58: ; preds = %54
%59 = load i8*, i8** %8
%60 = load i32, i32* %9
%61 = load i32, i32* @errno
%62 = call i8* @strerror(i32 %61)
%63 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %59, i32 %60, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %10
%65 = call i32 @close(i32 %64)
store i32 -1, i32* %5
br label %80
66: ; preds = %54
%67 = load i32, i32* %10
%68 = call i32 @listen(i32 %67, i32 1024)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %78
70: ; preds = %66
%71 = load i8*, i8** %8
%72 = load i32, i32* %9
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %71, i32 %72, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %74)
%76 = load i32, i32* %10
%77 = call i32 @close(i32 %76)
store i32 -1, i32* %5
br label %80
78: ; preds = %66
%79 = load i32, i32* %10
store i32 %79, i32* %5
br label %80
80: ; preds = %78, %70, %58, %47, %25
%81 = load i32, i32* %5
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
@.str = [36 x i8] c"Failed to create serversocket %s %d\00"
@.str.1 = [7 x i8] c"<Null>\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [24 x i8] c"Invalid bind address %s\00"
@.str.3 = [15 x i8] c"Bind error: %s\00"
@errno = global i32 0
@.str.4 = [17 x i8] c"Listen error: %s\00"
define i32 @tcp_server(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = tail call i32 @tcp_socket(i8* %2, i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %4
%10 = icmp eq i8* %0, null
%11 = select i1 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0
%12 = tail call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %2, i32 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %1)
br label %46
13: ; preds = %4
%14 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = icmp eq i8* %0, null
br i1 %22, label %30, label %23
23: ; preds = %13
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%25 = call i64 @inet_aton(i8* %0, %struct.TYPE_2__* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %2, i32 %3, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%29 = call i32 @close(i32 %7)
br label %46
30: ; preds = %23, %13
%31 = call i32 @bind(i32 %7, %struct.sockaddr_in* %5, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %2, i32 %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* %35)
%37 = call i32 @close(i32 %7)
br label %46
38: ; preds = %30
%39 = call i32 @listen(i32 %7, i32 1024)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, i32, i8*, i8*, ...) @set_error(i8* %2, i32 %3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %43)
%45 = call i32 @close(i32 %7)
br label %46
46: ; preds = %38, %41, %33, %27, %9
%47 = phi i32 [ -1, %9 ], [ -1, %27 ], [ -1, %33 ], [ -1, %41 ], [ %7, %38 ]
ret i32 %47
}