#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 stat {
  int /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
scalar_t__ stat(char const *, struct stat *);
char *strdup(char *);
char *_sysfs_backing_file(const char *loop) {
  struct stat st;
  char buf[PATH_MAX];
  size_t len;
  int fd;
  if (stat(loop, &st) || !S_ISBLK(st.st_mode))
    return NULL;
  snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file",
           major(st.st_rdev), minor(st.st_rdev));
  fd = open(buf, O_RDONLY);
  if (fd < 0)
    return NULL;
  len = read(fd, buf, PATH_MAX);
  close(fd);
  if (len < 2)
    return NULL;
  buf[len - 1] = '\0';
  return strdup(buf);
}


x86 assembly (clang, -O0):
<_sysfs_backing_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    11f4 <_sysfs_backing_file+0x64>
mov    -0x14(%rbp),%edi
call   1090 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    1208 <_sysfs_backing_file+0x78>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ce <_sysfs_backing_file+0x13e>
mov    -0x48(%rbp),%rax
mov    %eax,-0x50(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <minor@plt>
mov    -0x50(%rbp),%esi
mov    -0x4c(%rbp),%ecx
mov    -0x40(%rbp),%rdi
mov    %eax,%r8d
lea    0xdca(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    126e <_sysfs_backing_file+0xde>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ce <_sysfs_backing_file+0x13e>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
cmpq   $0x2,-0x30(%rbp)
jae    12ae <_sysfs_backing_file+0x11e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ce <_sysfs_backing_file+0x13e>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
call   10b0 <strdup@plt>
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sysfs_backing_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsp,%r12
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     11df <_sysfs_backing_file+0x4f>
xor    %eax,%eax
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x2c(%rbp),%edi
call   1090 <S_ISBLK@plt>
test   %eax,%eax
je     11cb <_sysfs_backing_file+0x3b>
mov    -0x30(%rbp),%edi
call   1050 <major@plt>
mov    %eax,%r15d
mov    -0x30(%rbp),%edi
call   1030 <minor@plt>
lea    0xdfb(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %r15d,%ecx
mov    %eax,%r8d
call   1040 <snprintf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     11cb <_sysfs_backing_file+0x3b>
mov    %eax,%r14d
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
mov    %rax,%r15
mov    %r14d,%edi
call   1060 <close@plt>
cmp    $0x2,%r15
jb     11cb <_sysfs_backing_file+0x3b>
movb   $0x0,-0x1(%r15,%rbx,1)
mov    %rbx,%rdi
call   10b0 <strdup@plt>
jmp    11cd <_sysfs_backing_file+0x3d>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [39 x i8] c"/sys/dev/block/%d:%d/loop/backing_file\00"
@O_RDONLY = global i32 0
define i8* @_sysfs_backing_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = load i8*, i8** %3
%15 = call i64 @stat(i8* %14, %struct.stat* %4)
%16 = icmp ne i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %1
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @S_ISBLK(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %17, %1
store i8* null, i8** %2
store i32 1, i32* %9
br label %51
23: ; preds = %17
%24 = trunc i64 %11 to i32
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @major(i32 %26)
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @minor(i32 %29)
%31 = call i32 @snprintf(i8* %13, i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %30)
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %13, i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %23
store i8* null, i8** %2
store i32 1, i32* %9
br label %51
37: ; preds = %23
%38 = load i32, i32* %8
%39 = load i32, i32* @PATH_MAX
%40 = call i64 @read(i32 %38, i8* %13, i32 %39)
store i64 %40, i64* %7
%41 = load i32, i32* %8
%42 = call i32 @close(i32 %41)
%43 = load i64, i64* %7
%44 = icmp ult i64 %43, 2
br i1 %44, label %45, label %46
45: ; preds = %37
store i8* null, i8** %2
store i32 1, i32* %9
br label %51
46: ; preds = %37
%47 = load i64, i64* %7
%48 = sub i64 %47, 1
%49 = getelementptr inbounds i8, i8* %13, i64 %48
store i8 0, i8* %49
%50 = call i8* @strdup(i8* %13)
store i8* %50, i8** %2
store i32 1, i32* %9
br label %51
51: ; preds = %46, %45, %36, %22
%52 = load i8*, i8** %5
%53 = load i8*, i8** %2
ret i8* %53
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@PATH_MAX = global i32 0
@.str = [39 x i8] c"/sys/dev/block/%d:%d/loop/backing_file\00"
@O_RDONLY = global i32 0
define noalias i8* @_sysfs_backing_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = call i64 @stat(i8* %0, %struct.stat* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @S_ISBLK(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %34, label %15
15: ; preds = %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @major(i32 %17)
%19 = load i32, i32* %16
%20 = call i32 @minor(i32 %19)
%21 = call i32 @snprintf(i8* %7, i32 %4, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %7, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %34, label %25
25: ; preds = %15
%26 = load i32, i32* @PATH_MAX
%27 = call i64 @read(i32 %23, i8* %7, i32 %26)
%28 = call i32 @close(i32 %23)
%29 = icmp ult i64 %27, 2
br i1 %29, label %34, label %30
30: ; preds = %25
%31 = add i64 %27, -1
%32 = getelementptr inbounds i8, i8* %7, i64 %31
store i8 0, i8* %32
%33 = call i8* @strdup(i8* %7)
br label %34
34: ; preds = %25, %15, %1, %10, %30
%35 = phi i8* [ %33, %30 ], [ null, %10 ], [ null, %1 ], [ null, %15 ], [ null, %25 ]
ret i8* %35
}