#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*/ AF_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int device_ifindex(const char *ifname) {
  int ret, sock, index;
  struct ifreq ifr;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0)
    return sock;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, ifname, strlen(ifname));
  ret = ioctl(sock, SIOCGIFINDEX, &ifr);
  if (!ret)
    index = ifr.ifr_ifindex;
  else
    index = -1;
  close(sock);
  return index;
}


x86 assembly (clang, -O0):
<device_ifindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    119d <device_ifindex+0x3d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <device_ifindex+0xb4>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11ff <device_ifindex+0x9f>
mov    -0x28(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1206 <device_ifindex+0xa6>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_ifindex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d6 <device_ifindex+0x76>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x4(%rsp),%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
neg    %eax
sbb    %ebp,%ebp
or     (%rsp),%ebp
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @device_ifindex(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
store i8* %0, i8** %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i32, i32* %5
store i32 %14, i32* %2
br label %36
15: ; preds = %1
%16 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 8)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load i8*, i8** %3
%20 = load i8*, i8** %3
%21 = call i32 @strlen(i8* %20)
%22 = call i32 @strncpy(i32 %18, i8* %19, i32 %21)
%23 = load i32, i32* %5
%24 = load i32, i32* @SIOCGIFINDEX
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.ifreq* %7)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %15
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%30 = load i32, i32* %29
store i32 %30, i32* %6
br label %32
31: ; preds = %15
store i32 -1, i32* %6
br label %32
32: ; preds = %31, %28
%33 = load i32, i32* %5
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %6
store i32 %35, i32* %2
br label %36
36: ; preds = %32, %13
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @device_ifindex(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %1
%9 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i32 @strlen(i8* %0)
%13 = call i32 @strncpy(i32 %11, i8* %0, i32 %12)
%14 = load i32, i32* @SIOCGIFINDEX
%15 = call i32 @ioctl(i32 %6, i32 %14, %struct.ifreq* %2)
%16 = icmp eq i32 %15, 0
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%18 = load i32, i32* %17
%19 = select i1 %16, i32 %18, i32 -1
%20 = call i32 @close(i32 %6)
br label %21
21: ; preds = %1, %8
%22 = phi i32 [ %19, %8 ], [ %6, %1 ]
ret i32 %22
}