#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_UNIX;
int /*<<< orphan*/ IF_NAMESIZE;
int /*<<< orphan*/ SIOCGIFNAME;
int SOCK_CLOEXEC;
int SOCK_DGRAM;
int /*<<< orphan*/ __syscall(int, int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
char *strncpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
char *if_indextoname(unsigned index, char *name) {
  struct ifreq ifr;
  int fd, r;
  if ((fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
    return 0;
  ifr.ifr_ifindex = index;
  r = ioctl(fd, SIOCGIFNAME, &ifr);
  __syscall(6, fd);
  return r < 0 ? 0 : strncpy(name, ifr.ifr_name, IF_NAMESIZE);
}


x86 assembly (clang, -O0):
<if_indextoname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    118a <if_indextoname+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    11eb <if_indextoname+0xab>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
mov    $0x6,%edi
call   1050 <__syscall@plt>
cmpl   $0x0,-0x28(%rbp)
jge    11ca <if_indextoname+0x8a>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    11e3 <if_indextoname+0xa3>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<if_indextoname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rax
mov    0x2e8c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11bc <if_indextoname+0x7c>
mov    %eax,%ebp
mov    %r15d,(%rsp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebx
mov    $0x6,%edi
mov    %ebp,%esi
call   1050 <__syscall@plt>
test   %ebx,%ebx
js     11ba <if_indextoname+0x7a>
mov    0x4(%rsp),%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %rax,%rbx
jmp    11bc <if_indextoname+0x7c>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@SIOCGIFNAME = global i32 0
@IF_NAMESIZE = global i32 0
define i8* @if_indextoname(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_DGRAM
%11 = load i32, i32* @SOCK_CLOEXEC
%12 = or i32 %10, %11
%13 = call i32 @socket(i32 %9, i32 %12, i32 0)
store i32 %13, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %35
16: ; preds = %2
%17 = load i32, i32* %4
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %7
%20 = load i32, i32* @SIOCGIFNAME
%21 = call i32 @ioctl(i32 %19, i32 %20, %struct.ifreq* %6)
store i32 %21, i32* %8
%22 = load i32, i32* %7
%23 = call i32 @__syscall(i32 6, i32 %22)
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %16
br label %33
27: ; preds = %16
%28 = load i8*, i8** %5
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load i32, i32* @IF_NAMESIZE
%32 = call i8* @strncpy(i8* %28, i32 %30, i32 %31)
br label %33
33: ; preds = %27, %26
%34 = phi i8* [ null, %26 ], [ %32, %27 ]
store i8* %34, i8** %3
br label %35
35: ; preds = %33, %15
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@SOCK_CLOEXEC = global i32 0
@SIOCGIFNAME = global i32 0
@IF_NAMESIZE = global i32 0
define i8* @if_indextoname(i32 %0, i8* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_DGRAM
%7 = load i32, i32* @SOCK_CLOEXEC
%8 = or i32 %7, %6
%9 = tail call i32 @socket(i32 %5, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %2
%12 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %0, i32* %12
%13 = load i32, i32* @SIOCGIFNAME
%14 = call i32 @ioctl(i32 %9, i32 %13, %struct.ifreq* %3)
%15 = call i32 @__syscall(i32 6, i32 %9)
%16 = icmp slt i32 %14, 0
br i1 %16, label %22, label %17
17: ; preds = %11
%18 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%19 = load i32, i32* %18
%20 = load i32, i32* @IF_NAMESIZE
%21 = call i8* @strncpy(i8* %1, i32 %19, i32 %20)
br label %22
22: ; preds = %17, %11, %2
%23 = phi i8* [ null, %2 ], [ %21, %17 ], [ null, %11 ]
ret i8* %23
}