#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_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int ifname_to_idx(char *ifname) {
  struct ifreq ifr;
  int fd = socket(AF_INET, SOCK_DGRAM, 0);
  int ifindex = -1;
  if (fd < 0)
    return (-1);
  strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
  if (ioctl(fd, SIOCGIFINDEX, (char *)&ifr) == 0)
    ifindex = ifr.ifr_ifindex;
  close(fd);
  return ifindex;
}


x86 assembly (clang, -O0):
<ifname_to_idx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%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)
movl   $0xffffffff,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1185 <ifname_to_idx+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <ifname_to_idx+0x89>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jne    11bb <ifname_to_idx+0x7b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifname_to_idx>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     119e <ifname_to_idx+0x5e>
mov    %eax,%ebx
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
neg    %rax
sbb    %ebp,%ebp
or     0x8(%rsp),%ebp
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11a3 <ifname_to_idx+0x63>
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_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @ifname_to_idx(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_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
store i32 -1, i32* %6
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %30
13: ; preds = %1
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%15 = load i32, i32* %14
%16 = load i8*, i8** %3
%17 = call i32 @strncpy(i32 %15, i8* %16, i32 4)
%18 = load i32, i32* %5
%19 = load i32, i32* @SIOCGIFINDEX
%20 = bitcast %struct.ifreq* %4 to i8*
%21 = call i64 @ioctl(i32 %18, i32 %19, i8* %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%25 = load i32, i32* %24
store i32 %25, i32* %6
br label %26
26: ; preds = %23, %13
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %6
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %12
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @ifname_to_idx(i8* %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)
%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 i64 @ioctl(i32 %6, i32 %10, i8* %3)
%12 = icmp eq i64 %11, 0
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = select i1 %12, i32 %14, i32 -1
%16 = call i32 @close(i32 %6)
br label %17
17: ; preds = %1, %8
%18 = phi i32 [ %15, %8 ], [ -1, %1 ]
ret i32 %18
}