#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;
  char *ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int IFNAMSIZ;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
char *if_indextoname(unsigned int interface, char *blub) {
  struct ifreq ifr;
  int fd;
  fd = socket(AF_INET6, 2, 0);
  if (fd < 0)
    fd = socket(AF_INET, 2, 0);
  ifr.ifr_ifindex = interface;
  if (ioctl(fd, SIOCGIFNAME, &ifr) == 0) {
    int i;
    close(fd);
    for (i = 0; i < IFNAMSIZ - 1; i++)
      if (!(blub[i] = ifr.ifr_name[i]))
        return blub;
    blub[i] = 0;
    return blub;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<if_indextoname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1179 <if_indextoname+0x49>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jne    121b <if_indextoname+0xeb>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1202 <if_indextoname+0xd2>
mov    -0x20(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x18(%rbp),%rcx
movslq -0x30(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
cmp    $0x0,%al
jne    11ef <if_indextoname+0xbf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122b <if_indextoname+0xfb>
jmp    11f4 <if_indextoname+0xc4>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11ad <if_indextoname+0x7d>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122b <if_indextoname+0xfb>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-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
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1172 <if_indextoname+0x42>
mov    0x2e86(%rip),%rax
mov    (%rax),%edi
mov    $0x2,%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebp
mov    %r14d,0x8(%rsp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rbx,%rbx
je     11a0 <if_indextoname+0x70>
xor    %r15d,%r15d
jmp    11e5 <if_indextoname+0xb5>
mov    0x2e21(%rip),%rcx
cmpl   $0x2,(%rcx)
jl     11de <if_indextoname+0xae>
mov    0x10(%rsp),%rdx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%rdx,%rax,1),%ebx
mov    %bl,(%r15,%rax,1)
test   %bl,%bl
je     11e5 <if_indextoname+0xb5>
add    $0x1,%rax
movslq (%rcx),%rsi
add    $0xffffffffffffffff,%rsi
cmp    %rsi,%rax
jl     11c0 <if_indextoname+0x90>
jmp    11e0 <if_indextoname+0xb0>
xor    %eax,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SIOCGIFNAME = global i32 0
@IFNAMSIZ = 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_INET6
%10 = call i32 @socket(i32 %9, i32 2, i32 0)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = call i32 @socket(i32 %14, i32 2, i32 0)
store i32 %15, i32* %7
br label %16
16: ; preds = %13, %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 i64 @ioctl(i32 %19, i32 %20, %struct.ifreq* %6)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %55
23: ; preds = %16
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
store i32 0, i32* %8
br label %26
26: ; preds = %46, %23
%27 = load i32, i32* %8
%28 = load i32, i32* @IFNAMSIZ
%29 = sub nsw i32 %28, 1
%30 = icmp slt i32 %27, %29
br i1 %30, label %31, label %49
31: ; preds = %26
%32 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%33 = load i8*, i8** %32
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %5
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 %37, i8* %41
%42 = icmp ne i8 %37, 0
br i1 %42, label %45, label %43
43: ; preds = %31
%44 = load i8*, i8** %5
store i8* %44, i8** %3
br label %58
45: ; preds = %31
br label %46
46: ; preds = %45
%47 = load i32, i32* %8
%48 = add nsw i32 %47, 1
store i32 %48, i32* %8
br label %26
49: ; preds = %26
%50 = load i8*, i8** %5
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %5
store i8* %54, i8** %3
br label %58
55: ; preds = %16
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
store i8* null, i8** %3
br label %58
58: ; preds = %55, %49, %43
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@AF_INET6 = global i32 0
@AF_INET = global i32 0
@SIOCGIFNAME = global i32 0
@IFNAMSIZ = global i32 0
define i8* @if_indextoname(i32 %0, i8* writeonly %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @AF_INET6
%6 = tail call i32 @socket(i32 %5, i32 2, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @AF_INET
%10 = tail call i32 @socket(i32 %9, i32 2, i32 0)
br label %11
11: ; preds = %8, %2
%12 = phi i32 [ %10, %8 ], [ %6, %2 ]
%13 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %0, i32* %13
%14 = load i32, i32* @SIOCGIFNAME
%15 = call i64 @ioctl(i32 %12, i32 %14, %struct.ifreq* %3)
%16 = icmp eq i64 %15, 0
%17 = call i32 @close(i32 %12)
br i1 %16, label %18, label %41
18: ; preds = %11
%19 = load i32, i32* @IFNAMSIZ
%20 = icmp sgt i32 %19, 1
br i1 %20, label %21, label %38
21: ; preds = %18
%22 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%23 = load i8*, i8** %22
br label %24
24: ; preds = %21, %30
%25 = phi i64 [ 0, %21 ], [ %31, %30 ]
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 %27, i8* %28
%29 = icmp eq i8 %27, 0
br i1 %29, label %41, label %30
30: ; preds = %24
%31 = add nuw nsw i64 %25, 1
%32 = load i32, i32* @IFNAMSIZ
%33 = add nsw i32 %32, -1
%34 = sext i32 %33 to i64
%35 = icmp slt i64 %31, %34
br i1 %35, label %24, label %36
36: ; preds = %30
%37 = and i64 %31, 4294967295
br label %38
38: ; preds = %36, %18
%39 = phi i64 [ 0, %18 ], [ %37, %36 ]
%40 = getelementptr inbounds i8, i8* %1, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %24, %11, %38
%42 = phi i8* [ %1, %38 ], [ null, %11 ], [ %1, %24 ]
ret i8* %42
}