#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 {
  char *ifr_name;
  int ifr_ifindex;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
unsigned int if_nametoindex(const char *blub) {
  struct ifreq ifr;
  int fd;
  int ret = 0;
  char *tmp;
  int len = sizeof(ifr.ifr_name);
  fd = socket(AF_INET6, 2, 0);
  if (fd < 0)
    fd = socket(AF_INET, 2, 0);
  for (tmp = ifr.ifr_name; len > 0; --len) {
    if ((*tmp++ = *blub++) == 0)
      break;
  }
  if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0)
    ret = ifr.ifr_ifindex;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<if_nametoindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1184 <if_nametoindex+0x54>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    11dc <if_nametoindex+0xac>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    -0x28(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
jne    11c9 <if_nametoindex+0x99>
jmp    11dc <if_nametoindex+0xac>
jmp    11ce <if_nametoindex+0x9e>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    118c <if_nametoindex+0x5c>
mov    -0x1c(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    1201 <if_nametoindex+0xd1>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_nametoindex>:
sub    $0x18,%rsp
mov    0x2ec5(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1030 <socket@plt>
mov    0x2eb8(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1030 <socket@plt>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @if_nametoindex(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifreq
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 8, i32* %7
%8 = load i32, i32* @AF_INET6
%9 = call i32 @socket(i32 %8, i32 2, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @AF_INET
%14 = call i32 @socket(i32 %13, i32 2, i32 0)
store i32 %14, i32* %4
br label %15
15: ; preds = %12, %1
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%17 = load i8*, i8** %16
store i8* %17, i8** %6
br label %18
18: ; preds = %31, %15
%19 = load i32, i32* %7
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %18
%22 = load i8*, i8** %2
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %2
%24 = load i8, i8* %22
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %6
store i8 %24, i8* %25
%27 = sext i8 %24 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %21
br label %34
30: ; preds = %21
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, -1
store i32 %33, i32* %7
br label %18
34: ; preds = %29, %18
%35 = load i32, i32* %4
%36 = load i32, i32* @SIOCGIFINDEX
%37 = call i64 @ioctl(i32 %35, i32 %36, %struct.ifreq* %3)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%41 = load i32, i32* %40
store i32 %41, i32* %5
br label %42
42: ; preds = %39, %34
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @if_nametoindex(i8* nocapture %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = load i32, i32* @AF_INET6
%5 = tail call i32 @socket(i32 %4, i32 2, i32 0)
%6 = icmp slt i32 %5, 0
%7 = load i32, i32* @AF_INET
%8 = tail call i32 @socket(i32 %7, i32 2, i32 0)
unreachable
}