#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_SET_FD;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int attach_loop_device(char *loop_device, char *file) {
  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;
  }
  int file_fd = open(file, O_RDWR);
  if (file_fd < 0) {
    fprintf(stderr, "Cannot open file %s (error code %d)\n", file, errno);
    close(loop_device_fd);
    return -1;
  }
  if (ioctl(loop_device_fd, LOOP_SET_FD, file_fd) != 0) {
    fprintf(stderr, "Cannot attach file %s to loop device %s (error code %d)\n",
            file, loop_device, errno);
    close(loop_device_fd);
    close(file_fd);
    return -1;
  }
  close(file_fd);
  close(loop_device_fd);
  return 0;
}


x86 assembly (clang, -O0):
<attach_loop_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    119f <attach_loop_device+0x5f>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    0x2e4d(%rip),%rax
mov    (%rax),%ecx
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <attach_loop_device+0x130>
mov    -0x18(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f6 <attach_loop_device+0xb6>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
lea    0xe51(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <attach_loop_device+0x130>
mov    -0x1c(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1259 <attach_loop_device+0x119>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    0x2da4(%rip),%rax
mov    (%rax),%r8d
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1270 <attach_loop_device+0x130>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_loop_device>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e94(%rip),%rbp
mov    0x0(%rbp),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11c0 <attach_loop_device+0x80>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11ea <attach_loop_device+0xaa>
mov    %eax,%ebp
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1030 <ioctl@plt>
test   %rax,%rax
je     121b <attach_loop_device+0xdb>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%r8d
lea    0xea9(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
jmp    120f <attach_loop_device+0xcf>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
lea    0xe27(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    122b <attach_loop_device+0xeb>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%ecx
lea    0xe29(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
jmp    122b <attach_loop_device+0xeb>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
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
@.str.1 = [37 x i8] c"Cannot open file %s (error code %d)\0A\00"
@LOOP_SET_FD = global i32 0
@.str.2 = [57 x i8] c"Cannot attach file %s to loop device %s (error code %d)\0A\00"
define i32 @attach_loop_device(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %16)
store i32 -1, i32* %3
br label %52
18: ; preds = %2
%19 = load i8*, i8** %5
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %5
%27 = load i32, i32* @errno
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %3
br label %52
31: ; preds = %18
%32 = load i32, i32* %6
%33 = load i32, i32* @LOOP_SET_FD
%34 = load i32, i32* %7
%35 = call i64 @ioctl(i32 %32, i32 %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %47
37: ; preds = %31
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %5
%40 = load i8*, i8** %4
%41 = load i32, i32* @errno
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* %40, i32 %41)
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %52
47: ; preds = %31
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %6
%51 = call i32 @close(i32 %50)
store i32 0, i32* %3
br label %52
52: ; preds = %47, %37, %24, %13
%53 = load i32, i32* %3
ret i32 %53
}

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
@.str.1 = [37 x i8] c"Cannot open file %s (error code %d)\0A\00"
@LOOP_SET_FD = global i32 0
@.str.2 = [57 x i8] c"Cannot attach file %s to loop device %s (error code %d)\0A\00"
define i32 @attach_loop_device(i8* %0, i8* %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
br label %32
10: ; preds = %2
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %1, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %16)
%18 = tail call i32 @close(i32 %4)
br label %32
19: ; preds = %10
%20 = load i32, i32* @LOOP_SET_FD
%21 = tail call i64 @ioctl(i32 %4, i32 %20, i32 %12)
%22 = icmp eq i64 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %0, i32 %25)
%27 = tail call i32 @close(i32 %4)
%28 = tail call i32 @close(i32 %12)
br label %32
29: ; preds = %19
%30 = tail call i32 @close(i32 %12)
%31 = tail call i32 @close(i32 %4)
br label %32
32: ; preds = %14, %23, %29, %6
%33 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %23 ], [ 0, %29 ]
ret i32 %33
}