#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BasSockUdpAlloc();
scalar_t__ BasSockUdpOpenPassive(int, struct sockaddr_in *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(unsigned short);
int BasSockUdpAllocPassive(int *sock, unsigned short port) {
  int s;
  struct sockaddr_in local;
  s = BasSockUdpAlloc();
  if (s < 0)
    return -1;
  local.sin_family = AF_INET;
  local.sin_port = htons(port);
  local.sin_addr.s_addr = 0;
  if (BasSockUdpOpenPassive(s, &local)) {
    close(s);
    return -1;
  }
  *sock = s;
  return 0;
}


x86 assembly (clang, -O0):
<BasSockUdpAllocPassive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    $0x0,%al
call   1050 <BasSockUdpAlloc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1173 <BasSockUdpAllocPassive+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <BasSockUdpAllocPassive+0x8d>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <BasSockUdpOpenPassive@plt>
cmp    $0x0,%rax
je     11bd <BasSockUdpAllocPassive+0x7d>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <BasSockUdpAllocPassive+0x8d>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BasSockUdpAllocPassive>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
xor    %eax,%eax
call   1050 <BasSockUdpAlloc@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11a4 <BasSockUdpAllocPassive+0x64>
mov    %eax,%ebp
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movzwl %bx,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1060 <BasSockUdpOpenPassive@plt>
test   %rax,%rax
je     119e <BasSockUdpAllocPassive+0x5e>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11a4 <BasSockUdpAllocPassive+0x64>
mov    %ebp,(%r14)
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
@AF_INET = global i32 0
define i32 @BasSockUdpAllocPassive(i32* %0, i16 zeroext %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i32* %0, i32** %4
store i16 %1, i16* %5
%8 = call i32 (...) @BasSockUdpAlloc()
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %29
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i16, i16* %5
%16 = call i32 @htons(i16 zeroext %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i64 0, i64* %19
%20 = load i32, i32* %6
%21 = call i64 @BasSockUdpOpenPassive(i32 %20, %struct.sockaddr_in* %7)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %12
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %3
br label %29
26: ; preds = %12
%27 = load i32, i32* %6
%28 = load i32*, i32** %4
store i32 %27, i32* %28
store i32 0, i32* %3
br label %29
29: ; preds = %26, %23, %11
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
@AF_INET = global i32 0
define i32 @BasSockUdpAllocPassive(i32* nocapture writeonly %0, i16 zeroext %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 (...) @BasSockUdpAlloc()
%6 = icmp slt i32 %5, 0
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @htons(i16 zeroext %1)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 0, i64* %12
%13 = call i64 @BasSockUdpOpenPassive(i32 %5, %struct.sockaddr_in* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %7
%16 = call i32 @close(i32 %5)
br label %18
17: ; preds = %7
store i32 %5, i32* %0
br label %18
18: ; preds = %2, %17, %15
%19 = phi i32 [ -1, %15 ], [ 0, %17 ], [ -1, %2 ]
ret i32 %19
}