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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFINDEX;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memcpy(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(char *, char *, int /*<<< orphan*/);
int raw_socket_bind(char *device, int fd, int proto) {
  struct sockaddr_ll sll;
  struct ifreq ifr;
  memcpy(&sll, 0, sizeof(sll));
  strncpy((char *)ifr.ifr_name, device, IFNAMSIZ);
  if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
    perror("ioctl() failure: ");
    return -1;
  }
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = ifr.ifr_ifindex;
  sll.sll_protocol = htons(proto);
  if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
    perror("bind() failure: ");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<raw_socket_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11cf <raw_socket_bind+0x6f>
lea    0xe42(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <raw_socket_bind+0xc6>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    121f <raw_socket_bind+0xbf>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1226 <raw_socket_bind+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<raw_socket_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memcpy@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     11f6 <raw_socket_bind+0x96>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x18(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     11ff <raw_socket_bind+0x9f>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe03(%rip),%rdi
jmp    1206 <raw_socket_bind+0xa6>
lea    0xe0c(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11e8 <raw_socket_bind+0x88>

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

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.ifreq = type { i32, i64 }
%struct.sockaddr = type { i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [18 x i8] c"ioctl() failure: \00"
@AF_PACKET = global i32 0
@.str.1 = [17 x i8] c"bind() failure: \00"
define i32 @raw_socket_bind(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 @memcpy(%struct.sockaddr_ll* %4, i32 0, i32 12)
%9 = load i32, i32* @IFNAMSIZ
%10 = call i32 @strncpy(i8* undef, i8* %0, i32 %9)
%11 = load i32, i32* @SIOCGIFINDEX
%12 = call i64 @ioctl(i32 %1, i32 %11, %struct.ifreq* %5)
%13 = icmp slt i64 %12, 0
br i1 %13, label %25, label %14
14: ; preds = %3
%15 = load i32, i32* @AF_PACKET
%16 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i64 0, i32 0
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %2)
%21 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%23 = call i64 @bind(i32 %1, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %14, %3
%26 = phi i8* [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), %14 ]
%27 = call i32 @perror(i8* %26)
br label %28
28: ; preds = %25, %14
%29 = phi i32 [ 0, %14 ], [ -1, %25 ]
ret i32 %29
}