#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_CTL_GET_FREE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
int ioctl(int, int /*<<< orphan*/);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *find_free_loop_device() {
  int loop_control_fd = open("/dev/loop-control", O_RDWR);
  if (loop_control_fd < 0) {
    fprintf(stderr, "Cannot open /dev/loop_control (error code %d)\n", errno);
    return (char *)-1;
  }
  char *loop_device_name = malloc(20);
  int free_loop_device_number = ioctl(loop_control_fd, LOOP_CTL_GET_FREE);
  if (free_loop_device_number < 0) {
    fprintf(stderr, "Cannot find free loop device (error code %d)\n", errno);
    free(loop_device_name);
    return (char *)-1;
  }
  sprintf(loop_device_name, "/dev/loop%d", free_loop_device_number);
  close(loop_control_fd);
  return loop_device_name;
}


x86 assembly (clang, -O0):
<find_free_loop_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11c8 <find_free_loop_device+0x58>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
lea    0xe5f(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    124e <find_free_loop_device+0xde>
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
call   1040 <ioctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122b <find_free_loop_device+0xbb>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
lea    0xe34(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    124e <find_free_loop_device+0xde>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe36(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_free_loop_device>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7d(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11c9 <find_free_loop_device+0x59>
mov    %eax,%ebp
mov    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
js     11e9 <find_free_loop_device+0x79>
lea    0xeb9(%rip),%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1216 <find_free_loop_device+0xa6>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
lea    0xe30(%rip),%rsi
call   1060 <fprintf@plt>
jmp    120f <find_free_loop_device+0x9f>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/dev/loop-control\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [47 x i8] c"Cannot open /dev/loop_control (error code %d)\0A\00"
@errno = global i32 0
@LOOP_CTL_GET_FREE = global i32 0
@.str.2 = [46 x i8] c"Cannot find free loop device (error code %d)\0A\00"
@.str.3 = [12 x i8] c"/dev/loop%d\00"
define i8* @find_free_loop_device() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* @errno
%12 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i8* inttoptr (i64 -1 to i8*), i8** %1
br label %33
13: ; preds = %0
%14 = call i8* @malloc(i32 20)
store i8* %14, i8** %3
%15 = load i32, i32* %2
%16 = load i32, i32* @LOOP_CTL_GET_FREE
%17 = call i32 @ioctl(i32 %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i8*, i8** %3
%25 = call i32 @free(i8* %24)
store i8* inttoptr (i64 -1 to i8*), i8** %1
br label %33
26: ; preds = %13
%27 = load i8*, i8** %3
%28 = load i32, i32* %4
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
%32 = load i8*, i8** %3
store i8* %32, i8** %1
br label %33
33: ; preds = %26, %20, %9
%34 = load i8*, i8** %1
ret i8* %34
}

LLVM IR (-O2):
@.str = [18 x i8] c"/dev/loop-control\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [47 x i8] c"Cannot open /dev/loop_control (error code %d)\0A\00"
@errno = global i32 0
@LOOP_CTL_GET_FREE = global i32 0
@.str.2 = [46 x i8] c"Cannot find free loop device (error code %d)\0A\00"
@.str.3 = [12 x i8] c"/dev/loop%d\00"
define i8* @find_free_loop_device() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %21
8: ; preds = %0
%9 = tail call dereferenceable_or_null(20) i8* @malloc(i32 20)
%10 = load i32, i32* @LOOP_CTL_GET_FREE
%11 = tail call i32 @ioctl(i32 %2, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = tail call i32 @free(i8* %9)
br label %21
18: ; preds = %8
%19 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%20 = tail call i32 @close(i32 %2)
br label %21
21: ; preds = %13, %18, %4
%22 = phi i8* [ inttoptr (i64 -1 to i8*), %4 ], [ inttoptr (i64 -1 to i8*), %13 ], [ %9, %18 ]
ret i8* %22
}