#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 /*<<< orphan*/ uint64_t;
struct loop_info64 {
  int /*<<< orphan*/ lo_offset;
};

/* Variables and functions */
int /*<<< orphan*/ LOOP_CLR_FD;
int /*<<< orphan*/ LOOP_SET_FD;
int /*<<< orphan*/ LOOP_SET_STATUS64;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ drop();
scalar_t__ escalate();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ memset(struct loop_info64 *, int /*<<< orphan*/, int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int loopdev_setup_device(const char *file, uint64_t offset,
                         const char *device) {
  int file_fd = open(file, O_RDWR);
  int device_fd = -1;
  struct loop_info64 info;
  if (file_fd < 0) {
    fprintf(stderr, "Failed to open backing file (%s).\n", file);
    goto error;
  }
  if (escalate())
    goto error;
  if ((device_fd = open(device, O_RDWR)) < 0) {
    fprintf(stderr, "Failed to open device (%s).\n", device);
    goto error;
  }
  if (drop())
    goto error;
  if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) {
    fprintf(stderr, "Failed to set fd.\n");
    goto error;
  }
  close(file_fd);
  file_fd = -1;
  memset(&info, 0, sizeof(struct loop_info64));
  info.lo_offset = offset;
  if (ioctl(device_fd, LOOP_SET_STATUS64, &info)) {
    fprintf(stderr, "Failed to set info.\n");
    goto error;
  }
  close(device_fd);
  device_fd = -1;
  return 0;
error:
  if (file_fd >= 0) {
    close(file_fd);
  }
  if (device_fd >= 0) {
    ioctl(device_fd, LOOP_CLR_FD, 0);
    close(device_fd);
  }
  return 1;
}


