#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*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ ifr_name;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ sll_protocol;
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ sll_ifindex;
  int /*<<< orphan*/ sll_family;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ ll;
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ AF_PACKET;
int ETH_P_ALL;
int IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ assert(int);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int const);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct sockaddr_ll *);
int /*<<< orphan*/ memset(struct sockaddr_ll *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int openSocket(const char device[IFNAMSIZ]) {
  struct ifreq ifr;
  struct sockaddr_ll ll;
  const int protocol = ETH_P_ALL;
  int sock = -1;
  assert(sizeof(ifr.ifr_name) == IFNAMSIZ);
  sock = socket(PF_PACKET, SOCK_RAW, htons(protocol));
  if (sock < 0) {
    perror("socket failed (do you have root priviledges?)");
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
    perror("ioctl[SIOCGIFINDEX]");
    close(sock);
    return -1;
  }
  memset(&ll, 0, sizeof(ll));
  ll.sll_family = AF_PACKET;
  ll.sll_ifindex = ifr.ifr_ifindex;
  ll.sll_protocol = htons(protocol);
  if (bind(sock, (struct sockaddr *)&ll, sizeof(ll)) < 0) {
    perror("bind[AF_PACKET]");
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<openSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
mov    0x2e0a(%rip),%rax
movslq (%rax),%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <htons@plt>
mov    -0x50(%rbp),%edi
mov    -0x4c(%rbp),%esi
mov    %eax,%edx
call   10b0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    1220 <openSocket+0x90>
lea    0xdf1(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <openSocket+0x15e>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1280 <openSocket+0xf0>
lea    0xdc7(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <openSocket+0x15e>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12e8 <openSocket+0x158>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x48(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <openSocket+0x15e>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%r14
mov    0x2e2c(%rip),%rax
mov    (%rax),%r15d
mov    0x2e12(%rip),%rax
xor    %edi,%edi
cmpl   $0x4,(%rax)
sete   %dil
call   1080 <assert@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e14(%rip),%rax
mov    (%rax),%ebp
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     127e <openSocket+0xee>
mov    %eax,%ebx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x30(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %rax,%rax
js     128c <openSocket+0xfc>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x24(%rsp),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x14,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     1295 <openSocket+0x105>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7b(%rip),%rdi
call   10a0 <perror@plt>
jmp    12a8 <openSocket+0x118>
lea    0xd9b(%rip),%rdi
jmp    129c <openSocket+0x10c>
lea    0xda6(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
jmp    1271 <openSocket+0xe1>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@IFNAMSIZ = global i32 0
@ETH_P_ALL = global i32 0
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [46 x i8] c"socket failed (do you have root priviledges?)\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [20 x i8] c"ioctl[SIOCGIFINDEX]\00"
@AF_PACKET = global i32 0
@.str.2 = [16 x i8] c"bind[AF_PACKET]\00"
define i32 @openSocket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca %struct.sockaddr_ll
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @IFNAMSIZ
%9 = zext i32 %8 to i64
%10 = load i32, i32* @ETH_P_ALL
store i32 %10, i32* %6
store i32 -1, i32* %7
%11 = load i32, i32* @IFNAMSIZ
%12 = sext i32 %11 to i64
%13 = icmp eq i64 4, %12
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* @PF_PACKET
%17 = load i32, i32* @SOCK_RAW
%18 = load i32, i32* %6
%19 = call i32 @htons(i32 %18)
%20 = call i32 @socket(i32 %16, i32 %17, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %1
%24 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %61
25: ; preds = %1
%26 = bitcast %struct.ifreq* %4 to %struct.sockaddr_ll*
%27 = call i32 @memset(%struct.sockaddr_ll* %26, i32 0, i32 20)
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 4
%29 = load i32, i32* %28
%30 = load i8*, i8** %3
%31 = call i32 @strncpy(i32 %29, i8* %30, i32 4)
%32 = load i32, i32* %7
%33 = load i32, i32* @SIOCGIFINDEX
%34 = bitcast %struct.ifreq* %4 to %struct.sockaddr_ll*
%35 = call i64 @ioctl(i32 %32, i32 %33, %struct.sockaddr_ll* %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %25
%38 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %7
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %2
br label %61
41: ; preds = %25
%42 = call i32 @memset(%struct.sockaddr_ll* %5, i32 0, i32 20)
%43 = load i32, i32* @AF_PACKET
%44 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 3
store i32 %43, i32* %44
%45 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 2
store i32 %46, i32* %47
%48 = load i32, i32* %6
%49 = call i32 @htons(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %5, i32 0, i32 0
store i32 %49, i32* %50
%51 = load i32, i32* %7
%52 = bitcast %struct.sockaddr_ll* %5 to %struct.sockaddr*
%53 = call i64 @bind(i32 %51, %struct.sockaddr* %52, i32 20)
%54 = icmp slt i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %41
%56 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
store i32 -1, i32* %2
br label %61
59: ; preds = %41
%60 = load i32, i32* %7
store i32 %60, i32* %2
br label %61
61: ; preds = %59, %55, %37, %23
%62 = load i32, i32* %2
ret i32 %62
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32, i32, i32 }
%struct.sockaddr_ll = type { i32, i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@IFNAMSIZ = global i32 0
@ETH_P_ALL = global i32 0
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@.str = [46 x i8] c"socket failed (do you have root priviledges?)\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [20 x i8] c"ioctl[SIOCGIFINDEX]\00"
@AF_PACKET = global i32 0
@.str.2 = [16 x i8] c"bind[AF_PACKET]\00"
define i32 @openSocket(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* @ETH_P_ALL
%7 = load i32, i32* @IFNAMSIZ
%8 = icmp eq i32 %7, 4
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* @PF_PACKET
%12 = load i32, i32* @SOCK_RAW
%13 = tail call i32 @htons(i32 %6)
%14 = tail call i32 @socket(i32 %11, i32 %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = tail call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %45
18: ; preds = %1
%19 = bitcast %struct.ifreq* %2 to %struct.sockaddr_ll*
%20 = call i32 @memset(%struct.sockaddr_ll* %19, i32 0, i32 20)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 4
%22 = load i32, i32* %21
%23 = call i32 @strncpy(i32 %22, i8* %0, i32 4)
%24 = load i32, i32* @SIOCGIFINDEX
%25 = call i64 @ioctl(i32 %14, i32 %24, %struct.sockaddr_ll* %19)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @close(i32 %14)
br label %45
30: ; preds = %18
%31 = call i32 @memset(%struct.sockaddr_ll* %3, i32 0, i32 20)
%32 = load i32, i32* @AF_PACKET
%33 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 3
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 2
store i32 %35, i32* %36
%37 = call i32 @htons(i32 %6)
%38 = getelementptr inbounds %struct.sockaddr_ll, %struct.sockaddr_ll* %3, i64 0, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_ll* %3 to %struct.sockaddr*
%40 = call i64 @bind(i32 %14, %struct.sockaddr* %39, i32 20)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %30
%43 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @close(i32 %14)
br label %45
45: ; preds = %30, %42, %27, %16
%46 = phi i32 [ -1, %16 ], [ -1, %27 ], [ -1, %42 ], [ %14, %30 ]
ret i32 %46
}