#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 {
  char *ifr_name;
  int ifr_ifindex;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ msg_Err(int /*<<< orphan*/ *, char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int GetInterfaceIndex(const char *psz_name) {
  int i_fd;
  struct ifreq ifr;
  if ((i_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    msg_Err(NULL, "unable to open socket (%s)", strerror(errno));
    exit(EXIT_FAILURE);
  }
  strncpy(ifr.ifr_name, psz_name, IFNAMSIZ);
  ifr.ifr_name[IFNAMSIZ - 1] = '\0';
  if (ioctl(i_fd, SIOCGIFINDEX, &ifr) < 0) {
    msg_Err(NULL, "unable to get interface index (%s)", strerror(errno));
    exit(EXIT_FAILURE);
  }
  close(i_fd);
  return ifr.ifr_ifindex;
}


x86 assembly (clang, -O0):
<GetInterfaceIndex>:
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,-0xc(%rbp)
cmp    $0x0,%eax
jge    11cf <GetInterfaceIndex+0x5f>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%edi
lea    0xe44(%rip),%rsi
call   1040 <msg_Err@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2df2(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    0x2de0(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1249 <GetInterfaceIndex+0xd9>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
xor    %eax,%eax
mov    %eax,%edi
lea    0xde5(%rip),%rsi
call   1040 <msg_Err@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetInterfaceIndex>:
push   %rbx
mov    %rdi,%rbx
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
jns    119d <GetInterfaceIndex+0x4d>
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe7a(%rip),%rsi
xor    %edi,%edi
mov    %eax,%edx
call   1040 <msg_Err@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.ifreq = type { i8*, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c"unable to open socket (%s)\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.1 = [35 x i8] c"unable to get interface index (%s)\00"
define i32 @GetInterfaceIndex(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.ifreq
store i8* %0, i8** %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%17 = load i8*, i8** %16
%18 = load i8*, i8** %2
%19 = load i32, i32* @IFNAMSIZ
%20 = call i32 @strncpy(i8* %17, i8* %18, i32 %19)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = load i32, i32* @IFNAMSIZ
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 0, i8* %26
%27 = load i32, i32* %3
%28 = load i32, i32* @SIOCGIFINDEX
%29 = call i64 @ioctl(i32 %27, i32 %28, %struct.ifreq* %4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %15
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %15
%38 = load i32, i32* %3
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i32 0, i32 1
%41 = load i32, i32* %40
ret i32 %41
}

LLVM IR (-O2):
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [27 x i8] c"unable to open socket (%s)\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@IFNAMSIZ = global i32 0
@SIOCGIFINDEX = global i32 0
define i32 @GetInterfaceIndex(i8* %0) {
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_DGRAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @msg_Err(i32* null, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = load i32, i32* @IFNAMSIZ
%14 = tail call i32 @strncpy(i8* undef, i8* %0, i32 %13)
unreachable
}