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

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
scalar_t__ htons(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
void bind_to_port(int socket, int port) {
  struct sockaddr_in name;
  name.sin_family = PF_INET;
  name.sin_port = (in_port_t)htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  int reuse = 1;
  if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse,
                 sizeof(int)) == -1)
    error("Can't set the 'reuse' option on the socket");
  if (bind(socket, (struct sockaddr *)&name, sizeof(name)) == -1)
    error("Can't bind to socket");
}


x86 assembly (clang, -O0):
<bind_to_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11c7 <bind_to_port+0x77>
lea    0xe3e(%rip),%rdi
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x18,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ed <bind_to_port+0x9d>
lea    0xe43(%rip),%rdi
call   1070 <error@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_to_port>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %esi,%edi
call   1040 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
movl   $0x1,0x4(%rsp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11bf <bind_to_port+0x6f>
lea    0xe46(%rip),%rdi
call   1070 <error@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    11e1 <bind_to_port+0x91>
lea    0xe4f(%rip),%rdi
call   1070 <error@plt>
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [43 x i8] c"Can't set the 'reuse' option on the socket\00"
@.str.1 = [21 x i8] c"Can't bind to socket\00"
define void @bind_to_port(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* @PF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* %4
%10 = call i64 @htons(i32 %9)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i64 %10, i64* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
store i32 1, i32* %6
%16 = load i32, i32* %3
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = bitcast i32* %6 to i8*
%20 = call i32 @setsockopt(i32 %16, i32 %17, i32 %18, i8* %19, i32 4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %2
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 24)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str = [43 x i8] c"Can't set the 'reuse' option on the socket\00"
@.str.1 = [21 x i8] c"Can't bind to socket\00"
define void @bind_to_port(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @PF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i64 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i64 %8, i64* %9
%10 = load i32, i32* @INADDR_ANY
%11 = tail call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %11, i32* %12
%13 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %0, i32 %14, i32 %15, i8* %13, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @error(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @bind(i32 %0, %struct.sockaddr* %21, i32 24)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
ret void
}