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

/* Variables and functions */
int /*<<< orphan*/ LOOP_GET_STATUS64;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int errno;
int ioctl(int, int /*<<< orphan*/, struct loop_info64 *);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int open(char const *, int);
int resolve_loop_device_with_loopdev(const char *loop_dev, char *loop_file) {
  int fd;
  int ret;
  struct loop_info64 lo64;
  fd = open(loop_dev, O_RDONLY | O_NONBLOCK);
  if (fd < 0)
    return -errno;
  ret = ioctl(fd, LOOP_GET_STATUS64, &lo64);
  if (ret < 0) {
    ret = -errno;
    goto out;
  }
  memcpy(loop_file, lo64.lo_file_name, sizeof(lo64.lo_file_name));
  loop_file[sizeof(lo64.lo_file_name)] = 0;
out:
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<resolve_loop_device_with_loopdev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
mov    0x2e84(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    118b <resolve_loop_device_with_loopdev+0x4b>
xor    %eax,%eax
mov    0x2e47(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <resolve_loop_device_with_loopdev+0xa7>
mov    -0x1c(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c0 <resolve_loop_device_with_loopdev+0x80>
xor    %eax,%eax
mov    0x2e12(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    11d9 <resolve_loop_device_with_loopdev+0x99>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,0x4(%rax)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_loop_device_with_loopdev>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e8e(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     119b <resolve_loop_device_with_loopdev+0x5b>
mov    %eax,%ebp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1030 <ioctl@plt>
test   %eax,%eax
js     11a8 <resolve_loop_device_with_loopdev+0x68>
mov    %eax,%ebx
mov    0x8(%rsp),%esi
mov    %r14,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movb   $0x0,0x4(%r14)
jmp    11b3 <resolve_loop_device_with_loopdev+0x73>
mov    0x2e26(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
jmp    11ba <resolve_loop_device_with_loopdev+0x7a>
mov    0x2e19(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.loop_info64 = type { i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@LOOP_GET_STATUS64 = global i32 0
define i32 @resolve_loop_device_with_loopdev(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.loop_info64
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %10, %11
%13 = call i32 @open(i8* %9, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
store i32 %18, i32* %3
br label %39
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @LOOP_GET_STATUS64
%22 = call i32 @ioctl(i32 %20, i32 %21, %struct.loop_info64* %8)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %7
br label %35
28: ; preds = %19
%29 = load i8*, i8** %5
%30 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i8* %29, i32 %31, i32 4)
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 4
store i8 0, i8* %34
br label %35
35: ; preds = %28, %25
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %39
39: ; preds = %35, %16
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
%struct.loop_info64 = type { i32 }
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i32 0
@LOOP_GET_STATUS64 = global i32 0
define i32 @resolve_loop_device_with_loopdev(i8* nocapture %0, i8* %1) {
%3 = alloca %struct.loop_info64
%4 = bitcast %struct.loop_info64* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %28
13: ; preds = %2
%14 = load i32, i32* @LOOP_GET_STATUS64
%15 = call i32 @ioctl(i32 %8, i32 %14, %struct.loop_info64* %3)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
br label %25
20: ; preds = %13
%21 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @memcpy(i8* %1, i32 %22, i32 4)
%24 = getelementptr inbounds i8, i8* %1, i64 4
store i8 0, i8* %24
br label %25
25: ; preds = %20, %17
%26 = phi i32 [ %19, %17 ], [ %15, %20 ]
%27 = call i32 @close(i32 %8)
br label %28
28: ; preds = %25, %10
%29 = phi i32 [ %12, %10 ], [ %26, %25 ]
ret i32 %29
}