#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 if_nameindex {
  unsigned int if_index;
  scalar_t__ if_name;
};

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ if_freenameindex(struct if_nameindex *);
struct if_nameindex *if_nameindex();
char *strncpy(char *, scalar_t__, int /*<<< orphan*/);
char *if_indextoname(unsigned int ifindex, char *ifname) {
  struct if_nameindex *idx;
  struct if_nameindex *p;
  char *result = NULL;
  idx = if_nameindex();
  if (idx != NULL) {
    for (p = idx; p->if_index || p->if_name; ++p) {
      if (p->if_index == ifindex) {
        result = strncpy(ifname, p->if_name, IFNAMSIZ);
        break;
      }
    }
    if_freenameindex(idx);
  }
  return result;
}


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)
mov    $0x0,%al
call   1040 <if_nameindex@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e7 <if_indextoname+0xb7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    $0x1,%al
cmpl   $0x0,(%rcx)
mov    %al,-0x29(%rbp)
jne    1186 <if_indextoname+0x56>
mov    -0x20(%rbp),%rax
cmpq   $0x0,0x8(%rax)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1196 <if_indextoname+0x66>
jmp    11de <if_indextoname+0xae>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
jne    11c8 <if_indextoname+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x28(%rbp)
jmp    11de <if_indextoname+0xae>
jmp    11cd <if_indextoname+0x9d>
mov    -0x20(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
jmp    1165 <if_indextoname+0x35>
mov    -0x18(%rbp),%rdi
call   1050 <if_freenameindex@plt>
mov    -0x28(%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,%r15
mov    %edi,%ebp
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <if_nameindex@plt>
test   %rax,%rax
je     1196 <if_indextoname+0x66>
mov    %rax,%rbx
add    $0x8,%rax
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    -0x8(%rax),%ecx
test   %ecx,%ecx
jne    116d <if_indextoname+0x3d>
cmpq   $0x0,(%rax)
je     118e <if_indextoname+0x5e>
cmp    %ebp,%ecx
je     1177 <if_indextoname+0x47>
add    $0x10,%rax
jmp    1160 <if_indextoname+0x30>
mov    (%rax),%rsi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
call   1030 <strncpy@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <if_freenameindex@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.if_nameindex = type { i32, i64 }
@IFNAMSIZ = global i32 0
define i8* @if_indextoname(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.if_nameindex*
%6 = alloca %struct.if_nameindex*
%7 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i8* null, i8** %7
%8 = call %struct.if_nameindex* (...) @if_nameindex()
store %struct.if_nameindex* %8, %struct.if_nameindex** %5
%9 = load %struct.if_nameindex*, %struct.if_nameindex** %5
%10 = icmp ne %struct.if_nameindex* %9, null
br i1 %10, label %11, label %45
11: ; preds = %2
%12 = load %struct.if_nameindex*, %struct.if_nameindex** %5
store %struct.if_nameindex* %12, %struct.if_nameindex** %6
br label %13
13: ; preds = %39, %11
%14 = load %struct.if_nameindex*, %struct.if_nameindex** %6
%15 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = load %struct.if_nameindex*, %struct.if_nameindex** %6
%20 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %19, i32 0, i32 1
%21 = load i64, i64* %20
%22 = icmp ne i64 %21, 0
br label %23
23: ; preds = %18, %13
%24 = phi i1 [ true, %13 ], [ %22, %18 ]
br i1 %24, label %25, label %42
25: ; preds = %23
%26 = load %struct.if_nameindex*, %struct.if_nameindex** %6
%27 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* %3
%30 = icmp eq i32 %28, %29
br i1 %30, label %31, label %38
31: ; preds = %25
%32 = load i8*, i8** %4
%33 = load %struct.if_nameindex*, %struct.if_nameindex** %6
%34 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = load i32, i32* @IFNAMSIZ
%37 = call i8* @strncpy(i8* %32, i64 %35, i32 %36)
store i8* %37, i8** %7
br label %42
38: ; preds = %25
br label %39
39: ; preds = %38
%40 = load %struct.if_nameindex*, %struct.if_nameindex** %6
%41 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %40, i32 1
store %struct.if_nameindex* %41, %struct.if_nameindex** %6
br label %13
42: ; preds = %31, %23
%43 = load %struct.if_nameindex*, %struct.if_nameindex** %5
%44 = call i32 @if_freenameindex(%struct.if_nameindex* %43)
br label %45
45: ; preds = %42, %2
%46 = load i8*, i8** %7
ret i8* %46
}

LLVM IR (-O2):
%struct.if_nameindex = type { i32, i64 }
@IFNAMSIZ = global i32 0
define i8* @if_indextoname(i32 %0, i8* %1) {
%3 = tail call %struct.if_nameindex* (...) @if_nameindex()
%4 = icmp eq %struct.if_nameindex* %3, null
br i1 %4, label %26, label %5
5: ; preds = %2, %21
%6 = phi %struct.if_nameindex* [ %22, %21 ], [ %3, %2 ]
%7 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %6, i64 0, i32 0
%8 = load i32, i32* %7
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %6, i64 0, i32 1
%12 = load i64, i64* %11
%13 = icmp eq i64 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %5, %10
%15 = icmp eq i32 %8, %0
br i1 %15, label %16, label %21
16: ; preds = %14
%17 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %6, i64 0, i32 1
%18 = load i64, i64* %17
%19 = load i32, i32* @IFNAMSIZ
%20 = tail call i8* @strncpy(i8* %1, i64 %18, i32 %19)
br label %23
21: ; preds = %14
%22 = getelementptr inbounds %struct.if_nameindex, %struct.if_nameindex* %6, i64 1
br label %5
23: ; preds = %10, %16
%24 = phi i8* [ %20, %16 ], [ null, %10 ]
%25 = tail call i32 @if_freenameindex(%struct.if_nameindex* %3)
br label %26
26: ; preds = %23, %2
%27 = phi i8* [ %24, %23 ], [ null, %2 ]
ret i8* %27
}