#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;
  scalar_t__ *ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char const *, int);
unsigned int if_nametoindex(const char *ifname) {
  int index;
  int ctl_sock;
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(struct ifreq));
  strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
  ifr.ifr_name[IFNAMSIZ - 1] = 0;
  index = 0;
  if ((ctl_sock = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
    if (ioctl(ctl_sock, SIOCGIFINDEX, &ifr) >= 0) {
      index = ifr.ifr_ifindex;
    }
    close(ctl_sock);
  }
  return index;
}


x86 assembly (clang, -O0):
<if_nametoindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x0,-0xc(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     11f6 <if_nametoindex+0xa6>
mov    -0x10(%rbp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jl     11ee <if_nametoindex+0x9e>
mov    -0x20(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_nametoindex>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbp
mov    %rsp,%rdi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x8(%rsp),%rdi
mov    0x2e57(%rip),%rbx
mov    (%rbx),%edx
mov    %rbp,%rsi
call   1030 <strncpy@plt>
mov    0x8(%rsp),%rax
movslq (%rbx),%rcx
movq   $0x0,-0x8(%rax,%rcx,8)
mov    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11d9 <if_nametoindex+0x89>
mov    %eax,%ebp
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
xor    %r14d,%r14d
test   %rax,%rax
js     11d2 <if_nametoindex+0x82>
mov    (%rsp),%r14d
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i64* }
@IFNAMSIZ = global i32 0
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @if_nametoindex(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.ifreq
store i8* %0, i8** %2
%6 = call i32 @memset(%struct.ifreq* %5, i32 0, i32 16)
%7 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%8 = load i64*, i64** %7
%9 = load i8*, i8** %2
%10 = load i32, i32* @IFNAMSIZ
%11 = call i32 @strncpy(i64* %8, i8* %9, i32 %10)
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%13 = load i64*, i64** %12
%14 = load i32, i32* @IFNAMSIZ
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i64, i64* %13, i64 %16
store i64 0, i64* %17
store i32 0, i32* %3
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_DGRAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %4
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %1
%23 = load i32, i32* %4
%24 = load i32, i32* @SIOCGIFINDEX
%25 = call i64 @ioctl(i32 %23, i32 %24, %struct.ifreq* %5)
%26 = icmp sge i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%29 = load i32, i32* %28
store i32 %29, i32* %3
br label %30
30: ; preds = %27, %22
%31 = load i32, i32* %4
%32 = call i32 @close(i32 %31)
br label %33
33: ; preds = %30, %1
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i64* }
@IFNAMSIZ = global i32 0
@AF_INET = 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 = call i32 @memset(%struct.ifreq* %2, i32 0, i32 16)
%5 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%6 = load i64*, i64** %5
%7 = load i32, i32* @IFNAMSIZ
%8 = call i32 @strncpy(i64* %6, i8* %0, i32 %7)
%9 = load i64*, i64** %5
%10 = load i32, i32* @IFNAMSIZ
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i64, i64* %9, i64 %12
store i64 0, i64* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %26
18: ; preds = %1
%19 = load i32, i32* @SIOCGIFINDEX
%20 = call i64 @ioctl(i32 %16, i32 %19, %struct.ifreq* %2)
%21 = icmp sgt i64 %20, -1
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = select i1 %21, i32 %23, i32 0
%25 = call i32 @close(i32 %16)
br label %26
26: ; preds = %18, %1
%27 = phi i32 [ %24, %18 ], [ 0, %1 ]
ret i32 %27
}