#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_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ if_nametoindex(char const *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_monitor(const char *ifname) {
  struct sockaddr_ll ll;
  int sock;
  memset(&ll, 0, sizeof ll);
  ll.sll_family = AF_PACKET;
  ll.sll_ifindex = if_nametoindex(ifname);
  sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (sock < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  if (bind(sock, (struct sockaddr *)&ll, sizeof ll) < 0) {
    perror("bind");
    exit(EXIT_FAILURE);
  }
  return sock;
}


x86 assembly (clang, -O0):
<open_monitor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <if_nametoindex@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fe <open_monitor+0x8e>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <bind@plt>
cmp    $0x0,%rax
jge    1233 <open_monitor+0xc3>
lea    0xde7(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_monitor>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e44(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
call   1050 <if_nametoindex@plt>
mov    %eax,(%rsp)
mov    0x0(%rbp),%ebx
mov    0x2e40(%rip),%rax
mov    (%rax),%ebp
mov    0x2e27(%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     11e6 <open_monitor+0x76>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1060 <bind@plt>
test   %rax,%rax
js     11ef <open_monitor+0x7f>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe13(%rip),%rdi
jmp    11f6 <open_monitor+0x86>
lea    0xe11(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

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