#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 ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int my_if_nametoindex(int sock_fd, const char *devname) {
  struct ifreq ifr;
  memset(&ifr, 0, sizeof(ifr));
  strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name));
  if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) == -1) {
    return -1;
  }
  return ifr.ifr_ifindex;
}


x86 assembly (clang, -O0):
<my_if_nametoindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%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),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    118a <my_if_nametoindex+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1190 <my_if_nametoindex+0x60>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_if_nametoindex>:
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
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
cmp    $0xffffffff,%eax
je     117e <my_if_nametoindex+0x4e>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@SIOCGIFINDEX = global i32 0
define i32 @my_if_nametoindex(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ifreq
store i32 %0, i32* %4
store i8* %1, i8** %5
%7 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%9 = load i32, i32* %8
%10 = load i8*, i8** %5
%11 = call i32 @strncpy(i32 %9, i8* %10, i32 4)
%12 = load i32, i32* %4
%13 = load i32, i32* @SIOCGIFINDEX
%14 = call i32 @ioctl(i32 %12, i32 %13, %struct.ifreq* %6)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 -1, i32* %3
br label %20
17: ; preds = %2
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %3
br label %20
20: ; preds = %17, %16
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@SIOCGIFINDEX = global i32 0
define i32 @my_if_nametoindex(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 @strncpy(i32 %7, i8* %1, i32 4)
%9 = load i32, i32* @SIOCGIFINDEX
%10 = call i32 @ioctl(i32 %0, i32 %9, %struct.ifreq* %3)
%11 = icmp eq i32 %10, -1
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = select i1 %11, i32 -1, i32 %13
ret i32 %14
}