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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(struct ifreq *);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
struct ifreq *malloc(int);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_IFACE_byindex(int index) {
  int ret, sd;
  struct ifreq *if_req;
  sd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sd < 0) {
    printf("\nError on socket");
    return -1;
  }
  if_req = malloc(sizeof(struct ifreq));
  memset(if_req, 0, sizeof(struct ifreq));
  if_req->ifr_ifindex = index;
  ret = ioctl(sd, SIOCGIFNAME, if_req);
  if (ret < 0) {
    printf("\nError on ioctl");
    return -1;
  }
  printf("\nInterface[%d]: %s\n", index, if_req->ifr_name);
  close(sd);
  free(if_req);
  return 0;
}


x86 assembly (clang, -O0):
<get_IFACE_byindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bb <get_IFACE_byindex+0x4b>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <get_IFACE_byindex+0xdf>
mov    $0x10,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    121e <get_IFACE_byindex+0xae>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <get_IFACE_byindex+0xdf>
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_IFACE_byindex>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11f9 <get_IFACE_byindex+0x89>
mov    %eax,%ebx
mov    $0x10,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    %r14d,0x0(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1060 <ioctl@plt>
test   %eax,%eax
js     1202 <get_IFACE_byindex+0x92>
mov    0x8(%rbp),%rdx
lea    0xe43(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1216 <get_IFACE_byindex+0xa6>
lea    0xe00(%rip),%rdi
jmp    1209 <get_IFACE_byindex+0x99>
lea    0xe08(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [17 x i8] c"\0AError on socket\00"
@SIOCGIFNAME = global i32 0
@.str.1 = [16 x i8] c"\0AError on ioctl\00"
@.str.2 = [20 x i8] c"\0AInterface[%d]: %s\0A\00"
define i32 @get_IFACE_byindex(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ifreq*
store i32 %0, i32* %3
%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 slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
14: ; preds = %1
%15 = call %struct.ifreq* @malloc(i32 16)
store %struct.ifreq* %15, %struct.ifreq** %6
%16 = load %struct.ifreq*, %struct.ifreq** %6
%17 = call i32 @memset(%struct.ifreq* %16, i32 0, i32 16)
%18 = load i32, i32* %3
%19 = load %struct.ifreq*, %struct.ifreq** %6
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %19, i32 0, i32 0
store i32 %18, i32* %20
%21 = load i32, i32* %5
%22 = load i32, i32* @SIOCGIFNAME
%23 = load %struct.ifreq*, %struct.ifreq** %6
%24 = call i32 @ioctl(i32 %21, i32 %22, %struct.ifreq* %23)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
29: ; preds = %14
%30 = load i32, i32* %3
%31 = load %struct.ifreq*, %struct.ifreq** %6
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %31, i32 0, i32 1
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8* %33)
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = load %struct.ifreq*, %struct.ifreq** %6
%38 = call i32 @free(%struct.ifreq* %37)
store i32 0, i32* %2
br label %39
39: ; preds = %29, %27, %12
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [17 x i8] c"\0AError on socket\00"
@SIOCGIFNAME = global i32 0
@.str.1 = [16 x i8] c"\0AError on ioctl\00"
@.str.2 = [20 x i8] c"\0AInterface[%d]: %s\0A\00"
define i32 @get_IFACE_byindex(i32 %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 %8
6: ; preds = %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %23
8: ; preds = %1
%9 = tail call %struct.ifreq* @malloc(i32 16)
%10 = tail call i32 @memset(%struct.ifreq* %9, i32 0, i32 16)
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i64 0, i32 0
store i32 %0, i32* %11
%12 = load i32, i32* @SIOCGIFNAME
%13 = tail call i32 @ioctl(i32 %4, i32 %12, %struct.ifreq* %9)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %23
17: ; preds = %8
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %9, i64 0, i32 1
%19 = load i8*, i8** %18
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %19)
%21 = tail call i32 @close(i32 %4)
%22 = tail call i32 @free(%struct.ifreq* %9)
br label %23
23: ; preds = %17, %15, %6
%24 = phi i32 [ -1, %6 ], [ -1, %15 ], [ 0, %17 ]
ret i32 %24
}