x86 assembly (clang, -O0):
<loopdev_setup_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11c9 <loopdev_setup_device+0x59>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ec <loopdev_setup_device+0x17c>
mov    $0x0,%al
call   1070 <escalate@plt>
cmp    $0x0,%rax
je     11df <loopdev_setup_device+0x6f>
jmp    12ec <loopdev_setup_device+0x17c>
mov    -0x20(%rbp),%rdi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    121d <loopdev_setup_device+0xad>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ec <loopdev_setup_device+0x17c>
mov    $0x0,%al
call   1080 <drop@plt>
cmp    $0x0,%rax
je     1233 <loopdev_setup_device+0xc3>
jmp    12ec <loopdev_setup_device+0x17c>
mov    -0x28(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    126f <loopdev_setup_device+0xff>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ec <loopdev_setup_device+0x17c>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
mov    $0x0,%al
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     12d1 <loopdev_setup_device+0x161>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ec <loopdev_setup_device+0x17c>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
movl   $0x0,-0x4(%rbp)
jmp    132c <loopdev_setup_device+0x1bc>
cmpl   $0x0,-0x24(%rbp)
jl     12fe <loopdev_setup_device+0x18e>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jl     1325 <loopdev_setup_device+0x1b5>
mov    -0x28(%rbp),%edi
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    $0x0,%al
call   1040 <ioctl@plt>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loopdev_setup_device>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e5c(%rip),%r14
mov    (%r14),%esi
call   1090 <open@plt>
test   %eax,%eax
js     11aa <loopdev_setup_device+0x3a>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <escalate@plt>
test   %rax,%rax
je     11c6 <loopdev_setup_device+0x56>
mov    %ebp,%edi
jmp    1203 <loopdev_setup_device+0x93>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1208 <loopdev_setup_device+0x98>
mov    (%r14),%esi
mov    %r12,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     121c <loopdev_setup_device+0xac>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <drop@plt>
test   %rax,%rax
je     123b <loopdev_setup_device+0xcb>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
xor    %eax,%eax
call   1040 <ioctl@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11a6 <loopdev_setup_device+0x36>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %rax,%rax
js     12ae <loopdev_setup_device+0x13e>
mov    %ebp,%edi
call   1050 <close@plt>
lea    0x8(%rsp),%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %r15d,0x8(%rsp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <ioctl@plt>
test   %rax,%rax
je     12ca <loopdev_setup_device+0x15a>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11eb <loopdev_setup_device+0x7b>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11e4 <loopdev_setup_device+0x74>
mov    %r14d,%edi
call   1050 <close@plt>
jmp    120d <loopdev_setup_device+0x9d>

LLVM IR (-O0):
%struct.loop_info64 = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"Failed to open backing file (%s).\0A\00"
@.str.1 = [29 x i8] c"Failed to open device (%s).\0A\00"
@LOOP_SET_FD = global i32 0
@.str.2 = [19 x i8] c"Failed to set fd.\0A\00"
@LOOP_SET_STATUS64 = global i32 0
@.str.3 = [21 x i8] c"Failed to set info.\0A\00"
@LOOP_CLR_FD = global i32 0
define i32 @loopdev_setup_device(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.loop_info64
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
store i32 -1, i32* %9
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %5
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %62
20: ; preds = %3
%21 = call i64 (...) @escalate()
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
br label %62
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %7
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %62
33: ; preds = %24
%34 = call i64 (...) @drop()
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %33
br label %62
37: ; preds = %33
%38 = load i32, i32* %9
%39 = load i32, i32* @LOOP_SET_FD
%40 = load i32, i32* %8
%41 = call i64 (i32, i32, ...) @ioctl(i32 %38, i32 %39, i32 %40)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %62
46: ; preds = %37
%47 = load i32, i32* %8
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %8
%49 = call i32 @memset(%struct.loop_info64* %10, i32 0, i32 4)
%50 = load i32, i32* %6
%51 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %10, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %9
%53 = load i32, i32* @LOOP_SET_STATUS64
%54 = call i64 (i32, i32, ...) @ioctl(i32 %52, i32 %53, %struct.loop_info64* %10)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %46
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %62
59: ; preds = %46
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %9
store i32 0, i32* %4
br label %78
62: ; preds = %56, %43, %36, %29, %23, %16
%63 = load i32, i32* %8
%64 = icmp sge i32 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %65, %62
%69 = load i32, i32* %9
%70 = icmp sge i32 %69, 0
br i1 %70, label %71, label %77
71: ; preds = %68
%72 = load i32, i32* %9
%73 = load i32, i32* @LOOP_CLR_FD
%74 = call i64 (i32, i32, ...) @ioctl(i32 %72, i32 %73, i32 0)
%75 = load i32, i32* %9
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %71, %68
store i32 1, i32* %4
br label %78
78: ; preds = %77, %59
%79 = load i32, i32* %4
ret i32 %79
}

LLVM IR (-O2):
%struct.loop_info64 = type { i32 }
@O_RDWR = global i32 0
@stderr = global i32 0
@.str = [35 x i8] c"Failed to open backing file (%s).\0A\00"
@.str.1 = [29 x i8] c"Failed to open device (%s).\0A\00"
@LOOP_SET_FD = global i32 0
@.str.2 = [19 x i8] c"Failed to set fd.\0A\00"
@LOOP_SET_STATUS64 = global i32 0
@.str.3 = [21 x i8] c"Failed to set info.\0A\00"
@LOOP_CLR_FD = global i32 0
define i32 @loopdev_setup_device(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.loop_info64
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = bitcast %struct.loop_info64* %4 to i8*
%8 = icmp slt i32 %6, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %52
12: ; preds = %3
%13 = tail call i64 (...) @escalate()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %46
15: ; preds = %12
%16 = load i32, i32* @O_RDWR
%17 = tail call i32 @open(i8* %2, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %46
22: ; preds = %15
%23 = tail call i64 (...) @drop()
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %44
25: ; preds = %22
%26 = load i32, i32* @LOOP_SET_FD
%27 = tail call i64 (i32, i32, ...) @ioctl(i32 %17, i32 %26, i32 %6)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %44
32: ; preds = %25
%33 = tail call i32 @close(i32 %6)
%34 = call i32 @memset(%struct.loop_info64* %4, i32 0, i32 4)
%35 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %4, i64 0, i32 0
store i32 %1, i32* %35
%36 = load i32, i32* @LOOP_SET_STATUS64
%37 = call i64 (i32, i32, ...) @ioctl(i32 %17, i32 %36, %struct.loop_info64* %4)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %48
42: ; preds = %32
%43 = call i32 @close(i32 %17)
br label %52
44: ; preds = %29, %22
%45 = tail call i32 @close(i32 %6)
br label %48
46: ; preds = %19, %12
%47 = tail call i32 @close(i32 %6)
br label %52
48: ; preds = %44, %39
%49 = load i32, i32* @LOOP_CLR_FD
%50 = call i64 (i32, i32, ...) @ioctl(i32 %17, i32 %49, i32 0)
%51 = call i32 @close(i32 %17)
br label %52
52: ; preds = %46, %9, %48, %42
%53 = phi i32 [ 0, %42 ], [ 1, %48 ], [ 1, %46 ], [ 1, %9 ]
ret i32 %53
}