#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 */

/* Variables and functions */
int /*<<< orphan*/ LOOP_CLR_FD;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
scalar_t__ ioctl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int detach_loop_device(char *loop_device) {
  int loop_device_fd = open(loop_device, O_RDWR);
  if (loop_device_fd < 0) {
    fprintf(stderr, "Cannot open loop device %s (error code %d)\n", loop_device,
            errno);
    return -1;
  }
  if (ioctl(loop_device_fd, LOOP_CLR_FD) != 0) {
    fprintf(stderr, "Cannot detach file from loop device %s (error code %d)\n",
            loop_device, errno);
    close(loop_device_fd);
    return -1;
  }
  close(loop_device_fd);
  return 0;
}


x86 assembly (clang, -O0):
<detach_loop_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1199 <detach_loop_device+0x59>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
lea    0xe78(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <detach_loop_device+0xb9>
mov    -0x14(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11ea <detach_loop_device+0xaa>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <detach_loop_device+0xb9>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach_loop_device>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     119e <detach_loop_device+0x5e>
mov    %eax,%ebp
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     11c6 <detach_loop_device+0x86>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
lea    0xea4(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
jmp    11cf <detach_loop_device+0x8f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
lea    0xe49(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11cf <detach_loop_device+0x8f>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"Cannot open loop device %s (error code %d)\0A\00"
@errno = global i32 0
@LOOP_CLR_FD = global i32 0
@.str.1 = [56 x i8] c"Cannot detach file from loop device %s (error code %d)\0A\00"
define i32 @detach_loop_device(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %3
%13 = load i32, i32* @errno
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13)
store i32 -1, i32* %2
br label %30
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @LOOP_CLR_FD
%18 = call i64 @ioctl(i32 %16, i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %3
%23 = load i32, i32* @errno
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i8* %22, i32 %23)
%25 = load i32, i32* %4
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %2
br label %30
27: ; preds = %15
%28 = load i32, i32* %4
%29 = call i32 @close(i32 %28)
store i32 0, i32* %2
br label %30
30: ; preds = %27, %20, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [44 x i8] c"Cannot open loop device %s (error code %d)\0A\00"
@errno = global i32 0
@LOOP_CLR_FD = global i32 0
@.str.1 = [56 x i8] c"Cannot detach file from loop device %s (error code %d)\0A\00"
define i32 @detach_loop_device(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %7)
br label %20
9: ; preds = %1
%10 = load i32, i32* @LOOP_CLR_FD
%11 = tail call i64 @ioctl(i32 %3, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %15)
%17 = tail call i32 @close(i32 %3)
br label %20
18: ; preds = %9
%19 = tail call i32 @close(i32 %3)
br label %20
20: ; preds = %18, %13, %5
%21 = phi i32 [ -1, %5 ], [ -1, %13 ], [ 0, %18 ]
ret i32 %21
}