#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int listen(int, int);
int /*<<< orphan*/ net_err(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int tcp_listen(char *err, int fd, struct sockaddr *sa, socklen_t len) {
  if (bind(fd, sa, len) == -1) {
    net_err(err, "bind: %s", strerror(errno));
    close(fd);
    return -1;
  }
  if (listen(fd, 511) == -1) {
    net_err(err, "listen: %s", strerror(errno));
    close(fd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcp_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ba <tcp_listen+0x6a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe5f(%rip),%rsi
call   1060 <net_err@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <tcp_listen+0xc3>
mov    -0x14(%rbp),%edi
mov    $0x1ff,%esi
call   1040 <listen@plt>
cmp    $0xffffffff,%eax
jne    120c <tcp_listen+0xbc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe16(%rip),%rsi
call   1060 <net_err@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <tcp_listen+0xc3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_listen>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     1189 <tcp_listen+0x39>
mov    %ebx,%edi
mov    $0x1ff,%esi
call   1040 <listen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11b7 <tcp_listen+0x67>
lea    0xe82(%rip),%r15
jmp    1190 <tcp_listen+0x40>
lea    0xe70(%rip),%r15
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <net_err@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"bind: %s\00"
@errno = global i32 0
@.str.1 = [11 x i8] c"listen: %s\00"
define i32 @tcp_listen(i8* %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr*
%9 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store %struct.sockaddr* %2, %struct.sockaddr** %8
store i32 %3, i32* %9
%10 = load i32, i32* %7
%11 = load %struct.sockaddr*, %struct.sockaddr** %8
%12 = load i32, i32* %9
%13 = call i32 @bind(i32 %10, %struct.sockaddr* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load i8*, i8** %6
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 @net_err(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %7
%21 = call i32 @close(i32 %20)
store i32 -1, i32* %5
br label %34
22: ; preds = %4
%23 = load i32, i32* %7
%24 = call i32 @listen(i32 %23, i32 511)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @net_err(i8* %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %7
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %5
br label %34
33: ; preds = %22
store i32 0, i32* %5
br label %34
34: ; preds = %33, %26, %15
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"bind: %s\00"
@errno = global i32 0
@.str.1 = [11 x i8] c"listen: %s\00"
define i32 @tcp_listen(i8* %0, i32 %1, %struct.sockaddr* %2, i32 %3) {
%5 = tail call i32 @bind(i32 %1, %struct.sockaddr* %2, i32 %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i32 @listen(i32 %1, i32 511)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %7, %4
%11 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %7 ]
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 @net_err(i8* %0, i8* %11, i32 %13)
%15 = tail call i32 @close(i32 %1)
br label %16
16: ; preds = %10, %7
%17 = phi i32 [ 0, %7 ], [ -1, %10 ]
ret i32 %17
}