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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFINDEX;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_ll *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int ioctl(int, int /*<<< orphan*/, struct sockaddr_ll *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int BindRawSocketToInterface(char *device, int rawsock, int protocol) {
  struct sockaddr_ll sll;
  struct ifreq ifr;
  bzero(&sll, sizeof(sll));
  bzero(&ifr, sizeof(ifr));
  strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
  if ((ioctl(rawsock, SIOCGIFINDEX, &ifr)) == -1) {
    printf("Error getting Interface index !\n");
    exit(-1);
  }
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = ifr.ifr_ifindex;
  sll.sll_protocol = htons(protocol);
  if ((bind(rawsock, (struct sockaddr *)&sll, sizeof(sll))) == -1) {
    perror("Error binding raw socket to interface\n");
    exit(-1);
  }
  return 1;
}


x86 assembly (clang, -O0):
<BindRawSocketToInterface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
lea    -0x40(%rbp),%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0xc(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11f8 <BindRawSocketToInterface+0x78>
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1245 <BindRawSocketToInterface+0xc5>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x1,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BindRawSocketToInterface>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%rbp
lea    0x8(%rsp),%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x18,%esi
call   1090 <bzero@plt>
mov    0x30(%rsp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1228 <BindRawSocketToInterface+0xa8>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x24(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     123e <BindRawSocketToInterface+0xbe>
mov    $0x1,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf8(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32, i32, i64 }
%struct.ifreq = type { i32, i32, i32, i32, i64 }
%struct.sockaddr = type { i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [33 x i8] c"Error getting Interface index !\0A\00"
@AF_PACKET = global i32 0
@.str.1 = [39 x i8] c"Error binding raw socket to interface\0A\00"
define i32 @BindRawSocketToInterface(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_ll
%8 = alloca %struct.ifreq
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = call i32 @bzero(%struct.sockaddr_ll* %7, i32 24)
%10 = bitcast %struct.ifreq* %8 to %struct.sockaddr_ll*
%11 = call i32 @bzero(%struct.sockaddr_ll* %10, i32 24)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 4
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to i8*
%15 = load i8*, i8** %4
%16 = load i32, i32* @IFNAMSIZ
%17 = call i32 @strncpy(i8* %14, i8* %15, i32 %16)
%18 = load i32, i32* %5
%19 = load i32, i32* @SIOCGIFINDEX
%20 = bitcast %struct.ifreq* %8 to %struct.sockaddr_ll*
%21 = call i32 @ioctl(i32 %18, i32 %19, %struct.sockaddr_ll* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %3
%27 = load i32, i32* @AF_PACKET
%28 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 3
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* %6
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %7, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %5
%36 = bitcast %struct.sockaddr_ll* %7 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 24)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %26
%40 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %26
ret i32 1
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32, i32, i64 }
%struct.ifreq = type { i32, i32, i32, i32, i64 }
%struct.sockaddr = type { i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@AF_PACKET = global i32 0
@.str.1 = [39 x i8] c"Error binding raw socket to interface\0A\00"
@str = [32 x i8] c"Error getting Interface index !\00"
define i32 @BindRawSocketToInterface(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_ll
%5 = alloca %struct.ifreq
%6 = bitcast %struct.sockaddr_ll* %4 to i8*
%7 = bitcast %struct.ifreq* %5 to i8*
%8 = call i32 @bzero(%struct.sockaddr_ll* %4, i32 24)
%9 = bitcast %struct.ifreq* %5 to %struct.sockaddr_ll*
%10 = call i32 @bzero(%struct.sockaddr_ll* %9, i32 24)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 4
%12 = load i64, i64* %11
%13 = inttoptr i64 %12 to i8*
%14 = load i32, i32* @IFNAMSIZ
%15 = tail call i32 @strncpy(i8* %13, i8* %0, i32 %14)
%16 = load i32, i32* @SIOCGIFINDEX
%17 = call i32 @ioctl(i32 %1, i32 %16, %struct.sockaddr_ll* %9)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %3
%23 = load i32, i32* @AF_PACKET
%24 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 3
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @htons(i32 %2)
%29 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%31 = call i32 @bind(i32 %1, %struct.sockaddr* %30, i32 24)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 -1)
unreachable
36: ; preds = %22
ret i32 1
}