#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 */
typedef int /*<<< orphan*/ u_short;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int errno;
int /*<<< orphan*/ fail(char const *, char *);
int /*<<< orphan*/ failx(char const *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
void ipdivert_bind(const char *test, int s, u_short port, int expect) {
  struct sockaddr_in sin;
  int err;
  bzero(&sin, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(port);
  err = bind(s, (struct sockaddr *)&sin, sizeof(sin));
  if (err < 0) {
    if (expect == 0)
      fail(test, "bind");
    if (errno != expect)
      fail(test, "bind");
  } else {
    if (expect != 0)
      failx(test, "bind");
  }
}


x86 assembly (clang, -O0):
<ipdivert_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    120a <ipdivert_bind+0xaa>
cmpl   $0x0,-0x14(%rbp)
jne    11e3 <ipdivert_bind+0x83>
mov    -0x8(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   1030 <fail@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
je     1205 <ipdivert_bind+0xa5>
mov    -0x8(%rbp),%rdi
lea    0xe00(%rip),%rsi
call   1030 <fail@plt>
jmp    1229 <ipdivert_bind+0xc9>
cmpl   $0x0,-0x14(%rbp)
je     1224 <ipdivert_bind+0xc4>
mov    -0x8(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   1060 <failx@plt>
jmp    1229 <ipdivert_bind+0xc9>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipdivert_bind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
js     11d8 <ipdivert_bind+0x78>
test   %r15d,%r15d
je     1207 <ipdivert_bind+0xa7>
lea    0xe32(%rip),%rsi
mov    %r14,%rdi
call   1060 <failx@plt>
jmp    1207 <ipdivert_bind+0xa7>
test   %r15d,%r15d
jne    11ec <ipdivert_bind+0x8c>
lea    0xe1c(%rip),%rsi
mov    %r14,%rdi
call   1030 <fail@plt>
mov    0x2ddd(%rip),%rax
cmp    %r15d,(%rax)
je     1207 <ipdivert_bind+0xa7>
lea    0xe01(%rip),%rsi
mov    %r14,%rdi
call   1030 <fail@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [5 x i8] c"bind\00"
@errno = global i32 0
define void @ipdivert_bind(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = call i32 @bzero(%struct.sockaddr_in* %9, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %12, i32* %13
%14 = load i32, i32* @INADDR_ANY
%15 = call i32 @htonl(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = load i32, i32* %7
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* %6
%22 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%23 = call i32 @bind(i32 %21, %struct.sockaddr* %22, i32 12)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %40
26: ; preds = %4
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i8*, i8** %5
%31 = call i32 @fail(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %29, %26
%33 = load i32, i32* @errno
%34 = load i32, i32* %8
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i8*, i8** %5
%38 = call i32 @fail(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %36, %32
br label %47
40: ; preds = %4
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** %5
%45 = call i32 @failx(i8* %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %43, %40
br label %47
47: ; preds = %46, %39
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [5 x i8] c"bind\00"
@errno = global i32 0
define void @ipdivert_bind(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = tail call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 %2)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%16 = call i32 @bind(i32 %1, %struct.sockaddr* %15, i32 12)
%17 = icmp slt i32 %16, 0
%18 = icmp eq i32 %3, 0
br i1 %17, label %19, label %27
19: ; preds = %4
br i1 %18, label %20, label %22
20: ; preds = %19
%21 = call i32 @fail(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %19
%23 = load i32, i32* @errno
%24 = icmp eq i32 %23, %3
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = call i32 @fail(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
27: ; preds = %4
br i1 %18, label %30, label %28
28: ; preds = %27
%29 = call i32 @failx(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %27, %28, %22, %25
ret void
}