#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 {
  scalar_t__ sll_ifindex;
  int /*<<< orphan*/ sll_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ll;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int /*<<< orphan*/ ETH_P_ALL;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SOCK_RAW;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ if_nametoindex(char const *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int monitor_init(const char *ifname) {
  struct sockaddr_ll ll;
  int monitor_sock;
  memset(&ll, 0, sizeof(ll));
  ll.sll_family = AF_PACKET;
  ll.sll_ifindex = if_nametoindex(ifname);
  if (ll.sll_ifindex == 0) {
    fprintf(stderr, "Monitor interface '%s' does not exist\n", ifname);
    return -1;
  }
  monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  if (monitor_sock < 0) {
    fprintf(stderr, "socket(PF_PACKET,SOCK_RAW): %s\n", strerror(errno));
    return -1;
  }
  if (bind(monitor_sock, (struct sockaddr *)&ll, sizeof(ll)) < 0) {
    fprintf(stderr, "bind(PACKET): %s\n", strerror(errno));
    close(monitor_sock);
    return -1;
  }
  return monitor_sock;
}


x86 assembly (clang, -O0):
<monitor_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <if_nametoindex@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e5 <monitor_init+0x65>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <monitor_init+0x13e>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dcc(%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    125d <monitor_init+0xdd>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xddb(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <monitor_init+0x13e>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12b8 <monitor_init+0x138>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xda8(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <monitor_init+0x13e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<monitor_init>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rbx,%rdi
call   1070 <if_nametoindex@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     1231 <monitor_init+0xb1>
mov    0x2e30(%rip),%rax
mov    (%rax),%ebx
mov    0x2e17(%rip),%rax
mov    (%rax),%ebp
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1246 <monitor_init+0xc6>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1273 <monitor_init+0xf3>
mov    0x2db9(%rip),%rax
mov    (%rax),%ebp
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    126e <monitor_init+0xee>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    %rbx,%rdx
jmp    1269 <monitor_init+0xe9>
mov    0x2d73(%rip),%rax
mov    (%rax),%ebx
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdc3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_ll = type { i64, i32 }
%struct.sockaddr = type { i32 }
@AF_PACKET = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Monitor interface '%s' does not exist\0A\00"
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_ALL = global i32 0
@.str.1 = [32 x i8] c"socket(PF_PACKET,SOCK_RAW): %s\0A\00"
@errno = global i32 0
@.str.2 = [18 x i8] c"bind(PACKET): %s\0A\00"
define i32 @monitor_init(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 16)
%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 i64 @if_nametoindex(i8* %0)
%8 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %2, i64 0, i32 0
store i64 %7, i64* %8
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %35
13: ; preds = %1
%14 = load i32, i32* @PF_PACKET
%15 = load i32, i32* @SOCK_RAW
%16 = load i32, i32* @ETH_P_ALL
%17 = call i32 @htons(i32 %16)
%18 = call i32 @socket(i32 %14, i32 %15, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %35
25: ; preds = %13
%26 = bitcast %struct.sockaddr_ll* %2 to %struct.sockaddr*
%27 = call i64 @bind(i32 %18, %struct.sockaddr* %26, i32 16)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = call i32 @close(i32 %18)
br label %35
35: ; preds = %25, %29, %20, %10
%36 = phi i32 [ -1, %10 ], [ -1, %20 ], [ -1, %29 ], [ %18, %25 ]
ret i32 %36
}