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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ENODEV;
scalar_t__ ENXIO;
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strncpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
char *if_indextoname(unsigned ifindex, char *ifname) {
  int index;
  int ctl_sock;
  struct ifreq ifr;
  char *ret = NULL;
  memset(&ifr, 0, sizeof(struct ifreq));
  ifr.ifr_ifindex = ifindex;
  if ((ctl_sock = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
    if (ioctl(ctl_sock, SIOCGIFNAME, &ifr) >= 0) {
      ret = strncpy(ifname, ifr.ifr_name, IFNAMSIZ);
    } else {
      if (errno == ENODEV)
        errno = ENXIO;
    }
    close(ctl_sock);
  }
  return ret;
}


x86 assembly (clang, -O0):
<if_indextoname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     121a <if_indextoname+0xca>
mov    -0x18(%rbp),%edi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jl     11df <if_indextoname+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
jmp    1212 <if_indextoname+0xc2>
mov    0x2dda(%rip),%rax
mov    (%rax),%rax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%rax
jne    120d <if_indextoname+0xbd>
mov    0x2db8(%rip),%rax
mov    (%rax),%rcx
mov    0x2db6(%rip),%rax
mov    %rcx,(%rax)
jmp    1212 <if_indextoname+0xc2>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_indextoname>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0x8(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %ebx,0x8(%rsp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11f6 <if_indextoname+0xa6>
mov    %eax,%ebx
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     11c9 <if_indextoname+0x79>
mov    0xc(%rsp),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %rax,%r15
jmp    11ef <if_indextoname+0x9f>
mov    0x2df0(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd6(%rip),%rdx
cmp    (%rdx),%rcx
jne    11ec <if_indextoname+0x9c>
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,(%rax)
xor    %r15d,%r15d
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@IFNAMSIZ = global i32 0
@errno = global i64 0
@ENODEV = global i64 0
@ENXIO = global i64 0
define i8* @if_indextoname(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ifreq
%8 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i8* null, i8** %8
%9 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 8)
%10 = load i32, i32* %3
%11 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %2
%17 = load i32, i32* %6
%18 = load i32, i32* @SIOCGIFNAME
%19 = call i64 @ioctl(i32 %17, i32 %18, %struct.ifreq* %7)
%20 = icmp sge i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i32, i32* @IFNAMSIZ
%26 = call i8* @strncpy(i8* %22, i32 %24, i32 %25)
store i8* %26, i8** %8
br label %34
27: ; preds = %16
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENODEV
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i64, i64* @ENXIO
store i64 %32, i64* @errno
br label %33
33: ; preds = %31, %27
br label %34
34: ; preds = %33, %21
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %2
%38 = load i8*, i8** %8
ret i8* %38
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@IFNAMSIZ = global i32 0
@errno = global i64 0
@ENODEV = global i64 0
@ENXIO = global i64 0
define i8* @if_indextoname(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%6 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %0, i32* %6
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %29
11: ; preds = %2
%12 = load i32, i32* @SIOCGIFNAME
%13 = call i64 @ioctl(i32 %9, i32 %12, %struct.ifreq* %3)
%14 = icmp sgt i64 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%17 = load i32, i32* %16
%18 = load i32, i32* @IFNAMSIZ
%19 = call i8* @strncpy(i8* %1, i32 %17, i32 %18)
br label %26
20: ; preds = %11
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENODEV
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = load i64, i64* @ENXIO
store i64 %25, i64* @errno
br label %26
26: ; preds = %20, %24, %15
%27 = phi i8* [ %19, %15 ], [ null, %24 ], [ null, %20 ]
%28 = call i32 @close(i32 %9)
br label %29
29: ; preds = %26, %2
%30 = phi i8* [ %27, %26 ], [ null, %2 ]
ret i8* %30
}