#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 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,-0x14(%rbp)
cmp    $0x0,%eax
jl     121a <if_indextoname+0xca>
mov    -0x14(%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    -0x14(%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 %struct.ifreq
%7 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i8* null, i8** %7
%8 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%9 = load i32, i32* %3
%10 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %36
15: ; preds = %2
%16 = load i32, i32* %5
%17 = load i32, i32* @SIOCGIFNAME
%18 = call i64 @ioctl(i32 %16, i32 %17, %struct.ifreq* %6)
%19 = icmp sge i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%23 = load i32, i32* %22
%24 = load i32, i32* @IFNAMSIZ
%25 = call i8* @strncpy(i8* %21, i32 %23, i32 %24)
store i8* %25, i8** %7
br label %33
26: ; preds = %15
%27 = load i64, i64* @errno
%28 = load i64, i64* @ENODEV
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = load i64, i64* @ENXIO
store i64 %31, i64* @errno
br label %32
32: ; preds = %30, %26
br label %33
33: ; preds = %32, %20
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %2
%37 = load i8*, i8** %7
ret i8* %37
}

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
}