#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 /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ __syscall(int, int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, 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, 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    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    117d <if_nametoindex+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <if_nametoindex+0x94>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e30(%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    11c8 <if_nametoindex+0x88>
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11ce <if_nametoindex+0x8e>
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   %rbx
push   %rax
mov    %rdi,%rbp
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     119c <if_nametoindex+0x5c>
mov    %eax,%ebx
mov    %rbp,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %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     11a1 <if_nametoindex+0x61>
mov    (%rsp),%ebp
jmp    11a1 <if_nametoindex+0x61>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = 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 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = tail call i32 @strncpy(i32 undef, i8* %0, i32 4)
%10 = load i32, i32* @SIOCGIFINDEX
%11 = call i32 @ioctl(i32 %6, i32 %10, %struct.ifreq* %2)
%12 = call i32 @__syscall(i32 6, i32 %6)
%13 = icmp slt i32 %11, 0
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = select i1 %13, i32 %11, i32 %15
br label %17
17: ; preds = %1, %8
%18 = phi i32 [ %16, %8 ], [ -1, %1 ]
ret i32 %18
}