#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 sll_ifindex;
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sll;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ die_perror(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
void bind_to_interface(int fd, int interface_index) {
  struct sockaddr_ll sll;
  memset(&sll, 0, sizeof(sll));
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = interface_index;
  sll.sll_protocol = htons(0x0003);
  if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0)
    die_perror("bind packet socket");
}


x86 assembly (clang, -O0):
<bind_to_interface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x3,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    11a4 <bind_to_interface+0x64>
lea    0xe61(%rip),%rdi
call   1050 <die_perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_to_interface>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,(%rsp)
mov    $0x3,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %rax,%rax
jns    119c <bind_to_interface+0x5c>
lea    0xe69(%rip),%rdi
call   1050 <die_perror@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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