#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_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ in_port_t;
typedef int /*<<< orphan*/ in_addr_t;

/* Variables and functions */
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int net_socket(int /*<<< orphan*/, int /*<<< orphan*/, struct sockaddr_in *);
int net_listen_at(in_addr_t iaIP, in_port_t ipPort, int backlog) {
  struct sockaddr_in sa;
  int sock = net_socket(iaIP, ipPort, &sa);
  if (sock < 0)
    return sock;
  if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    close(sock);
    return -1;
  }
  if (listen(sock, backlog) < 0) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<net_listen_at>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x18(%rbp),%rdx
call   1040 <net_socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1178 <net_listen_at+0x38>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d6 <net_listen_at+0x96>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11a7 <net_listen_at+0x67>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <net_listen_at+0x96>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    11d0 <net_listen_at+0x90>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d6 <net_listen_at+0x96>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_listen_at>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsp,%rdx
call   1040 <net_socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1181 <net_listen_at+0x41>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     1175 <net_listen_at+0x35>
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <listen@plt>
test   %rax,%rax
jns    1181 <net_listen_at+0x41>
mov    %ebx,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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