#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sockaddr;
typedef int in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, int,
                           char *);
int listen(int, int /*<<< orphan*/);
int resolve(char const *, int, struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int unix_set_nonblock(int);
int unix_listen(const char *host, in_port_t port) {
  int fd, ret;
  struct sockaddr_in sockaddr;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1)
    return -1;
  ret = unix_set_nonblock(fd);
  if (ret == -1) {
    close(fd);
    return -1;
  }
  ret = resolve(host, port, &sockaddr);
  if (ret == -1) {
    close(fd);
    return -1;
  }
  ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  if (ret == -1) {
    fprintf(stderr, "Could not bind %s:%d: %s\n", host, port, strerror(errno));
    close(fd);
    return -1;
  }
  ret = listen(fd, SOMAXCONN);
  if (ret == -1) {
    fprintf(stderr, "Bound %s:%d but listen failed: %s\n", host, port,
            strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<unix_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c1 <unix_listen+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <unix_listen+0x187>
mov    -0x18(%rbp),%edi
call   1060 <unix_set_nonblock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ea <unix_listen+0x6a>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <unix_listen+0x187>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1030 <resolve@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121b <unix_listen+0x9b>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <unix_listen+0x187>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    128e <unix_listen+0x10e>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    %rax,%r8
lea    0xd8b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <unix_listen+0x187>
mov    -0x18(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
call   1070 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1301 <unix_listen+0x181>
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%ecx
mov    %rax,%r8
lea    0xd32(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <unix_listen+0x187>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_listen>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     126b <unix_listen+0xeb>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <unix_set_nonblock@plt>
cmp    $0xffffffff,%eax
je     1264 <unix_listen+0xe4>
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <resolve@plt>
cmp    $0xffffffff,%eax
je     1264 <unix_listen+0xe4>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     1213 <unix_listen+0x93>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     1234 <unix_listen+0xb4>
mov    %ebp,%ebx
jmp    126b <unix_listen+0xeb>
mov    0x2dbe(%rip),%rax
mov    (%rax),%r12d
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdce(%rip),%rsi
jmp    1253 <unix_listen+0xd3>
mov    0x2d9d(%rip),%rax
mov    (%rax),%r12d
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc7(%rip),%rsi
mov    %r12d,%edi
mov    %r15,%rdx
mov    %r14d,%ecx
mov    %rax,%r8
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Could not bind %s:%d: %s\0A\00"
@errno = global i32 0
@SOMAXCONN = global i32 0
@.str.1 = [35 x i8] c"Bound %s:%d but listen failed: %s\0A\00"
define i32 @unix_listen(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %64
15: ; preds = %2
%16 = load i32, i32* %6
%17 = call i32 @unix_set_nonblock(i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %3
br label %64
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = load i32, i32* %5
%26 = call i32 @resolve(i8* %24, i32 %25, %struct.sockaddr_in* %8)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %6
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %3
br label %64
32: ; preds = %23
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 4)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %47
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %4
%41 = load i32, i32* %5
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %40, i32 %41, i8* %43)
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %64
47: ; preds = %32
%48 = load i32, i32* %6
%49 = load i32, i32* @SOMAXCONN
%50 = call i32 @listen(i32 %48, i32 %49)
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %62
53: ; preds = %47
%54 = load i32, i32* @stderr
%55 = load i8*, i8** %4
%56 = load i32, i32* %5
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %55, i32 %56, i8* %58)
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %3
br label %64
62: ; preds = %47
%63 = load i32, i32* %6
store i32 %63, i32* %3
br label %64
64: ; preds = %62, %53, %38, %29, %20, %14
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"Could not bind %s:%d: %s\0A\00"
@errno = global i32 0
@SOMAXCONN = global i32 0
@.str.1 = [35 x i8] c"Bound %s:%d but listen failed: %s\0A\00"
define i32 @unix_listen(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %39, label %9
9: ; preds = %2
%10 = tail call i32 @unix_set_nonblock(i32 %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @close(i32 %7)
br label %39
14: ; preds = %9
%15 = call i32 @resolve(i8* %0, i32 %1, %struct.sockaddr_in* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @close(i32 %7)
br label %39
19: ; preds = %14
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @bind(i32 %7, %struct.sockaddr* %20, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i8* %26)
%28 = call i32 @close(i32 %7)
br label %39
29: ; preds = %19
%30 = load i32, i32* @SOMAXCONN
%31 = call i32 @listen(i32 %7, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i8* %36)
%38 = call i32 @close(i32 %7)
br label %39
39: ; preds = %29, %2, %33, %23, %17, %12
%40 = phi i32 [ -1, %12 ], [ -1, %17 ], [ -1, %23 ], [ -1, %33 ], [ -1, %2 ], [ %7, %29 ]
ret i32 %40
}