#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_set_error(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ warning(char *, char *);
int net_listen(char *err, int sock, struct sockaddr *sa, socklen_t len) {
  if (bind(sock, sa, len) == -1) {
    net_set_error(err, "bind: %s", strerror(errno));
    warning("bind socket error:\n%s", err);
    close(sock);
    return -1;
  }
  if (listen(sock, 511) == -1) {
    net_set_error(err, "listen: %s", strerror(errno));
    close(sock);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<net_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   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    11da <net_listen+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe4f(%rip),%rsi
call   1050 <net_set_error@plt>
mov    -0x10(%rbp),%rsi
lea    0xe48(%rip),%rdi
call   1030 <warning@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <net_listen+0xd3>
mov    -0x14(%rbp),%edi
mov    $0x1ff,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    122c <net_listen+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe0c(%rip),%rsi
call   1050 <net_set_error@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <net_listen+0xd3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_listen>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     11b0 <net_listen+0x50>
mov    %ebx,%edi
mov    $0x1ff,%esi
call   1060 <listen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11ea <net_listen+0x8a>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe7b(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1050 <net_set_error@plt>
jmp    11de <net_listen+0x7e>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe3b(%rip),%rsi
mov    %r14,%rdi
mov    %eax,%edx
call   1050 <net_set_error@plt>
lea    0xe33(%rip),%rdi
mov    %r14,%rsi
call   1030 <warning@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"bind: %s\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"bind socket error:\0A%s\00"
@.str.2 = [11 x i8] c"listen: %s\00"
define i32 @net_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 %24
15: ; preds = %4
%16 = load i8*, i8** %6
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 @net_set_error(i8* %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %6
%21 = call i32 @warning(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %5
br label %36
24: ; preds = %4
%25 = load i32, i32* %7
%26 = call i32 @listen(i32 %25, i32 511)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %24
%29 = load i8*, i8** %6
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 @net_set_error(i8* %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
store i32 -1, i32* %5
br label %36
35: ; preds = %24
store i32 0, i32* %5
br label %36
36: ; preds = %35, %28, %15
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"bind: %s\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"bind socket error:\0A%s\00"
@.str.2 = [11 x i8] c"listen: %s\00"
define i32 @net_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 %7, label %12
7: ; preds = %4
%8 = load i32, i32* @errno
%9 = tail call i32 @strerror(i32 %8)
%10 = tail call i32 @net_set_error(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @warning(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %19
12: ; preds = %4
%13 = tail call i32 @listen(i32 %1, i32 511)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @net_set_error(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %7, %15
%20 = tail call i32 @close(i32 %1)
br label %21
21: ; preds = %19, %12
%22 = phi i32 [ 0, %12 ], [ -1, %19 ]
ret i32 %22
}