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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PF_PACKET;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
int SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ if_nametoindex(char const *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int bpf_open_raw_sock(const char *name) {
  struct sockaddr_ll sll;
  int sock;
  sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC,
                htons(ETH_P_ALL));
  if (sock < 0) {
    printf("cannot create raw socket\n");
    return -1;
  }
  memset(&sll, 0, sizeof(sll));
  sll.sll_family = AF_PACKET;
  sll.sll_ifindex = if_nametoindex(name);
  sll.sll_protocol = htons(ETH_P_ALL);
  if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
    printf("bind to %s: %s\n", name, strerror(errno));
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<bpf_open_raw_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    0x2e10(%rip),%rcx
or     (%rcx),%eax
mov    0x2e37(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    %eax,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f8 <bpf_open_raw_sock+0x78>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <bpf_open_raw_sock+0x111>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <if_nametoindex@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    128b <bpf_open_raw_sock+0x10b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <bpf_open_raw_sock+0x111>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_open_raw_sock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2b(%rip),%rax
mov    0x2e04(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e29(%rip),%rax
or     (%rax),%ebx
mov    0x2e00(%rip),%r15
mov    (%r15),%edi
call   1040 <htons@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1259 <bpf_open_raw_sock+0xc9>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r14,%rdi
call   1080 <if_nametoindex@plt>
mov    %eax,0xc(%rsp)
mov    (%r15),%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    126a <bpf_open_raw_sock+0xda>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdbd(%rip),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1265 <bpf_open_raw_sock+0xd5>
lea    0xdb0(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
@ETH_P_ALL = global i32 0
@.str = [26 x i8] c"cannot create raw socket\0A\00"
@AF_PACKET = global i32 0
@.str.1 = [16 x i8] c"bind to %s: %s\0A\00"
@errno = global i32 0
define i32 @bpf_open_raw_sock(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_ll
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @SOCK_NONBLOCK
%9 = or i32 %7, %8
%10 = load i32, i32* @SOCK_CLOEXEC
%11 = or i32 %9, %10
%12 = load i32, i32* @ETH_P_ALL
%13 = call i32 @htons(i32 %12)
%14 = call i32 @socket(i32 %6, i32 %11, i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
19: ; preds = %1
%20 = call i32 @memset(%struct.sockaddr_ll* %4, i32 0, i32 12)
%21 = load i32, i32* @AF_PACKET
%22 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i8*, i8** %3
%24 = call i32 @if_nametoindex(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @ETH_P_ALL
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %5
%30 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %40
33: ; preds = %19
%34 = load i8*, i8** %3
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %36)
%38 = load i32, i32* %5
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %42
40: ; preds = %19
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %33, %17
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
@ETH_P_ALL = global i32 0
@AF_PACKET = global i32 0
@.str.1 = [16 x i8] c"bind to %s: %s\0A\00"
@errno = global i32 0
@str = [25 x i8] c"cannot create raw socket\00"
define i32 @bpf_open_raw_sock(i8* %0) {
%2 = alloca %struct.sockaddr_ll
%3 = bitcast %struct.sockaddr_ll* %2 to i8*
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @SOCK_NONBLOCK
%7 = or i32 %6, %5
%8 = load i32, i32* @SOCK_CLOEXEC
%9 = or i32 %7, %8
%10 = load i32, i32* @ETH_P_ALL
%11 = tail call i32 @htons(i32 %10)
%12 = tail call i32 @socket(i32 %4, i32 %9, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %33
16: ; preds = %1
%17 = call i32 @memset(%struct.sockaddr_ll* %2, i32 0, i32 12)
%18 = load i32, i32* @AF_PACKET
%19 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @if_nametoindex(i8* %0)
%21 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @ETH_P_ALL
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%26 = call i64 @bind(i32 %12, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %16
%29 = load i32, i32* @errno
%30 = call i8* @strerror(i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %30)
%32 = call i32 @close(i32 %12)
br label %33
33: ; preds = %16, %28, %14
%34 = phi i32 [ -1, %14 ], [ -1, %28 ], [ %12, %16 ]
ret i32 %34
}