#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 */
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ si;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char const *);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
void *tcp_server_init(const char *host, uint16_t port) {
  int fd;
  struct sockaddr_in si;
  socklen_t len = sizeof(si);
  si.sin_family = AF_INET;
  si.sin_addr.s_addr = host ? inet_addr(host) : INADDR_ANY;
  si.sin_port = htons(port);
  fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == fd) {
    printf("socket: %s\n", strerror(errno));
    return NULL;
  }
  if (-1 == bind(fd, (struct sockaddr *)&si, sizeof(si))) {
    printf("bind: %s\n", strerror(errno));
    close(fd);
    return NULL;
  }
  if (-1 == listen(fd, SOMAXCONN)) {
    printf("listen: %s\n", strerror(errno));
    close(fd);
    return NULL;
  }
  int *afd = (int *)calloc(1, sizeof(int));
  *afd = accept(fd, (struct sockaddr *)&si, &len);
  if (*afd == -1) {
    printf("accept: %s\n", strerror(errno));
    return NULL;
  }
  return (void *)afd;
}


x86 assembly (clang, -O0):
<tcp_server_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xc,-0x2c(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11de <tcp_server_init+0x3e>
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x3c(%rbp)
jmp    11ea <tcp_server_init+0x4a>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    1256 <tcp_server_init+0xb6>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdbc(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <tcp_server_init+0x1c4>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12a8 <tcp_server_init+0x108>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd7e(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <tcp_server_init+0x1c4>
mov    -0x18(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12fa <tcp_server_init+0x15a>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd36(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <tcp_server_init+0x1c4>
mov    $0x1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
cmpl   $0xffffffff,(%rax)
jne    135c <tcp_server_init+0x1bc>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xcd8(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1364 <tcp_server_init+0x1c4>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_server_init>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
movl   $0xc,0x4(%rsp)
mov    0x2e31(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
test   %rdi,%rdi
je     11ca <tcp_server_init+0x2a>
call   1070 <inet_addr@plt>
jmp    11d3 <tcp_server_init+0x33>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     126f <tcp_server_init+0xcf>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     128e <tcp_server_init+0xee>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     12a5 <tcp_server_init+0x105>
mov    $0x1,%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <accept@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
jne    12cb <tcp_server_init+0x12b>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdb5(%rip),%rdi
jmp    1284 <tcp_server_init+0xe4>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd7c(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
jmp    12c9 <tcp_server_init+0x129>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd69(%rip),%rdi
jmp    12ba <tcp_server_init+0x11a>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd5c(%rip),%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"bind: %s\0A\00"
@SOMAXCONN = global i32 0
@.str.2 = [12 x i8] c"listen: %s\0A\00"
@.str.3 = [12 x i8] c"accept: %s\0A\00"
define i8* @tcp_server_init(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 12, i32* %8
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @inet_addr(i8* %15)
br label %19
17: ; preds = %2
%18 = load i32, i32* @INADDR_ANY
br label %19
19: ; preds = %17, %14
%20 = phi i32 [ %16, %14 ], [ %18, %17 ]
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %5
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = load i32, i32* @IPPROTO_TCP
%29 = call i32 @socket(i32 %26, i32 %27, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %36
32: ; preds = %19
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %34)
store i8* null, i8** %3
br label %75
36: ; preds = %19
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 -1, %39
br i1 %40, label %41, label %47
41: ; preds = %36
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i8* null, i8** %3
br label %75
47: ; preds = %36
%48 = load i32, i32* %6
%49 = load i32, i32* @SOMAXCONN
%50 = call i32 @listen(i32 %48, i32 %49)
%51 = icmp eq i32 -1, %50
br i1 %51, label %52, label %58
52: ; preds = %47
%53 = load i32, i32* @errno
%54 = call i8* @strerror(i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
store i8* null, i8** %3
br label %75
58: ; preds = %47
%59 = call i64 @calloc(i32 1, i32 4)
%60 = inttoptr i64 %59 to i32*
store i32* %60, i32** %9
%61 = load i32, i32* %6
%62 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%63 = call i32 @accept(i32 %61, %struct.sockaddr* %62, i32* %8)
%64 = load i32*, i32** %9
store i32 %63, i32* %64
%65 = load i32*, i32** %9
%66 = load i32, i32* %65
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %72
68: ; preds = %58
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %70)
store i8* null, i8** %3
br label %75
72: ; preds = %58
%73 = load i32*, i32** %9
%74 = bitcast i32* %73 to i8*
store i8* %74, i8** %3
br label %75
75: ; preds = %72, %68, %52, %41, %32
%76 = load i8*, i8** %3
ret i8* %76
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [12 x i8] c"socket: %s\0A\00"
@errno = global i32 0
@.str.1 = [10 x i8] c"bind: %s\0A\00"
@SOMAXCONN = global i32 0
@.str.2 = [12 x i8] c"listen: %s\0A\00"
@.str.3 = [12 x i8] c"accept: %s\0A\00"
define i8* @tcp_server_init(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 12, i32* %4
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %7, i32* %8
%9 = icmp eq i8* %0, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @inet_addr(i8* %0)
br label %14
12: ; preds = %2
%13 = load i32, i32* @INADDR_ANY
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ %11, %10 ], [ %13, %12 ]
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* @IPPROTO_TCP
%22 = tail call i32 @socket(i32 %19, i32 %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %57
28: ; preds = %14
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %22, %struct.sockaddr* %29, i32 12)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @close(i32 %22)
br label %57
37: ; preds = %28
%38 = load i32, i32* @SOMAXCONN
%39 = call i32 @listen(i32 %22, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = call i32 @close(i32 %22)
br label %57
46: ; preds = %37
%47 = call i64 @calloc(i32 1, i32 4)
%48 = inttoptr i64 %47 to i32*
%49 = call i32 @accept(i32 %22, %struct.sockaddr* %29, i32* %4)
store i32 %49, i32* %48
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %53)
br label %57
55: ; preds = %46
%56 = inttoptr i64 %47 to i8*
br label %57
57: ; preds = %51, %55, %41, %32, %24
%58 = phi i8* [ null, %24 ], [ null, %32 ], [ null, %41 ], [ null, %51 ], [ %56, %55 ]
ret i8* %58
}