#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 {
  int sin_family;
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int AF_INET;
int EINVAL;
int bind(int, struct sockaddr const *, size_t const);
int errno;
int /*<<< orphan*/ htons(int);
int inet_pton(int const, char const *, int /*<<< orphan*/ *);
int listen(int, int);
int socket_listen_tcp(int fd, struct sockaddr_in *addr, const char *host,
                      int port, int backlog) {
  const int addr_family = AF_INET;
  addr->sin_family = addr_family;
  int ok = inet_pton(addr_family, host, &addr->sin_addr.s_addr);
  if (ok == 0)
    return EINVAL;
  if (ok == -1)
    return errno;
  addr->sin_port = htons(port);
  const size_t addrsize = sizeof(struct sockaddr_in);
  int err = bind(fd, (const struct sockaddr *)addr, addrsize);
  if (err == -1)
    return errno;
  err = listen(fd, backlog);
  if (err == -1)
    return errno;
  return 0;
}


x86 assembly (clang, -O0):
<socket_listen_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
add    $0x8,%rdx
call   1040 <inet_pton@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11a1 <socket_listen_tcp+0x61>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <socket_listen_tcp+0xf4>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11bc <socket_listen_tcp+0x7c>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <socket_listen_tcp+0xf4>
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movq   $0xc,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1204 <socket_listen_tcp+0xc4>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <socket_listen_tcp+0xf4>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1050 <listen@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    122d <socket_listen_tcp+0xed>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1234 <socket_listen_tcp+0xf4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_listen_tcp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    %edi,(%rsi)
lea    0x8(%rsi),%rax
mov    %rdx,%rsi
mov    %rax,%rdx
call   1040 <inet_pton@plt>
cmp    $0xffffffff,%eax
je     11b1 <socket_listen_tcp+0x71>
test   %eax,%eax
jne    117e <socket_listen_tcp+0x3e>
mov    0x2e54(%rip),%rax
jmp    11b8 <socket_listen_tcp+0x78>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rbx)
mov    $0xc,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11b1 <socket_listen_tcp+0x71>
mov    %r15d,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11ba <socket_listen_tcp+0x7a>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @socket_listen_tcp(i32 %0, %struct.sockaddr_in* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
store i32 %0, i32* %7
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%16 = load i32, i32* @AF_INET
store i32 %16, i32* %12
%17 = load i32, i32* %12
%18 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %12
%21 = load i8*, i8** %9
%22 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i32 0, i32 2
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
%25 = call i32 @inet_pton(i32 %20, i8* %21, i32* %24)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %5
%29 = load i32, i32* @EINVAL
store i32 %29, i32* %6
br label %57
30: ; preds = %5
%31 = load i32, i32* %13
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = load i32, i32* @errno
store i32 %34, i32* %6
br label %57
35: ; preds = %30
%36 = load i32, i32* %10
%37 = call i32 @htons(i32 %36)
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 1
store i32 %37, i32* %39
store i64 12, i64* %14
%40 = load i32, i32* %7
%41 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%42 = bitcast %struct.sockaddr_in* %41 to %struct.sockaddr*
%43 = call i32 @bind(i32 %40, %struct.sockaddr* %42, i64 12)
store i32 %43, i32* %15
%44 = load i32, i32* %15
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %35
%47 = load i32, i32* @errno
store i32 %47, i32* %6
br label %57
48: ; preds = %35
%49 = load i32, i32* %7
%50 = load i32, i32* %11
%51 = call i32 @listen(i32 %49, i32 %50)
store i32 %51, i32* %15
%52 = load i32, i32* %15
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %48
%55 = load i32, i32* @errno
store i32 %55, i32* %6
br label %57
56: ; preds = %48
store i32 0, i32* %6
br label %57
57: ; preds = %56, %54, %46, %33, %28
%58 = load i32, i32* %6
ret i32 %58
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @socket_listen_tcp(i32 %0, %struct.sockaddr_in* %1, i8* %2, i32 %3, i32 %4) {
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
%9 = tail call i32 @inet_pton(i32 %6, i8* %2, i32* %8)
switch i32 %9, label %14 [
i32 0, label %10
i32 -1, label %12
]
10: ; preds = %5
%11 = load i32, i32* @EINVAL
br label %27
12: ; preds = %5
%13 = load i32, i32* @errno
br label %27
14: ; preds = %5
%15 = tail call i32 @htons(i32 %3)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = tail call i32 @bind(i32 %0, %struct.sockaddr* %17, i64 12)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* @errno
br label %27
22: ; preds = %14
%23 = tail call i32 @listen(i32 %0, i32 %4)
%24 = icmp eq i32 %23, -1
%25 = load i32, i32* @errno
%26 = select i1 %24, i32 %25, i32 0
br label %27
27: ; preds = %22, %20, %12, %10
%28 = phi i32 [ %11, %10 ], [ %13, %12 ], [ %21, %20 ], [ %26, %22 ]
ret i32 %28
}