#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 */

/* Type definitions */
struct sockaddr_storage {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int __socket_bind(char const *, char const *, struct addrinfo *,
                  struct sockaddr_storage *);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket_tcp_bind(const char *address, const char *port,
                    struct sockaddr_storage *addr) {
  struct addrinfo hints;
  int sock;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  if ((sock = __socket_bind(address, port, &hints, addr)) < 0)
    return (sock);
  if (listen(sock, (16)) < 0) {
    perror("listen()");
    close(sock);
    return (-2);
  }
  return (sock);
}


x86 assembly (clang, -O0):
<socket_tcp_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x30(%rbp),%rdx
call   1050 <__socket_bind@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    11d0 <socket_tcp_bind+0x80>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <socket_tcp_bind+0xbd>
mov    -0x34(%rbp),%edi
mov    $0x10,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    1207 <socket_tcp_bind+0xb7>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120d <socket_tcp_bind+0xbd>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_tcp_bind>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1050 <__socket_bind@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d4 <socket_tcp_bind+0x84>
mov    %ebx,%edi
mov    $0x10,%esi
call   1060 <listen@plt>
test   %rax,%rax
js     11e2 <socket_tcp_bind+0x92>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xfffffffe,%ebx
jmp    11d4 <socket_tcp_bind+0x84>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [9 x i8] c"listen()\00"
define i32 @socket_tcp_bind(i8* %0, i8* %1, %struct.sockaddr_storage* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.addrinfo
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
%10 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 16)
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @IPPROTO_TCP
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i8*, i8** %5
%20 = load i8*, i8** %6
%21 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%22 = call i32 @__socket_bind(i8* %19, i8* %20, %struct.addrinfo* %8, %struct.sockaddr_storage* %21)
store i32 %22, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %3
%25 = load i32, i32* %9
store i32 %25, i32* %4
br label %36
26: ; preds = %3
%27 = load i32, i32* %9
%28 = call i64 @listen(i32 %27, i32 16)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%32 = load i32, i32* %9
%33 = call i32 @close(i32 %32)
store i32 -2, i32* %4
br label %36
34: ; preds = %26
%35 = load i32, i32* %9
store i32 %35, i32* %4
br label %36
36: ; preds = %34, %30, %24
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [9 x i8] c"listen()\00"
define i32 @socket_tcp_bind(i8* %0, i8* %1, %struct.sockaddr_storage* %2) {
%4 = alloca %struct.addrinfo
%5 = bitcast %struct.addrinfo* %4 to i8*
%6 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 16)
%7 = load i32, i32* @AI_PASSIVE
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %7, i32* %8
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @IPPROTO_TCP
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %13, i32* %14
%15 = call i32 @__socket_bind(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.sockaddr_storage* %2)
%16 = icmp slt i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %3
%18 = call i64 @listen(i32 %15, i32 16)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @close(i32 %15)
br label %23
23: ; preds = %17, %3, %20
%24 = phi i32 [ -2, %20 ], [ %15, %3 ], [ %15, %17 ]
ret i32 %24
}