#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 /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ __syscall(int, int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, 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, 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    0x2e92(%rip),%rax
mov    (%rax),%edi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1181 <if_indextoname+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <if_indextoname+0xa2>
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e37(%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    11c1 <if_indextoname+0x81>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    11da <if_indextoname+0x9a>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e11(%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    0x2e94(%rip),%rax
mov    (%rax),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1060 <socket@plt>
test   %eax,%eax
js     11b3 <if_indextoname+0x73>
mov    %eax,%ebp
mov    %r15d,(%rsp)
mov    0x2e4f(%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     11b1 <if_indextoname+0x71>
mov    0x4(%rsp),%esi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
mov    %rax,%rbx
jmp    11b3 <if_indextoname+0x73>
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
@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 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i8* null, i8** %3
br label %33
14: ; preds = %2
%15 = load i32, i32* %4
%16 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = load i32, i32* @SIOCGIFNAME
%19 = call i32 @ioctl(i32 %17, i32 %18, %struct.ifreq* %6)
store i32 %19, i32* %8
%20 = load i32, i32* %7
%21 = call i32 @__syscall(i32 6, i32 %20)
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %14
br label %31
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%28 = load i32, i32* %27
%29 = load i32, i32* @IF_NAMESIZE
%30 = call i8* @strncpy(i8* %26, i32 %28, i32 %29)
br label %31
31: ; preds = %25, %24
%32 = phi i8* [ null, %24 ], [ %30, %25 ]
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %13
%34 = load i8*, i8** %3
ret i8* %34
}

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