#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_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int if_getindex(const char *nic) {
  struct ifreq interface;
  strncpy(interface.ifr_name, nic, sizeof(interface.ifr_name));
  int sockd;
  if ((sockd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Failed to create socket");
    exit(1);
  }
  int nic_index;
  if (ioctl(sockd, SIOCGIFINDEX, &interface) == -1) {
    perror("Failed to obtain Index of interface");
    exit(1);
  }
  return interface.ifr_ifindex;
}


x86 assembly (clang, -O0):
<if_getindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a8 <if_getindex+0x58>
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    11dc <if_getindex+0x8c>
lea    0xe4b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_getindex>:
push   %rax
mov    %rdi,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     1199 <if_getindex+0x49>
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11a2 <if_getindex+0x52>
mov    (%rsp),%eax
pop    %rcx
ret
lea    0xe60(%rip),%rdi
jmp    11a9 <if_getindex+0x59>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [36 x i8] c"Failed to obtain Index of interface\00"
define i32 @if_getindex(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifreq
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%7 = load i32, i32* %6
%8 = load i8*, i8** %2
%9 = call i32 @strncpy(i32 %7, i8* %8, i32 4)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = load i32, i32* %4
%19 = load i32, i32* @SIOCGIFINDEX
%20 = call i32 @ioctl(i32 %18, i32 %19, %struct.ifreq* %3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %17
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%27 = load i32, i32* %26
ret i32 %27
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Failed to create socket\00"
@SIOCGIFINDEX = global i32 0
@.str.1 = [36 x i8] c"Failed to obtain Index of interface\00"
define i32 @if_getindex(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i32, i32* @SIOCGIFINDEX
%14 = call i32 @ioctl(i32 %7, i32 %13, %struct.ifreq* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%21 = load i32, i32* %20
ret i32 %21
}