#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_UNIX;
int /*<<< orphan*/ SIOCGIFINDEX;
int SOCK_CLOEXEC;
int SOCK_DGRAM;
int /*<<< orphan*/ __syscall(int, int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
unsigned if_nametoindex(const char *name) {
  struct ifreq ifr;
  int fd, r;
  if ((fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
    return -1;
  strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
  r = ioctl(fd, SIOCGIFINDEX, &ifr);
  __syscall((0x40000000 + 3), fd);
  return r < 0 ? 0 : ifr.ifr_ifindex;
}


x86 assembly (clang, -O0):
<if_nametoindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1186 <if_nametoindex+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dc <if_nametoindex+0x9c>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    $0x40000003,%edi
call   1050 <__syscall@plt>
cmpl   $0x0,-0x20(%rbp)
jge    11d0 <if_nametoindex+0x90>
xor    %eax,%eax
mov    %eax,-0x24(%rbp)
jmp    11d6 <if_nametoindex+0x96>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_nametoindex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    0x2e7e(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11ad <if_nametoindex+0x6d>
mov    %eax,%ebp
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebx
mov    $0x40000003,%edi
mov    %ebp,%esi
call   1050 <__syscall@plt>
test   %ebx,%ebx
js     11b3 <if_nametoindex+0x73>
mov    (%rsp),%r15d
jmp    11b3 <if_nametoindex+0x73>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @if_nametoindex(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.ifreq
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_DGRAM
%9 = load i32, i32* @SOCK_CLOEXEC
%10 = or i32 %8, %9
%11 = call i32 @socket(i32 %7, i32 %10, i32 0)
store i32 %11, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %32
14: ; preds = %1
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load i8*, i8** %3
%18 = call i32 @strncpy(i32 %16, i8* %17, i32 4)
%19 = load i32, i32* %5
%20 = load i32, i32* @SIOCGIFINDEX
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.ifreq* %4)
store i32 %21, i32* %6
%22 = load i32, i32* %5
%23 = call i32 @__syscall(i32 1073741827, i32 %22)
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %14
br label %30
27: ; preds = %14
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%29 = load i32, i32* %28
br label %30
30: ; preds = %27, %26
%31 = phi i32 [ 0, %26 ], [ %29, %27 ]
store i32 %31, i32* %2
br label %32
32: ; preds = %30, %13
%33 = load i32, i32* %2
ret i32 %33
}

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