#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;
  unsigned int ifr_ifindex;
};

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


x86 assembly (clang, -O0):
<if_nametoindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x8,-0x34(%rbp)
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x34(%rbp)
jle    11b7 <if_nametoindex+0x87>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%al
mov    -0x30(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %al,(%rcx)
movsbl %al,%eax
cmp    $0x0,%eax
jne    11a4 <if_nametoindex+0x74>
jmp    11b7 <if_nametoindex+0x87>
jmp    11a9 <if_nametoindex+0x79>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1167 <if_nametoindex+0x37>
mov    -0x24(%rbp),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    11e9 <if_nametoindex+0xb9>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f8 <if_nametoindex+0xc8>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

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

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

LLVM IR (-O2):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = 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_INET
%5 = load i32, i32* @SOCK_DGRAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
unreachable
}