#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 sockaddr_storage {
  int dummy;
};
struct TYPE_2__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  scalar_t__ sin_family;
  scalar_t__ sin_port;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr_verify;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ assert(int);
int bind_socket(struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ getsockname(int, struct sockaddr *, int *);
scalar_t__ htonl(int /*<<< orphan*/);
scalar_t__ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int test_binder(int port) {
  int fd;
  struct sockaddr_in addr;
  struct sockaddr_storage addr_verify;
  socklen_t len;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  fd = bind_socket((struct sockaddr *)&addr, sizeof(addr));
  assert(fd > 0);
  len = sizeof(addr_verify);
  if (getsockname(fd, (struct sockaddr *)&addr_verify, &len) < 0) {
    perror("getsockname:");
    exit(1);
  }
  assert(addr.sin_family == ((struct sockaddr_in *)&addr_verify)->sin_family);
  assert(addr.sin_addr.s_addr ==
         ((struct sockaddr_in *)&addr_verify)->sin_addr.s_addr);
  assert(addr.sin_port == ((struct sockaddr_in *)&addr_verify)->sin_port);
  if (listen(fd, 5) < 0) {
    perror("listen:");
    exit(1);
  }
  fd = bind_socket((struct sockaddr *)&addr, sizeof(addr));
  assert(fd == -1);
  return 0;
}


x86 assembly (clang, -O0):
<test_binder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0x18,%esi
call   1030 <bind_socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0x4,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10a0 <getsockname@plt>
cmp    $0x0,%rax
jge    1230 <test_binder+0xa0>
lea    0xddf(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rax
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    129c <test_binder+0x10c>
lea    0xd80(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0x18,%esi
call   1030 <bind_socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_binder>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %rax,0x18(%rsp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %rax,0x20(%rsp)
mov    %r14,%rdi
mov    $0x18,%esi
call   1030 <bind_socket@plt>
mov    %eax,%ebx
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1070 <assert@plt>
movl   $0x4,0xc(%rsp)
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <getsockname@plt>
test   %rax,%rax
js     128c <test_binder+0xfc>
mov    0x10(%rsp),%rax
xor    %edi,%edi
cmp    0x8(%rsp),%rax
sete   %dil
call   1070 <assert@plt>
mov    0x20(%rsp),%rax
xor    %edi,%edi
cmp    0x18(%rsp),%rax
sete   %dil
call   1070 <assert@plt>
mov    0x18(%rsp),%rax
xor    %edi,%edi
cmp    0x10(%rsp),%rax
sete   %dil
call   1070 <assert@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     1295 <test_binder+0x105>
lea    0x10(%rsp),%rdi
mov    $0x18,%esi
call   1030 <bind_socket@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
sete   %dil
call   1070 <assert@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd6d(%rip),%rdi
jmp    129c <test_binder+0x10c>
lea    0xd71(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@INADDR_LOOPBACK = global i32 0
@.str = [13 x i8] c"getsockname:\00"
@.str.1 = [8 x i8] c"listen:\00"
define i32 @test_binder(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
store i32 %0, i32* %2
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 24)
%8 = load i64, i64* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i64 %8, i64* %9
%10 = load i32, i32* %2
%11 = call i64 @htons(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i64 %11, i64* %12
%13 = load i32, i32* @INADDR_LOOPBACK
%14 = call i64 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i64 %14, i64* %16
%17 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%18 = call i32 @bind_socket(%struct.sockaddr* %17, i32 24)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp sgt i32 %19, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
store i32 4, i32* %6
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%25 = call i64 @getsockname(i32 %23, %struct.sockaddr* %24, i32* %6)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %1
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %1
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%32 = load i64, i64* %31
%33 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = icmp eq i64 %32, %35
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
%41 = load i64, i64* %40
%42 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in*
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %42, i32 0, i32 2
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = icmp eq i64 %41, %45
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%50 = load i64, i64* %49
%51 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr_in*
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %51, i32 0, i32 1
%53 = load i64, i64* %52
%54 = icmp eq i64 %50, %53
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i32, i32* %3
%58 = call i64 @listen(i32 %57, i32 5)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %30
%61 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %30
%64 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%65 = call i32 @bind_socket(%struct.sockaddr* %64, i32 24)
store i32 %65, i32* %3
%66 = load i32, i32* %3
%67 = icmp eq i32 %66, -1
%68 = zext i1 %67 to i32
%69 = call i32 @assert(i32 %68)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i64, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i64 0
@INADDR_LOOPBACK = global i32 0
@.str = [13 x i8] c"getsockname:\00"
@.str.1 = [8 x i8] c"listen:\00"
define i32 @test_binder(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_storage* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 24)
%9 = load i64, i64* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i64 %9, i64* %10
%11 = call i64 @htons(i32 %0)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i64 %11, i64* %12
%13 = load i32, i32* @INADDR_LOOPBACK
%14 = call i64 @htonl(i32 %13)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2, i32 0
store i64 %14, i64* %15
%16 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%17 = call i32 @bind_socket(%struct.sockaddr* %16, i32 24)
%18 = icmp sgt i32 %17, 0
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
store i32 4, i32* %4
%21 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%22 = call i64 @getsockname(i32 %17, %struct.sockaddr* %21, i32* %4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %1
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %1
%28 = load i64, i64* %10
%29 = bitcast %struct.sockaddr_storage* %3 to i64*
%30 = load i64, i64* %29
%31 = icmp eq i64 %28, %30
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i64, i64* %15
%35 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 4
%36 = bitcast %struct.sockaddr_storage* %35 to i64*
%37 = load i64, i64* %36
%38 = icmp eq i64 %34, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = load i64, i64* %12
%42 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 2
%43 = bitcast %struct.sockaddr_storage* %42 to i64*
%44 = load i64, i64* %43
%45 = icmp eq i64 %41, %44
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = call i64 @listen(i32 %17, i32 5)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %27
%51 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %27
%54 = call i32 @bind_socket(%struct.sockaddr* %16, i32 24)
%55 = icmp eq i32 %54, -1
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
ret i32 0
}