#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 */
scalar_t__ attach_loop_device(char *, char *);
int /*<<< orphan*/ detach_loop_device(char *);
int errno;
char *find_free_loop_device();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *,
                           int);
int /*<<< orphan*/ free(char *);
scalar_t__ mount(char *, char *, char *, int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
char *mount_loopfs(char *img, char *target, char *fstype) {
  char *loop_device = find_free_loop_device();
  if (loop_device < 0) {
    return (char *)-1;
  }
  if (attach_loop_device(loop_device, img) != 0) {
    free(loop_device);
    return (char *)-1;
  }
  if (mount(loop_device, target, fstype, 0, "") != 0) {
    fprintf(stderr, "Canot mount %s on %s with fstype %s (error code %d)\n",
            loop_device, target, fstype, errno);
    detach_loop_device(loop_device);
    free(loop_device);
    return (char *)-1;
  }
  return loop_device;
}


x86 assembly (clang, -O0):
<mount_loopfs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x0,%al
call   1050 <find_free_loop_device@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    119a <mount_loopfs+0x3a>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <mount_loopfs+0xe3>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <attach_loop_device@plt>
cmp    $0x0,%rax
je     11ca <mount_loopfs+0x6a>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <mount_loopfs+0xe3>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
lea    0xe55(%rip),%r8
call   1060 <mount@plt>
cmp    $0x0,%rax
je     123b <mount_loopfs+0xdb>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    0x2dce(%rip),%rax
mov    (%rax),%r9d
lea    0xdec(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <detach_loop_device@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1243 <mount_loopfs+0xe3>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mount_loopfs>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
xor    %eax,%eax
call   1050 <find_free_loop_device@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1080 <attach_loop_device@plt>
test   %rax,%rax
jne    11d7 <mount_loopfs+0x77>
lea    0xea2(%rip),%r8
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1060 <mount@plt>
test   %rax,%rax
je     11e6 <mount_loopfs+0x86>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%r9d
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <detach_loop_device@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [53 x i8] c"Canot mount %s on %s with fstype %s (error code %d)\0A\00"
@errno = global i32 0
define i8* @mount_loopfs(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%9 = call i8* (...) @find_free_loop_device()
store i8* %9, i8** %8
%10 = load i8*, i8** %8
%11 = icmp ult i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* inttoptr (i64 -1 to i8*), i8** %4
br label %40
13: ; preds = %3
%14 = load i8*, i8** %8
%15 = load i8*, i8** %5
%16 = call i64 @attach_loop_device(i8* %14, i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %8
%20 = call i32 @free(i8* %19)
store i8* inttoptr (i64 -1 to i8*), i8** %4
br label %40
21: ; preds = %13
%22 = load i8*, i8** %8
%23 = load i8*, i8** %6
%24 = load i8*, i8** %7
%25 = call i64 @mount(i8* %22, i8* %23, i8* %24, i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %8
%30 = load i8*, i8** %6
%31 = load i8*, i8** %7
%32 = load i32, i32* @errno
%33 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %30, i8* %31, i32 %32)
%34 = load i8*, i8** %8
%35 = call i32 @detach_loop_device(i8* %34)
%36 = load i8*, i8** %8
%37 = call i32 @free(i8* %36)
store i8* inttoptr (i64 -1 to i8*), i8** %4
br label %40
38: ; preds = %21
%39 = load i8*, i8** %8
store i8* %39, i8** %4
br label %40
40: ; preds = %38, %27, %18, %12
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.1 = [53 x i8] c"Canot mount %s on %s with fstype %s (error code %d)\0A\00"
@errno = global i32 0
define i8* @mount_loopfs(i8* %0, i8* %1, i8* %2) {
%4 = tail call i8* (...) @find_free_loop_device()
%5 = tail call i64 @attach_loop_device(i8* %4, i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %3
%8 = tail call i64 @mount(i8* %4, i8* %1, i8* %2, i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %1, i8* %2, i32 %12)
%14 = tail call i32 @detach_loop_device(i8* %4)
br label %15
15: ; preds = %3, %10
%16 = tail call i32 @free(i8* %4)
br label %17
17: ; preds = %15, %7
%18 = phi i8* [ %4, %7 ], [ inttoptr (i64 -1 to i8*), %15 ]
ret i8* %18
}