#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;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ ETH_P_IP;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ PF_PACKET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int get_interface_index(const char *devicename) {
  struct ifreq ifr;
  int s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP));
  if (s == -1) {
    return -1;
  }
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, devicename, IFNAMSIZ);
  if (ioctl(s, SIOCGIFINDEX, &ifr) == -1) {
    printf("SIOCGIFINDEX: fail ioctl errno=%d\n", errno);
    close(s);
    return -1;
  }
  close(s);
  return ifr.ifr_ifindex;
}


x86 assembly (clang, -O0):
<get_interface_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c8 <get_interface_index+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <get_interface_index+0xd2>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dea(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1234 <get_interface_index+0xc4>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    0xde5(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <get_interface_index+0xd2>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_interface_index>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e6f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e56(%rip),%rax
mov    (%rax),%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1040 <htons@plt>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1218 <get_interface_index+0xa8>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x4(%rsp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rsi
call   1030 <strncpy@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r15,%rdx
call   1070 <ioctl@plt>
cmp    $0xffffffff,%eax
je     11fc <get_interface_index+0x8c>
mov    %ebp,%edi
call   1080 <close@plt>
mov    (%rsp),%ebx
jmp    1218 <get_interface_index+0xa8>
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    0xdf4(%rip),%rdi
call   1050 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [35 x i8] c"SIOCGIFINDEX: fail ioctl errno=%d\0A\00"
@errno = global i32 0
define i32 @get_interface_index(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @PF_PACKET
%7 = load i32, i32* @SOCK_RAW
%8 = load i32, i32* @ETH_P_IP
%9 = call i32 @htons(i32 %8)
%10 = call i32 @socket(i32 %6, i32 %7, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %35
14: ; preds = %1
%15 = call i32 @memset(%struct.ifreq* %4, i32 0, i32 8)
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %3
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i32 %17, i8* %18, i32 %19)
%21 = load i32, i32* %5
%22 = load i32, i32* @SIOCGIFINDEX
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %14
%26 = load i32, i32* @errno
%27 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %5
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %2
br label %35
30: ; preds = %14
%31 = load i32, i32* %5
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%34 = load i32, i32* %33
store i32 %34, i32* %2
br label %35
35: ; preds = %30, %25, %13
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@PF_PACKET = global i32 0
@SOCK_RAW = global i32 0
@ETH_P_IP = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [35 x i8] c"SIOCGIFINDEX: fail ioctl errno=%d\0A\00"
@errno = global i32 0
define i32 @get_interface_index(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @PF_PACKET
%5 = load i32, i32* @SOCK_RAW
%6 = load i32, i32* @ETH_P_IP
%7 = tail call i32 @htons(i32 %6)
%8 = tail call i32 @socket(i32 %4, i32 %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %27, label %10
10: ; preds = %1
%11 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = load i32, i32* @IFNAMSIZ
%15 = call i32 @strncpy(i32 %13, i8* %0, i32 %14)
%16 = load i32, i32* @SIOCGIFINDEX
%17 = call i32 @ioctl(i32 %8, i32 %16, %struct.ifreq* %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %10
%20 = load i32, i32* @errno
%21 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = call i32 @close(i32 %8)
br label %27
23: ; preds = %10
%24 = call i32 @close(i32 %8)
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%26 = load i32, i32* %25
br label %27
27: ; preds = %1, %23, %19
%28 = phi i32 [ -1, %19 ], [ %26, %23 ], [ -1, %1 ]
ret i32 %28
}