#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(6, fd);
  return r < 0 ? r : 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    11dd <if_nametoindex+0x9d>
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    $0x6,%edi
call   1050 <__syscall@plt>
cmpl   $0x0,-0x20(%rbp)
jge    11d1 <if_nametoindex+0x91>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11d7 <if_nametoindex+0x97>
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   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
mov    0x2e7d(%rip),%rax
mov    0x2e8e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11ad <if_nametoindex+0x6d>
mov    %eax,%ebx
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebp
mov    $0x6,%edi
mov    %ebx,%esi
call   1050 <__syscall@plt>
test   %ebp,%ebp
js     11b2 <if_nametoindex+0x72>
mov    0x8(%rsp),%ebp
jmp    11b2 <if_nametoindex+0x72>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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 %33
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 6, i32 %22)
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %14
%27 = load i32, i32* %6
br label %31
28: ; preds = %14
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%30 = load i32, i32* %29
br label %31
31: ; preds = %28, %26
%32 = phi i32 [ %27, %26 ], [ %30, %28 ]
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %13
%34 = load i32, i32* %2
ret i32 %34
}

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 6, 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 %13, i32 %17
br label %19
19: ; preds = %1, %10
%20 = phi i32 [ %18, %10 ], [ -1, %1 ]
ret i32 %20
}