#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ sockaddrnumfill(struct sockaddr_in *, int /*<<< orphan*/,
                                   int /*<<< orphan*/);
int tcpnumlisten(int sock, uint32_t ip, uint16_t port, uint16_t queue) {
  struct sockaddr_in sa;
  sockaddrnumfill(&sa, ip, port);
  if (bind(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
    return -1;
  }
  if (listen(sock, queue) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcpnumlisten>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    -0x18(%rbp),%rdi
call   1040 <sockaddrnumfill@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    117a <tcpnumlisten+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <tcpnumlisten+0x72>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <listen@plt>
cmp    $0x0,%rax
jge    119b <tcpnumlisten+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a2 <tcpnumlisten+0x72>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpnumlisten>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %edi,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <sockaddrnumfill@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     116e <tcpnumlisten+0x3e>
mov    %ebp,%edi
mov    %r14d,%esi
call   1030 <listen@plt>
sar    $0x3f,%rax
jmp    1173 <tcpnumlisten+0x43>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @tcpnumlisten(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %7
%12 = load i32, i32* %8
%13 = call i32 @sockaddrnumfill(%struct.sockaddr_in* %10, i32 %11, i32 %12)
%14 = load i32, i32* %6
%15 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%16 = call i64 @bind(i32 %14, %struct.sockaddr* %15, i32 4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %4
store i32 -1, i32* %5
br label %26
19: ; preds = %4
%20 = load i32, i32* %6
%21 = load i32, i32* %9
%22 = call i64 @listen(i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %5
br label %26
25: ; preds = %19
store i32 0, i32* %5
br label %26
26: ; preds = %25, %24, %18
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @tcpnumlisten(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = call i32 @sockaddrnumfill(%struct.sockaddr_in* %5, i32 %1, i32 %2)
%8 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%9 = call i64 @bind(i32 %0, %struct.sockaddr* %8, i32 4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %4
%12 = call i64 @listen(i32 %0, i32 %3)
%13 = ashr i64 %12, 63
%14 = trunc i64 %13 to i32
br label %15
15: ; preds = %11, %4
%16 = phi i32 [ -1, %4 ], [ %14, %11 ]
ret i32 %16
}