#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 */
scalar_t__ bind(int, struct sockaddr *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ shutdown(int, int);
int net_bind(int fd, struct sockaddr *sa, socklen_t len) {
  if (bind(fd, sa, len) < 0) {
    close(fd);
    return -1;
  }
  if (fd < 0) {
    return -1;
  }
  if (listen(fd, 511) < 0) {
    shutdown(fd, 2);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<net_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    117f <net_bind+0x3f>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <net_bind+0x8c>
cmpl   $0x0,-0x8(%rbp)
jge    1195 <net_bind+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <net_bind+0x8c>
mov    -0x8(%rbp),%edi
mov    $0x1ff,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    11c5 <net_bind+0x85>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <net_bind+0x8c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<net_bind>:
push   %rbx
mov    %edi,%ebx
call   1060 <bind@plt>
test   %rax,%rax
js     116e <net_bind+0x2e>
mov    $0xffffffff,%eax
test   %ebx,%ebx
js     116c <net_bind+0x2c>
mov    %ebx,%edi
mov    $0x1ff,%esi
call   1050 <listen@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1177 <net_bind+0x37>
pop    %rbx
ret
mov    %ebx,%edi
call   1040 <close@plt>
jmp    1183 <net_bind+0x43>
mov    %ebx,%edi
mov    $0x2,%esi
call   1030 <shutdown@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

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