#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;
};
typedef int /*<<< orphan*/ ifr;

/* Variables and functions */
int /*<<< orphan*/ SIOCGIFINDEX;
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int ifindex(int fd, const char *iface) {
  struct ifreq ifr;
  int ret_val;
  memset(&ifr, 0, sizeof(ifr));
  snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", iface);
  if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
    ret_val = errno;
    perror("ioctl");
    errno = ret_val;
    ret_val = -1;
  } else {
    ret_val = ifr.ifr_ifindex;
  }
  return (ret_val);
}


x86 assembly (clang, -O0):
<ifindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xe8e(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x4(%rbp),%edi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    11c6 <ifindex+0x86>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2e28(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x1c(%rbp)
jmp    11cc <ifindex+0x8c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifindex>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
lea    0xe94(%rip),%rdx
mov    $0x4,%esi
mov    %rbx,%rcx
call   1030 <snprintf@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
test   %rax,%rax
js     119e <ifindex+0x5e>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e3b(%rip),%rbx
mov    (%rbx),%ebp
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,(%rbx)
mov    $0xffffffff,%eax
jmp    1195 <ifindex+0x55>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [3 x i8] c"%s\00"
@SIOCGIFINDEX = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @ifindex(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.ifreq
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = call i32 @memset(%struct.ifreq* %5, i32 0, i32 8)
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%9 = load i32, i32* %8
%10 = load i8*, i8** %4
%11 = call i32 @snprintf(i32 %9, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* %3
%13 = load i32, i32* @SIOCGIFINDEX
%14 = call i64 @ioctl(i32 %12, i32 %13, %struct.ifreq* %5)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i32, i32* @errno
store i32 %17, i32* %6
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* %6
store i32 %19, i32* @errno
store i32 -1, i32* %6
br label %23
20: ; preds = %2
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%22 = load i32, i32* %21
store i32 %22, i32* %6
br label %23
23: ; preds = %20, %16
%24 = load i32, i32* %6
ret i32 %24
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [3 x i8] c"%s\00"
@SIOCGIFINDEX = global i32 0
@errno = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
define i32 @ifindex(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%6 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%7 = load i32, i32* %6
%8 = call i32 @snprintf(i32 %7, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%9 = load i32, i32* @SIOCGIFINDEX
%10 = call i64 @ioctl(i32 %0, i32 %9, %struct.ifreq* %3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 %13, i32* @errno
br label %18
15: ; preds = %2
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%17 = load i32, i32* %16
br label %18
18: ; preds = %15, %12
%19 = phi i32 [ -1, %12 ], [ %17, %15 ]
ret i32 %19
}