#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_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/);
int /*<<< orphan*/ kmsg(char *, char const *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void detach_loop(const char *src) {
  int fd = open(src, O_RDONLY);
  if (fd < 0) {
    kmsg("* unable to open loop device %s: %s", src, strerror(errno));
  } else {
    if (ioctl(fd, LOOP_CLR_FD) < 0) {
      kmsg("* unable to disassociate loop device %s: %s", src, strerror(errno));
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<detach_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a8 <detach_loop+0x58>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%rsi
mov    %eax,%edx
lea    0xe62(%rip),%rdi
call   1060 <kmsg@plt>
jmp    11f3 <detach_loop+0xa3>
mov    -0xc(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    11eb <detach_loop+0x9b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe3e(%rip),%rdi
call   1060 <kmsg@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach_loop>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11ab <detach_loop+0x5b>
mov    %eax,%ebp
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jns    119e <detach_loop+0x4e>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe90(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1060 <kmsg@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <close@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe40(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <kmsg@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"* unable to open loop device %s: %s\00"
@errno = global i32 0
@LOOP_CLR_FD = global i32 0
@.str.1 = [44 x i8] c"* unable to disassociate loop device %s: %s\00"
define void @detach_loop(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* %4, i32 %5)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = call i32 @kmsg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %12)
br label %27
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i32, i32* @LOOP_CLR_FD
%17 = call i64 @ioctl(i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i8*, i8** %2
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @kmsg(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %22)
br label %24
24: ; preds = %19, %14
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
br label %27
27: ; preds = %24, %9
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [36 x i8] c"* unable to open loop device %s: %s\00"
@errno = global i32 0
@LOOP_CLR_FD = global i32 0
@.str.1 = [44 x i8] c"* unable to disassociate loop device %s: %s\00"
define void @detach_loop(i8* %0) {
%2 = load i32, i32* @O_RDONLY
%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* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @kmsg(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %7)
br label %19
9: ; preds = %1
%10 = load i32, i32* @LOOP_CLR_FD
%11 = tail call i64 @ioctl(i32 %3, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 @kmsg(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %15)
br label %17
17: ; preds = %13, %9
%18 = tail call i32 @close(i32 %3)
br label %19
19: ; preds = %17, %5
ret void
}