#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*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int ifname(int if_index, char *ifNameVar) {
  int fd;
  struct ifreq ifr;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    perror("socket");
    exit(1);
  }
  ifr.ifr_ifindex = if_index;
  if (ioctl(fd, SIOCGIFNAME, &ifr, sizeof(ifr))) {
    perror("ioctl");
    printf("ERROR ROUTING FIND INTERFACE\n");
  }
  strcpy(ifNameVar, ifr.ifr_name);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<ifname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11bb <ifname+0x4b>
lea    0xe54(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
mov    $0x8,%ecx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
je     11fd <ifname+0x8d>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ifname>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f8 <ifname+0x88>
mov    %eax,%ebp
mov    %ebx,0x8(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    $0x8,%ecx
call   1050 <ioctl@plt>
test   %rax,%rax
jne    11de <ifname+0x6e>
mov    0xc(%rsp),%esi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
jmp    11c0 <ifname+0x50>
lea    0xe01(%rip),%rdi
call   1070 <perror@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
@.str = [7 x i8] c"socket\00"
@SIOCGIFNAME = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@.str.2 = [30 x i8] c"ERROR ROUTING FIND INTERFACE\0A\00"
define i32 @ifname(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %3
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %5
%19 = load i32, i32* @SIOCGIFNAME
%20 = call i64 @ioctl(i32 %18, i32 %19, %struct.ifreq* %6, i32 8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %22, %15
%26 = load i8*, i8** %4
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @strcpy(i8* %26, i32 %28)
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
ret i32 0
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SIOCGIFNAME = global i32 0
@.str.1 = [6 x i8] c"ioctl\00"
@str = [29 x i8] c"ERROR ROUTING FIND INTERFACE\00"
define i32 @ifname(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %0, i32* %13
%14 = load i32, i32* @SIOCGIFNAME
%15 = call i64 @ioctl(i32 %7, i32 %14, %struct.ifreq* %3, i32 8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %17, %12
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 @strcpy(i8* %1, i32 %22)
%24 = call i32 @close(i32 %7)
ret i32 0
}