#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 ifreq {
  int ifr_ifindex;
  int /*<<< orphan*/ ifr_flags;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ IFF_PROMISC;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFFLAGS;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SIOCSIFFLAGS;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int getInterfaceIndex(int sock, char *deviceName) {
  struct ifreq ifr;
  strncpy(ifr.ifr_name, deviceName, IFNAMSIZ);
  if (ioctl(sock, SIOCGIFINDEX, &ifr) == -1) {
    perror("ETHERNET_LINUX: Failed to get interface index -> exit");
    exit(1);
  }
  int interfaceIndex = ifr.ifr_ifindex;
  if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
    perror("ETHERNET_LINUX: Problem getting device flags -> exit");
    exit(1);
  }
  ifr.ifr_flags |= IFF_PROMISC;
  if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
    perror("ETHERNET_LINUX: Setting device to promiscuous mode failed -> exit");
    exit(1);
  }
  return interfaceIndex;
}


x86 assembly (clang, -O0):
<getInterfaceIndex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x4(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1198 <getInterfaceIndex+0x58>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11d2 <getInterfaceIndex+0x92>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1215 <getInterfaceIndex+0xd5>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getInterfaceIndex>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11be <getInterfaceIndex+0x7e>
mov    0x8(%rsp),%ebp
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11c7 <getInterfaceIndex+0x87>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
or     %eax,0xc(%rsp)
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11d0 <getInterfaceIndex+0x90>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe3b(%rip),%rdi
jmp    11d7 <getInterfaceIndex+0x97>
lea    0xe68(%rip),%rdi
jmp    11d7 <getInterfaceIndex+0x97>
lea    0xe94(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [54 x i8] c"ETHERNET_LINUX: Failed to get interface index -> exit\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [53 x i8] c"ETHERNET_LINUX: Problem getting device flags -> exit\00"
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [66 x i8] c"ETHERNET_LINUX: Setting device to promiscuous mode failed -> exit\00"
define i32 @getInterfaceIndex(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.ifreq
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 2
%8 = load i32, i32* %7
%9 = load i8*, i8** %4
%10 = load i32, i32* @IFNAMSIZ
%11 = call i32 @strncpy(i32 %8, i8* %9, i32 %10)
%12 = load i32, i32* %3
%13 = load i32, i32* @SIOCGIFINDEX
%14 = call i32 @ioctl(i32 %12, i32 %13, %struct.ifreq* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %6
%22 = load i32, i32* %3
%23 = load i32, i32* @SIOCGIFFLAGS
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %5)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = load i32, i32* @IFF_PROMISC
%31 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%32 = load i32, i32* %31
%33 = or i32 %32, %30
store i32 %33, i32* %31
%34 = load i32, i32* %3
%35 = load i32, i32* @SIOCSIFFLAGS
%36 = call i32 @ioctl(i32 %34, i32 %35, %struct.ifreq* %5)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = call i32 @perror(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32, i32 }
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [54 x i8] c"ETHERNET_LINUX: Failed to get interface index -> exit\00"
@SIOCGIFFLAGS = global i32 0
@.str.1 = [53 x i8] c"ETHERNET_LINUX: Problem getting device flags -> exit\00"
@IFF_PROMISC = global i32 0
@SIOCSIFFLAGS = global i32 0
@.str.2 = [66 x i8] c"ETHERNET_LINUX: Setting device to promiscuous mode failed -> exit\00"
define i32 @getInterfaceIndex(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @IFNAMSIZ
%6 = tail call i32 @strncpy(i32 undef, i8* %1, i32 %5)
%7 = load i32, i32* @SIOCGIFINDEX
%8 = call i32 @ioctl(i32 %0, i32 %7, %struct.ifreq* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @SIOCGIFFLAGS
%17 = call i32 @ioctl(i32 %0, i32 %16, %struct.ifreq* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i32, i32* @IFF_PROMISC
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = load i32, i32* @SIOCSIFFLAGS
%28 = call i32 @ioctl(i32 %0, i32 %27, %struct.ifreq* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %22
ret i32 %15
}