#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_ll {
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_IP;
int bind(int, void *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
void socket_bind(int fd) {
  struct sockaddr_ll addr = {};
  addr.sll_family = AF_PACKET;
  addr.sll_protocol = htons(ETH_P_IP);
  if (bind(fd, (void *)&addr, sizeof(addr)) == -1)
    error(1, errno, "packetsock bind");
}


x86 assembly (clang, -O0):
<socket_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    11ac <socket_bind+0x6c>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe59(%rip),%rdx
call   1060 <error@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_bind>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1040 <bind@plt>
cmp    $0xffffffff,%eax
jne    1186 <socket_bind+0x56>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdx
mov    $0x1,%edi
call   1050 <error@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32 }
@AF_PACKET = global i32 0
@ETH_P_IP = global i32 0
@errno = global i32 0
@.str = [16 x i8] c"packetsock bind\00"
define void @socket_bind(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_ll
store i32 %0, i32* %2
%4 = bitcast %struct.sockaddr_ll* %3 to i8*
%5 = load i32, i32* @AF_PACKET
%6 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 1
store i32 %5, i32* %6
%7 = load i32, i32* @ETH_P_IP
%8 = call i32 @htons(i32 %7)
%9 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i32 0, i32 0
store i32 %8, i32* %9
%10 = load i32, i32* %2
%11 = bitcast %struct.sockaddr_ll* %3 to i8*
%12 = call i32 @bind(i32 %10, i8* %11, i32 8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = call i32 @error(i32 1, i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %14, %1
ret void
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32 }
@AF_PACKET = global i32 0
@ETH_P_IP = global i32 0
@errno = global i32 0
@.str = [16 x i8] c"packetsock bind\00"
define void @socket_bind(i32 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to %struct.sockaddr_ll*
%4 = bitcast i64* %2 to i8*
%5 = load i32, i32* @AF_PACKET
%6 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 1
store i32 %5, i32* %6
%7 = load i32, i32* @ETH_P_IP
%8 = tail call i32 @htons(i32 %7)
%9 = bitcast i64* %2 to i32*
store i32 %8, i32* %9
%10 = call i32 @bind(i32 %0, i8* %4, i32 8)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = call i32 @error(i32 1, i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %1
ret void
}