#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 loop_info64 {
  char *lo_file_name;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ LOOP_GET_STATUS64;
int LO_NAME_SIZE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct loop_info64 *);
int open(char const *, int /*<<< orphan*/);
char *strdup(char *);
char *_ioctl_backing_file(const char *loop) {
  struct loop_info64 lo64 = {0};
  int loop_fd;
  loop_fd = open(loop, O_RDONLY);
  if (loop_fd < 0)
    return NULL;
  if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0) {
    close(loop_fd);
    return NULL;
  }
  lo64.lo_file_name[LO_NAME_SIZE - 2] = '*';
  lo64.lo_file_name[LO_NAME_SIZE - 1] = 0;
  close(loop_fd);
  return strdup((char *)lo64.lo_file_name);
}


x86 assembly (clang, -O0):
<_ioctl_backing_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1198 <_ioctl_backing_file+0x48>
movq   $0x0,-0x8(%rbp)
jmp    120f <_ioctl_backing_file+0xbf>
mov    -0x24(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11cc <_ioctl_backing_file+0x7c>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    120f <_ioctl_backing_file+0xbf>
mov    -0x20(%rbp),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x2a,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_ioctl_backing_file>:
push   %rbx
sub    $0x10,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11b0 <_ioctl_backing_file+0x70>
mov    %eax,%ebx
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
js     11a9 <_ioctl_backing_file+0x69>
mov    (%rsp),%rax
mov    0x2e65(%rip),%rcx
movslq (%rcx),%rdx
movb   $0x2a,-0x2(%rdx,%rax,1)
mov    (%rsp),%rax
movslq (%rcx),%rcx
movb   $0x0,-0x1(%rcx,%rax,1)
mov    %ebx,%edi
call   1040 <close@plt>
mov    (%rsp),%rdi
call   1060 <strdup@plt>
jmp    11b2 <_ioctl_backing_file+0x72>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.loop_info64 = type { i8*, i32 }
@O_RDONLY = global i32 0
@LOOP_GET_STATUS64 = global i32 0
@LO_NAME_SIZE = global i32 0
define i8* @_ioctl_backing_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.loop_info64
%5 = alloca i32
store i8* %0, i8** %3
%6 = bitcast %struct.loop_info64* %4 to i8*
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %39
13: ; preds = %1
%14 = load i32, i32* %5
%15 = load i32, i32* @LOOP_GET_STATUS64
%16 = call i64 @ioctl(i32 %14, i32 %15, %struct.loop_info64* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
store i8* null, i8** %2
br label %39
21: ; preds = %13
%22 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %4, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = load i32, i32* @LO_NAME_SIZE
%25 = sub nsw i32 %24, 2
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 42, i8* %27
%28 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %4, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = load i32, i32* @LO_NAME_SIZE
%31 = sub nsw i32 %30, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8 0, i8* %33
%34 = load i32, i32* %5
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %4, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = call i8* @strdup(i8* %37)
store i8* %38, i8** %2
br label %39
39: ; preds = %21, %18, %12
%40 = load i8*, i8** %2
ret i8* %40
}

LLVM IR (-O2):
%struct.loop_info64 = type { i8*, i32 }
@O_RDONLY = global i32 0
@LOOP_GET_STATUS64 = global i32 0
@LO_NAME_SIZE = global i32 0
define noalias i8* @_ioctl_backing_file(i8* nocapture %0) {
%2 = alloca %struct.loop_info64
%3 = bitcast %struct.loop_info64* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %28, label %7
7: ; preds = %1
%8 = load i32, i32* @LOOP_GET_STATUS64
%9 = call i64 @ioctl(i32 %5, i32 %8, %struct.loop_info64* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @close(i32 %5)
br label %28
13: ; preds = %7
%14 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %2, i64 0, i32 0
%15 = load i8*, i8** %14
%16 = load i32, i32* @LO_NAME_SIZE
%17 = add nsw i32 %16, -2
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
store i8 42, i8* %19
%20 = load i8*, i8** %14
%21 = load i32, i32* @LO_NAME_SIZE
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 0, i8* %24
%25 = call i32 @close(i32 %5)
%26 = load i8*, i8** %14
%27 = call i8* @strdup(i8* %26)
br label %28
28: ; preds = %1, %13, %11
%29 = phi i8* [ null, %11 ], [ %27, %13 ], [ null, %1 ]
ret i8* %29
}