#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*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFINDEX;
int af_socket(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlcpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int device_ifindex(const char *ifname) {
  int ret, sock, index;
  struct ifreq ifr;
  if (!strncmp("any", ifname, strlen("any")))
    return 0;
  sock = af_socket(AF_INET);
  memset(&ifr, 0, sizeof(ifr));
  strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
  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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe75(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe63(%rip),%rdi
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11b7 <device_ifindex+0x47>
movl   $0x0,-0x4(%rbp)
jmp    122f <device_ifindex+0xbf>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1040 <af_socket@plt>
mov    %eax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
call   1090 <strlcpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    121a <device_ifindex+0xaa>
mov    -0x28(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1221 <device_ifindex+0xb1>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_ifindex>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xe7e(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11f8 <device_ifindex+0x88>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1040 <af_socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0xc(%rsp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1090 <strlcpy@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
neg    %eax
sbb    %ebx,%ebx
or     0x8(%rsp),%ebx
mov    %ebp,%edi
call   1080 <close@plt>
jmp    11fa <device_ifindex+0x8a>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [4 x i8] c"any\00"
@AF_INET = global i32 0
@IFNAMSIZ = 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 i8*, i8** %3
%9 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %8, i32 %9)
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %1
store i32 0, i32* %2
br label %35
13: ; preds = %1
%14 = load i32, i32* @AF_INET
%15 = call i32 @af_socket(i32 %14)
store i32 %15, i32* %5
%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 i32, i32* @IFNAMSIZ
%21 = call i32 @strlcpy(i32 %18, i8* %19, i32 %20)
%22 = load i32, i32* %5
%23 = load i32, i32* @SIOCGIFINDEX
%24 = call i32 @ioctl(i32 %22, i32 %23, %struct.ifreq* %7)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %13
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
%29 = load i32, i32* %28
store i32 %29, i32* %6
br label %31
30: ; preds = %13
store i32 -1, i32* %6
br label %31
31: ; preds = %30, %27
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %6
store i32 %34, i32* %2
br label %35
35: ; preds = %31, %12
%36 = load i32, i32* %2
ret i32 %36
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [4 x i8] c"any\00"
@AF_INET = global i32 0
@IFNAMSIZ = 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 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @strncmp(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %22, label %7
7: ; preds = %1
%8 = load i32, i32* @AF_INET
%9 = tail call i32 @af_socket(i32 %8)
%10 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 8)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = load i32, i32* @IFNAMSIZ
%14 = call i32 @strlcpy(i32 %12, i8* %0, i32 %13)
%15 = load i32, i32* @SIOCGIFINDEX
%16 = call i32 @ioctl(i32 %9, i32 %15, %struct.ifreq* %2)
%17 = icmp eq i32 %16, 0
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = select i1 %17, i32 %19, i32 -1
%21 = call i32 @close(i32 %9)
br label %22
22: ; preds = %1, %7
%23 = phi i32 [ %20, %7 ], [ 0, %1 ]
ret i32 %23
}