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

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ evutil_closesocket(int);
int neubot_socket_create(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int neubot_socket_listen(int, struct sockaddr_storage *, int /*<<< orphan*/);
int neubot_storage_init(struct sockaddr_storage *, int /*<<< orphan*/ *,
                        char const *, char const *, char const *);
evutil_socket_t neubot_listen(int use_ipv6, const char *address,
                              const char *port) {
  struct sockaddr_storage storage;
  socklen_t salen;
  const char *family;
  evutil_socket_t filedesc;
  int result;
  if (use_ipv6)
    family = "PF_INET6";
  else
    family = "PF_INET";
  result = neubot_storage_init(&storage, &salen, family, address, port);
  if (result == -1)
    return (-1);
  filedesc = neubot_socket_create(storage.ss_family, SOCK_STREAM, 0);
  if (filedesc == -1)
    return (-1);
  result = neubot_socket_listen(filedesc, &storage, salen);
  if (result != 0) {
    (void)evutil_closesocket(filedesc);
    return (-1);
  }
  return (filedesc);
}


x86 assembly (clang, -O0):
<neubot_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     116d <neubot_listen+0x2d>
lea    0xe9c(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    1178 <neubot_listen+0x38>
lea    0xe95(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1040 <neubot_storage_init@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11aa <neubot_listen+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <neubot_listen+0xcc>
mov    -0x20(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <neubot_socket_create@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11d6 <neubot_listen+0x96>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <neubot_listen+0xcc>
mov    -0x34(%rbp),%edi
mov    -0x24(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1030 <neubot_socket_listen@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     1206 <neubot_listen+0xc6>
mov    -0x34(%rbp),%edi
call   1050 <evutil_closesocket@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120c <neubot_listen+0xcc>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<neubot_listen>:
push   %rbp
push   %rbx
push   %rax
mov    %rdx,%r8
mov    %rsi,%rcx
test   %edi,%edi
lea    0xeb7(%rip),%rax
lea    0xea7(%rip),%rdx
cmove  %rax,%rdx
mov    %rsp,%rdi
lea    0x4(%rsp),%rsi
call   1040 <neubot_storage_init@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     11ab <neubot_listen+0x6b>
mov    (%rsp),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <neubot_socket_create@plt>
cmp    $0xffffffff,%eax
je     11ab <neubot_listen+0x6b>
mov    %eax,%ebp
mov    0x4(%rsp),%edx
mov    %rsp,%rsi
mov    %eax,%edi
call   1030 <neubot_socket_listen@plt>
test   %eax,%eax
je     11a9 <neubot_listen+0x69>
mov    %ebp,%edi
call   1050 <evutil_closesocket@plt>
jmp    11ab <neubot_listen+0x6b>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
@.str = [9 x i8] c"PF_INET6\00"
@.str.1 = [8 x i8] c"PF_INET\00"
@SOCK_STREAM = global i32 0
define i32 @neubot_listen(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %10
br label %17
16: ; preds = %3
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8** %10
br label %17
17: ; preds = %16, %15
%18 = load i8*, i8** %10
%19 = load i8*, i8** %6
%20 = load i8*, i8** %7
%21 = call i32 @neubot_storage_init(%struct.sockaddr_storage* %8, i32* %9, i8* %18, i8* %19, i8* %20)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %17
store i32 -1, i32* %4
br label %44
25: ; preds = %17
%26 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %8, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @neubot_socket_create(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 -1, i32* %4
br label %44
33: ; preds = %25
%34 = load i32, i32* %11
%35 = load i32, i32* %9
%36 = call i32 @neubot_socket_listen(i32 %34, %struct.sockaddr_storage* %8, i32 %35)
store i32 %36, i32* %12
%37 = load i32, i32* %12
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = load i32, i32* %11
%41 = call i32 @evutil_closesocket(i32 %40)
store i32 -1, i32* %4
br label %44
42: ; preds = %33
%43 = load i32, i32* %11
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %39, %32, %24
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
@.str = [9 x i8] c"PF_INET6\00"
@.str.1 = [8 x i8] c"PF_INET\00"
@SOCK_STREAM = global i32 0
define i32 @neubot_listen(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp eq i32 %0, 0
%9 = select i1 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0)
%10 = call i32 @neubot_storage_init(%struct.sockaddr_storage* %4, i32* %5, i8* %9, i8* %1, i8* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %24, label %12
12: ; preds = %3
%13 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @neubot_socket_create(i32 %14, i32 %15, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %24, label %18
18: ; preds = %12
%19 = load i32, i32* %5
%20 = call i32 @neubot_socket_listen(i32 %16, %struct.sockaddr_storage* %4, i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @evutil_closesocket(i32 %16)
br label %24
24: ; preds = %18, %12, %3, %22
%25 = phi i32 [ -1, %22 ], [ -1, %3 ], [ -1, %12 ], [ %16, %18 ]
ret i32 %25
}