#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;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ sll;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ SIOCGIFINDEX;
int SOCK_NONBLOCK;
int SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int open_raw(const char *name) {
  struct ifreq ifr;
  struct sockaddr_ll sll;
  int fd;
  if ((fd = socket(AF_PACKET, SOCK_RAW | SOCK_NONBLOCK, htons(ETH_P_ALL))) <
      0) {
    perror("open raw socket");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
  if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
    perror("get interface index");
    return -1;
  }
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = ifr.ifr_ifindex;
  sll.sll_protocol = htons(ETH_P_ALL);
  if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
    perror("bind raw socket to interface");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    0x2e30(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11dc <open_raw+0x6c>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <open_raw+0x120>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1234 <open_raw+0xc4>
lea    0xded(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <open_raw+0x120>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    128a <open_raw+0x11a>
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1290 <open_raw+0x120>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_raw>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2e4a(%rip),%r15
mov    (%r15),%ebx
mov    0x2e58(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e36(%rip),%r12
mov    (%r12),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     123e <open_raw+0xce>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     1247 <open_raw+0xd7>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rsp),%eax
mov    %eax,0x14(%rsp)
mov    (%r12),%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
js     1250 <open_raw+0xe0>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbb(%rip),%rdi
jmp    1257 <open_raw+0xe7>
lea    0xdc2(%rip),%rdi
jmp    1257 <open_raw+0xe7>
lea    0xdcd(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    122f <open_raw+0xbf>

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

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