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

/* Variables and functions */
scalar_t__ ENXIO;
int /*<<< orphan*/ LOOP_GET_STATUS64;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct loop_info64 *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
char *crypt_loop_get_device(void) {
  char dev[20];
  int i, loop_fd;
  struct stat st;
  struct loop_info64 lo64 = {0};
  for (i = 0; i < 256; i++) {
    sprintf(dev, "/dev/loop%d", i);
    if (stat(dev, &st) || !S_ISBLK(st.st_mode))
      return NULL;
    loop_fd = open(dev, O_RDONLY);
    if (loop_fd < 0)
      return NULL;
    if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) && errno == ENXIO) {
      close(loop_fd);
      return strdup(dev);
    }
    close(loop_fd);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<crypt_loop_get_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x100,-0x24(%rbp)
jge    1289 <crypt_loop_get_device+0x109>
lea    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe46(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11e7 <crypt_loop_get_device+0x67>
mov    -0x30(%rbp),%edi
call   1070 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    11f4 <crypt_loop_get_device+0x74>
movq   $0x0,-0x8(%rbp)
jmp    1291 <crypt_loop_get_device+0x111>
lea    -0x20(%rbp),%rdi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1220 <crypt_loop_get_device+0xa0>
movq   $0x0,-0x8(%rbp)
jmp    1291 <crypt_loop_get_device+0x111>
mov    -0x28(%rbp),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     1273 <crypt_loop_get_device+0xf3>
mov    0x2d8a(%rip),%rax
mov    (%rax),%rax
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <crypt_loop_get_device+0xf3>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
lea    -0x20(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1291 <crypt_loop_get_device+0x111>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119f <crypt_loop_get_device+0x1f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_loop_get_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x0,(%rsp)
lea    0xe74(%rip),%r15
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r12
mov    %rsp,%r13
xor    %ebp,%ebp
jmp    11b7 <crypt_loop_get_device+0x47>
nopl   (%rax)
mov    %r14d,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    $0x100,%ebp
je     1240 <crypt_loop_get_device+0xd0>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1080 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    1240 <crypt_loop_get_device+0xd0>
mov    0x8(%rsp),%edi
call   1060 <S_ISBLK@plt>
test   %eax,%eax
je     1240 <crypt_loop_get_device+0xd0>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1240 <crypt_loop_get_device+0xd0>
mov    %eax,%r14d
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %r13,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     11a0 <crypt_loop_get_device+0x30>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <crypt_loop_get_device+0x30>
mov    %r14d,%edi
call   1040 <close@plt>
lea    0x10(%rsp),%rdi
call   1090 <strdup@plt>
jmp    1242 <crypt_loop_get_device+0xd2>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
%struct.loop_info64 = type { i32 }
@.str = [12 x i8] c"/dev/loop%d\00"
@O_RDONLY = global i32 0
@LOOP_GET_STATUS64 = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
define i8* @crypt_loop_get_device() {
%1 = alloca i8*
%2 = alloca [20 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca %struct.loop_info64
%7 = bitcast %struct.loop_info64* %6 to i8*
store i32 0, i32* %3
br label %8
8: ; preds = %48, %0
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 256
br i1 %10, label %11, label %51
11: ; preds = %8
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%16 = call i64 @stat(i8* %15, %struct.stat* %5)
%17 = icmp ne i64 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @S_ISBLK(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %18, %11
store i8* null, i8** %1
br label %52
24: ; preds = %18
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i8* null, i8** %1
br label %52
31: ; preds = %24
%32 = load i32, i32* %4
%33 = load i32, i32* @LOOP_GET_STATUS64
%34 = call i64 @ioctl(i32 %32, i32 %33, %struct.loop_info64* %6)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %31
%37 = load i64, i64* @errno
%38 = load i64, i64* @ENXIO
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%44 = call i8* @strdup(i8* %43)
store i8* %44, i8** %1
br label %52
45: ; preds = %36, %31
%46 = load i32, i32* %4
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45
%49 = load i32, i32* %3
%50 = add nsw i32 %49, 1
store i32 %50, i32* %3
br label %8
51: ; preds = %8
store i8* null, i8** %1
br label %52
52: ; preds = %51, %40, %30, %23
%53 = load i8*, i8** %1
ret i8* %53
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.loop_info64 = type { i32 }
@.str = [12 x i8] c"/dev/loop%d\00"
@O_RDONLY = global i32 0
@LOOP_GET_STATUS64 = global i32 0
@errno = global i64 0
@ENXIO = global i64 0
define noalias i8* @crypt_loop_get_device() {
%1 = alloca [20 x i8]
%2 = alloca %struct.stat
%3 = alloca %struct.loop_info64
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.stat* %2 to i8*
%6 = bitcast %struct.loop_info64* %3 to i8*
%7 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %3, i64 0, i32 0
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %9
9: ; preds = %0, %33
%10 = phi i32 [ 0, %0 ], [ %35, %33 ]
%11 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i64 @stat(i8* %4, %struct.stat* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %9
%15 = load i32, i32* %8
%16 = call i32 @S_ISBLK(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %37, label %18
18: ; preds = %14
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* %4, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %18
%23 = load i32, i32* @LOOP_GET_STATUS64
%24 = call i64 @ioctl(i32 %20, i32 %23, %struct.loop_info64* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %22
%27 = load i64, i64* @errno
%28 = load i64, i64* @ENXIO
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = call i32 @close(i32 %20)
%32 = call i8* @strdup(i8* %4)
br label %37
33: ; preds = %26, %22
%34 = call i32 @close(i32 %20)
%35 = add nuw nsw i32 %10, 1
%36 = icmp eq i32 %35, 256
br i1 %36, label %37, label %9
37: ; preds = %33, %18, %9, %14, %30
%38 = phi i8* [ %32, %30 ], [ null, %14 ], [ null, %9 ], [ null, %18 ], [ null, %33 ]
ret i8* %38
}