#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 */
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int asprintf(char **, char *, char const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
bool is_loop_file_deleted(const char *loopdev) {
  bool result = false;
  FILE *f = NULL;
  char *path = NULL;
  char *linebuf = NULL;
  const char *loop_num_str = loopdev + (sizeof("/dev/loop") - 1);
  if (asprintf(&path, "/sys/devices/virtual/block/loop%s/loop/backing_file",
               loop_num_str) == -1) {
    return false;
  }
  f = fopen(path, "r");
  if (f == NULL) {
    goto cleanup;
  }
  size_t linebuf_len = 0;
  ssize_t len = getline(&linebuf, &linebuf_len, f);
  if (len <= (sizeof(" (deleted)\n") - 1)) {
    goto cleanup;
  }
  result =
      !strcmp(" (deleted)\n", linebuf + len - (sizeof(" (deleted)\n") - 1));
cleanup:
  if (f != NULL) {
    fclose(f);
  }
  free(linebuf);
  free(path);
  return result;
}


x86 assembly (clang, -O0):
<is_loop_file_deleted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
add    $0x9,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    11c0 <is_loop_file_deleted+0x60>
movl   $0x0,-0x4(%rbp)
jmp    126a <is_loop_file_deleted+0x10a>
mov    -0x28(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e4 <is_loop_file_deleted+0x84>
jmp    123e <is_loop_file_deleted+0xde>
movq   $0x0,-0x40(%rbp)
mov    -0x20(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <getline@plt>
mov    %eax,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmp    $0xb,%rax
ja     1213 <is_loop_file_deleted+0xb3>
jmp    123e <is_loop_file_deleted+0xde>
mov    -0x30(%rbp),%rsi
movslq -0x44(%rbp),%rax
add    %rax,%rsi
add    $0xfffffffffffffff5,%rsi
lea    0xe0d(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1252 <is_loop_file_deleted+0xf2>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_loop_file_deleted>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
movq   $0x0,(%rsp)
lea    0x9(%rdi),%rdx
lea    0xe7e(%rip),%rsi
lea    0x8(%rsp),%rdi
call   1060 <asprintf@plt>
cmp    $0xffffffff,%eax
je     11fa <is_loop_file_deleted+0x9a>
mov    0x8(%rsp),%rdi
lea    0xe97(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11fe <is_loop_file_deleted+0x9e>
mov    %rax,%rbx
movq   $0x0,0x10(%rsp)
mov    %rsp,%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1080 <getline@plt>
xor    %ebp,%ebp
cmp    $0xc,%eax
jb     11ec <is_loop_file_deleted+0x8c>
cltq
mov    (%rsp),%rcx
lea    (%rcx,%rax,1),%rsi
add    $0xfffffffffffffff5,%rsi
lea    0xe57(%rip),%rdi
call   1050 <strcmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    (%rsp),%rdi
jmp    1202 <is_loop_file_deleted+0xa2>
xor    %ebp,%ebp
jmp    1211 <is_loop_file_deleted+0xb1>
xor    %edi,%edi
xor    %ebp,%ebp
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [52 x i8] c"/sys/devices/virtual/block/loop%s/loop/backing_file\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c" (deleted)\0A\00"
define i32 @is_loop_file_deleted(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i32* null, i32** %5
store i8* null, i8** %6
store i8* null, i8** %7
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 9
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %1
store i32 0, i32* %2
br label %52
17: ; preds = %1
%18 = load i8*, i8** %6
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %23
22: ; preds = %17
br label %40
23: ; preds = %17
store i64 0, i64* %9
%24 = load i32*, i32** %5
%25 = call i32 @getline(i8** %7, i64* %9, i32* %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = icmp ule i64 %27, 11
br i1 %28, label %29, label %30
29: ; preds = %23
br label %40
30: ; preds = %23
%31 = load i8*, i8** %7
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 -11
%36 = call i32 @strcmp(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = icmp ne i32 %36, 0
%38 = xor i1 %37, true
%39 = zext i1 %38 to i32
store i32 %39, i32* %4
br label %40
40: ; preds = %30, %29, %22
%41 = load i32*, i32** %5
%42 = icmp ne i32* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32*, i32** %5
%45 = call i32 @fclose(i32* %44)
br label %46
46: ; preds = %43, %40
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
%49 = load i8*, i8** %6
%50 = call i32 @free(i8* %49)
%51 = load i32, i32* %4
store i32 %51, i32* %2
br label %52
52: ; preds = %46, %16
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
@.str = [52 x i8] c"/sys/devices/virtual/block/loop%s/loop/backing_file\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c" (deleted)\0A\00"
define i32 @is_loop_file_deleted(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%6 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%7 = getelementptr inbounds i8, i8* %0, i64 9
%8 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %35, label %10
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %29, label %14
14: ; preds = %10
store i64 0, i64* %4
%15 = call i32 @getline(i8** %3, i64* %4, i32* %12)
%16 = icmp ult i32 %15, 12
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = sext i32 %15 to i64
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 %18
%21 = getelementptr inbounds i8, i8* %20, i64 -11
%22 = call i32 @strcmp(i8* dereferenceable(12) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = icmp eq i32 %22, 0
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %14, %17
%26 = phi i32 [ %24, %17 ], [ 0, %14 ]
%27 = call i32 @fclose(i32* %12)
%28 = load i8*, i8** %3
br label %29
29: ; preds = %10, %25
%30 = phi i8* [ %28, %25 ], [ null, %10 ]
%31 = phi i32 [ %26, %25 ], [ 0, %10 ]
%32 = call i32 @free(i8* %30)
%33 = load i8*, i8** %2
%34 = call i32 @free(i8* %33)
br label %35
35: ; preds = %1, %29
%36 = phi i32 [ %31, %29 ], [ 0, %1 ]
ret i32 %36
}