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

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ SOCK_RAW;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ get_if_idx(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ t01_log(int, char *, ...);
int create_l2_raw_socket(const char *if_name) {
  int ret;
  struct sockaddr_ll sock_addr = {.sll_family = AF_PACKET,
                                  .sll_protocol = 0,
                                  .sll_ifindex = get_if_idx(if_name)};
  int fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (fd < 0) {
    t01_log(3, "failed to create L2 socket %s", strerror(errno));
    exit(1);
  }
  ret = bind(fd, (struct sockaddr *)&sock_addr, sizeof(struct sockaddr_ll));
  if (ret < 0) {
    t01_log(3, "failed to bind socket");
    exit(1);
  }
  return fd;
}


x86 assembly (clang, -O0):
<create_l2_raw_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <get_if_idx@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1208 <create_l2_raw_socket+0x98>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    $0x3,%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1040 <t01_log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1243 <create_l2_raw_socket+0xd3>
mov    $0x3,%edi
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1040 <t01_log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_l2_raw_socket>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
call   1050 <get_if_idx@plt>
mov    %eax,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,0x10(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebp
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11da <create_l2_raw_socket+0x6a>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
js     1207 <create_l2_raw_socket+0x97>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe11(%rip),%rsi
mov    $0x3,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <t01_log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe10(%rip),%rsi
mov    $0x3,%edi
xor    %eax,%eax
call   1040 <t01_log@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [30 x i8] c"failed to create L2 socket %s\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"failed to bind socket\00"
define i32 @create_l2_raw_socket(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_ll
%5 = alloca i32
store i8* %0, i8** %2
%6 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 0
%7 = load i8*, i8** %2
%8 = call i32 @get_if_idx(i8* %7)
store i32 %8, i32* %6
%9 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 1
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %4, i32 0, i32 2
%11 = load i32, i32* @AF_PACKET
store i32 %11, i32* %10
%12 = load i32, i32* @AF_PACKET
%13 = load i32, i32* @SOCK_RAW
%14 = load i32, i32* @ETH_P_ALL
%15 = call i32 @htons(i32 %14)
%16 = call i32 @socket(i32 %12, i32 %13, i32 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %1
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %1
%25 = load i32, i32* %5
%26 = bitcast %struct.sockaddr_ll* %4 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
store i32 %27, i32* %3
%28 = load i32, i32* %3
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %24
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
%struct.sockaddr_ll = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str = [30 x i8] c"failed to create L2 socket %s\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"failed to bind socket\00"
define i32 @create_l2_raw_socket(i8* %0) {
%2 = alloca %struct.sockaddr_ll
%3 = bitcast %struct.sockaddr_ll* %2 to i8*
%4 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
%5 = tail call i32 @get_if_idx(i8* %0)
store i32 %5, i32* %4
%6 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 1
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 2
%8 = load i32, i32* @AF_PACKET
store i32 %8, i32* %7
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @ETH_P_ALL
%11 = tail call i32 @htons(i32 %10)
%12 = tail call i32 @socket(i32 %8, i32 %9, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %12, %struct.sockaddr* %20, i32 12)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 (i32, i8*, ...) @t01_log(i32 3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %19
ret i32 %12
}