#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close_noeintr(int);
int listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_listen_socket(struct addrinfo *info) {
  int fd;
  fd = socket(info->ai_family, SOCK_STREAM, 0);
  if (fd == -1)
    return -1;
  if (bind(fd, info->ai_addr, info->ai_addrlen) == -1) {
    close_noeintr(fd);
    return -1;
  }
  if (listen(fd, SOMAXCONN) == -1) {
    close_noeintr(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<make_listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    117c <make_listen_socket+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <make_listen_socket+0xa2>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ae <make_listen_socket+0x6e>
mov    -0x14(%rbp),%edi
call   1030 <close_noeintr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <make_listen_socket+0xa2>
mov    -0x14(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
jne    11dc <make_listen_socket+0x9c>
mov    -0x14(%rbp),%edi
call   1030 <close_noeintr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <make_listen_socket+0xa2>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_listen_socket>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x8(%rdi),%edi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1199 <make_listen_socket+0x59>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     1192 <make_listen_socket+0x52>
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
je     1192 <make_listen_socket+0x52>
mov    %ebp,%r14d
jmp    1199 <make_listen_socket+0x59>
mov    %ebp,%edi
call   1030 <close_noeintr@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@SOMAXCONN = global i32 0
define i32 @make_listen_socket(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store %struct.addrinfo* %0, %struct.addrinfo** %3
%5 = load %struct.addrinfo*, %struct.addrinfo** %3
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
%7 = load i32, i32* %6
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %36
13: ; preds = %1
%14 = load i32, i32* %4
%15 = load %struct.addrinfo*, %struct.addrinfo** %3
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @bind(i32 %14, i32 %17, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = load i32, i32* %4
%25 = call i32 @close_noeintr(i32 %24)
store i32 -1, i32* %2
br label %36
26: ; preds = %13
%27 = load i32, i32* %4
%28 = load i32, i32* @SOMAXCONN
%29 = call i32 @listen(i32 %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = load i32, i32* %4
%33 = call i32 @close_noeintr(i32 %32)
store i32 -1, i32* %2
br label %36
34: ; preds = %26
%35 = load i32, i32* %4
store i32 %35, i32* %2
br label %36
36: ; preds = %34, %31, %23, %12
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@SOCK_STREAM = global i32 0
@SOMAXCONN = global i32 0
define i32 @make_listen_socket(%struct.addrinfo* nocapture %0) {
%2 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%3 = load i32, i32* %2
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %20, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @bind(i32 %5, i32 %9, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %7
%15 = load i32, i32* @SOMAXCONN
%16 = tail call i32 @listen(i32 %5, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14, %7
%19 = tail call i32 @close_noeintr(i32 %5)
br label %20
20: ; preds = %18, %14, %1
%21 = phi i32 [ -1, %1 ], [ %5, %14 ], [ -1, %18 ]
ret i32 %21
}