#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_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int get_iface_index(const char *iface) {
  struct ifreq ifr;
  int sockfd;
  strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name) - 1);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    return -1;
  }
  if (ioctl(sockfd, SIOCGIFINDEX, &ifr) != 0) {
    ifr.ifr_ifindex = -1;
  }
  close(sockfd);
  return ifr.ifr_ifindex;
}


x86 assembly (clang, -O0):
<get_iface_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    118f <get_iface_index+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <get_iface_index+0x83>
mov    -0x1c(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11b5 <get_iface_index+0x75>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_iface_index>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
cmp    $0xffffffff,%eax
je     11a1 <get_iface_index+0x61>
mov    %eax,%ebx
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
je     1194 <get_iface_index+0x54>
movl   $0xffffffff,0x8(%rsp)
ff
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%eax
jmp    11a6 <get_iface_index+0x66>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @get_iface_index(i8* %0) {
%2 = alloca %struct.ifreq
%3 = bitcast %struct.ifreq* %2 to i8*
%4 = tail call i32 @strncpy(i32 undef, i8* %0, i32 3)
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %19, label %9
9: ; preds = %1
%10 = load i32, i32* @SIOCGIFINDEX
%11 = call i64 @ioctl(i32 %7, i32 %10, %struct.ifreq* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
store i32 -1, i32* %14
br label %15
15: ; preds = %13, %9
%16 = call i32 @close(i32 %7)
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%18 = load i32, i32* %17
br label %19
19: ; preds = %1, %15
%20 = phi i32 [ %18, %15 ], [ -1, %1 ]
ret i32 %20
}