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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *do_ioctl_get_ifname(int idx) {
  static struct ifreq ifr;
  int fd;
  int err;
  ifr.ifr_ifindex = idx;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  err = ioctl(fd, SIOCGIFNAME, &ifr);
  if (err) {
    perror("ioctl");
    return NULL;
  }
  close(fd);
  return ifr.ifr_name;
}


x86 assembly (clang, -O0):
<do_ioctl_get_ifname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,0x2ef4(%rip)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0x2ec5(%rip),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ae <do_ioctl_get_ifname+0x6e>
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <do_ioctl_get_ifname+0x81>
mov    -0x10(%rbp),%edi
call   1040 <close@plt>
mov    0x2e93(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_ioctl_get_ifname>:
push   %r14
push   %rbx
push   %rax
mov    %edi,0x2efe(%rip)
lea    0x2ef7(%rip),%r14
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <socket@plt>
mov    %eax,%ebx
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
test   %eax,%eax
jne    1199 <do_ioctl_get_ifname+0x59>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2ebf(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe60(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    1191 <do_ioctl_get_ifname+0x51>

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@do_ioctl_get_ifname.ifr = internal global %struct.ifreq zeroinitializer
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i8* @do_ioctl_get_ifname(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
store i32 %6, i32* getelementptr inbounds (%struct.ifreq, %struct.ifreq* @do_ioctl_get_ifname.ifr, i32 0, i32 0)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = load i32, i32* @SIOCGIFNAME
%12 = call i32 @ioctl(i32 %10, i32 %11, %struct.ifreq* @do_ioctl_get_ifname.ifr)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %21
17: ; preds = %1
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
%20 = load i8*, i8** getelementptr inbounds (%struct.ifreq, %struct.ifreq* @do_ioctl_get_ifname.ifr, i32 0, i32 1)
store i8* %20, i8** %2
br label %21
21: ; preds = %17, %15
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@do_ioctl_get_ifname.ifr = internal global %struct.ifreq zeroinitializer
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@.str = [6 x i8] c"ioctl\00"
define i8* @do_ioctl_get_ifname(i32 %0) {
store i32 %0, i32* getelementptr inbounds (%struct.ifreq, %struct.ifreq* @do_ioctl_get_ifname.ifr, i64 0, i32 0)
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_DGRAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = load i32, i32* @SIOCGIFNAME
%6 = tail call i32 @ioctl(i32 %4, i32 %5, %struct.ifreq* @do_ioctl_get_ifname.ifr)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %13
10: ; preds = %1
%11 = tail call i32 @close(i32 %4)
%12 = load i8*, i8** getelementptr inbounds (%struct.ifreq, %struct.ifreq* @do_ioctl_get_ifname.ifr, i64 0, i32 1)
br label %13
13: ; preds = %10, %8
%14 = phi i8* [ null, %8 ], [ %12, %10 ]
ret i8* %14
}