#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;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int iif_to_name(int iif, struct ifreq *ifr) {
  int n;
  int sk = socket(PF_INET, SOCK_DGRAM, 0);
  if (sk < 0)
    return -1;
  ifr->ifr_ifindex = iif;
  n = ioctl(sk, SIOCGIFNAME, ifr);
  close(sk);
  return n;
}


x86 assembly (clang, -O0):
<iif_to_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1171 <iif_to_name+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <iif_to_name+0x70>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iif_to_name>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
test   %eax,%eax
js     117c <iif_to_name+0x4c>
mov    %eax,%ebp
mov    %r14d,(%rbx)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
jmp    1181 <iif_to_name+0x51>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
define i32 @iif_to_name(i32 %0, %struct.ifreq* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.ifreq*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.ifreq* %1, %struct.ifreq** %5
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %25
14: ; preds = %2
%15 = load i32, i32* %4
%16 = load %struct.ifreq*, %struct.ifreq** %5
%17 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = load i32, i32* %7
%19 = load i32, i32* @SIOCGIFNAME
%20 = load %struct.ifreq*, %struct.ifreq** %5
%21 = call i32 @ioctl(i32 %18, i32 %19, %struct.ifreq* %20)
store i32 %21, i32* %6
%22 = load i32, i32* %7
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %6
store i32 %24, i32* %3
br label %25
25: ; preds = %14, %13
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.ifreq = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
define i32 @iif_to_name(i32 %0, %struct.ifreq* %1) {
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %1, i64 0, i32 0
store i32 %0, i32* %8
%9 = load i32, i32* @SIOCGIFNAME
%10 = tail call i32 @ioctl(i32 %5, i32 %9, %struct.ifreq* %1)
%11 = tail call i32 @close(i32 %5)
br label %12
12: ; preds = %2, %7
%13 = phi i32 [ %10, %7 ], [ -1, %2 ]
ret i32 %13
}