#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*/ AF_INET;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ t01_log(int, char *, char const *);
int get_if_idx(const char *if_name) {
  struct ifreq ifr;
  int ret, sockfd;
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  memset((void *)&ifr, 0, sizeof(ifr));
  snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), if_name);
  ret = ioctl(sockfd, SIOCGIFINDEX, &ifr);
  if (ret < 0) {
    t01_log(3, "failed to get idx of if %s", if_name);
    exit(1);
  }
  ret = ifr.ifr_ifindex;
  close(sockfd);
  return ret;
}


x86 assembly (clang, -O0):
<get_if_idx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rdx
mov    $0x4,%esi
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fa <get_if_idx+0x8a>
mov    -0x8(%rbp),%rdx
mov    $0x3,%edi
lea    0xe15(%rip),%rsi
call   1070 <t01_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_if_idx>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0xc(%rsp),%edi
mov    $0x4,%esi
mov    %r14,%rdx
call   1030 <snprintf@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1050 <ioctl@plt>
test   %eax,%eax
js     11e8 <get_if_idx+0x78>
mov    0x8(%rsp),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe11(%rip),%rsi
mov    $0x3,%edi
mov    %r14,%rdx
call   1070 <t01_log@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [27 x i8] c"failed to get idx of if %s\00"
define i32 @get_if_idx(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.ifreq
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = bitcast %struct.ifreq* %3 to i8*
%10 = call i32 @memset(i8* %9, i32 0, i32 8)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 1
%12 = load i32, i32* %11
%13 = load i8*, i8** %2
%14 = call i32 @snprintf(i32 %12, i32 4, i8* %13)
%15 = load i32, i32* %5
%16 = load i32, i32* @SIOCGIFINDEX
%17 = call i32 @ioctl(i32 %15, i32 %16, %struct.ifreq* %3)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %1
%21 = load i8*, i8** %2
%22 = call i32 @t01_log(i32 3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %1
%25 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i32 0, i32 0
%26 = load i32, i32* %25
store i32 %26, i32* %4
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFINDEX = global i32 0
@.str = [27 x i8] c"failed to get idx of if %s\00"
define i32 @get_if_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 = call i32 @memset(i8* %3, i32 0, i32 8)
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 1
%9 = load i32, i32* %8
%10 = call i32 @snprintf(i32 %9, i32 4, i8* %0)
%11 = load i32, i32* @SIOCGIFINDEX
%12 = call i32 @ioctl(i32 %6, i32 %11, %struct.ifreq* %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @t01_log(i32 3, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @close(i32 %6)
ret i32 %19
}