#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 /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int listen(int, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int anetListen(char *err, int fd, struct sockaddr *sa, socklen_t len,
               int backlog) {
  if (bind(fd, sa, len) == -1) {
    anetSetError(err, "bind: %s", strerror(errno));
    close(fd);
    return -1;
  }
  if (listen(fd, backlog) == -1) {
    anetSetError(err, "listen: %s", strerror(errno));
    close(fd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<anetListen>:
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    %r8d,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    11be <anetListen+0x6e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe5b(%rip),%rsi
call   1040 <anetSetError@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <anetListen+0xc5>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    120e <anetListen+0xbe>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe14(%rip),%rsi
call   1040 <anetSetError@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <anetListen+0xc5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetListen>:
push   %rbp
push   %r14
push   %rbx
mov    %r8d,%ebp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %esi,%edi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     1188 <anetListen+0x38>
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <listen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11b6 <anetListen+0x66>
lea    0xe83(%rip),%rbp
jmp    118f <anetListen+0x3f>
lea    0xe71(%rip),%rbp
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <anetSetError@plt>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
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 @anetListen(i8* %0, i32 %1, %struct.sockaddr* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.sockaddr*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store %struct.sockaddr* %2, %struct.sockaddr** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%12 = load i32, i32* %8
%13 = load %struct.sockaddr*, %struct.sockaddr** %9
%14 = load i32, i32* %10
%15 = call i32 @bind(i32 %12, %struct.sockaddr* %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %24
17: ; preds = %5
%18 = load i8*, i8** %7
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @anetSetError(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %8
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %6
br label %37
24: ; preds = %5
%25 = load i32, i32* %8
%26 = load i32, i32* %11
%27 = call i32 @listen(i32 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %24
%30 = load i8*, i8** %7
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @anetSetError(i8* %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
store i32 -1, i32* %6
br label %37
36: ; preds = %24
store i32 0, i32* %6
br label %37
37: ; preds = %36, %29, %17
%38 = load i32, i32* %6
ret i32 %38
}

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 @anetListen(i8* %0, i32 %1, %struct.sockaddr* %2, i32 %3, i32 %4) {
%6 = tail call i32 @bind(i32 %1, %struct.sockaddr* %2, i32 %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i32 @listen(i32 %1, i32 %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %8, %5
%12 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), %8 ]
%13 = load i32, i32* @errno
%14 = tail call i32 @strerror(i32 %13)
%15 = tail call i32 @anetSetError(i8* %0, i8* %12, i32 %14)
%16 = tail call i32 @close(i32 %1)
br label %17
17: ; preds = %11, %8
%18 = phi i32 [ 0, %8 ], [ -1, %11 ]
ret i32 %18
}