#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*/ member_0;
};

/* Variables and functions */
scalar_t__ ENXIO;
int /*<<< orphan*/ LOOP_GET_STATUS64;
int /*<<< orphan*/ O_RDONLY;
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);
char *strdup(char *);
char *crypt_loop_get_device_old(void) {
  char dev[20];
  int i, loop_fd;
  struct loop_info64 lo64 = {0};
  for (i = 0; i < 256; i++) {
    sprintf(dev, "/dev/loop%d", i);
    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_old>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0x100,-0x24(%rbp)
jge    1234 <crypt_loop_get_device_old+0xd4>
lea    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe66(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11cb <crypt_loop_get_device_old+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    123c <crypt_loop_get_device_old+0xdc>
mov    -0x28(%rbp),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     121e <crypt_loop_get_device_old+0xbe>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%rax
jne    121e <crypt_loop_get_device_old+0xbe>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
lea    -0x20(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    123c <crypt_loop_get_device_old+0xdc>
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117f <crypt_loop_get_device_old+0x1f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_loop_get_device_old>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x0,0x8(%rsp)
lea    0x10(%rsp),%r13
mov    0x2e76(%rip),%r14
mov    0x2e67(%rip),%r15
lea    0x8(%rsp),%r12
xor    %ebp,%ebp
jmp    11a2 <crypt_loop_get_device_old+0x52>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <close@plt>
add    $0x1,%ebp
cmp    $0x100,%ebp
je     11ff <crypt_loop_get_device_old+0xaf>
mov    %r13,%rdi
lea    0xe54(%rip),%rsi
mov    %ebp,%edx
call   1060 <sprintf@plt>
mov    (%r14),%esi
mov    %r13,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11ff <crypt_loop_get_device_old+0xaf>
mov    %eax,%ebx
mov    (%r15),%esi
mov    %eax,%edi
mov    %r12,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
je     1190 <crypt_loop_get_device_old+0x40>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
jne    1190 <crypt_loop_get_device_old+0x40>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0x10(%rsp),%rdi
call   1070 <strdup@plt>
jmp    1201 <crypt_loop_get_device_old+0xb1>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%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_old() {
%1 = alloca i8*
%2 = alloca [20 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.loop_info64
%6 = bitcast %struct.loop_info64* %5 to i8*
store i32 0, i32* %3
br label %7
7: ; preds = %37, %0
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 256
br i1 %9, label %10, label %40
10: ; preds = %7
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* %3
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %10
store i8* null, i8** %1
br label %41
20: ; preds = %10
%21 = load i32, i32* %4
%22 = load i32, i32* @LOOP_GET_STATUS64
%23 = call i64 @ioctl(i32 %21, i32 %22, %struct.loop_info64* %5)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENXIO
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%33 = call i8* @strdup(i8* %32)
store i8* %33, i8** %1
br label %41
34: ; preds = %25, %20
%35 = load i32, i32* %4
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %7
40: ; preds = %7
store i8* null, i8** %1
br label %41
41: ; preds = %40, %29, %19
%42 = load i8*, i8** %1
ret i8* %42
}

LLVM IR (-O2):
%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_old() {
%1 = alloca [20 x i8]
%2 = alloca %struct.loop_info64
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.loop_info64* %2 to i8*
%5 = getelementptr inbounds %struct.loop_info64, %struct.loop_info64* %2, i64 0, i32 0
store i32 0, i32* %5
br label %6
6: ; preds = %0, %23
%7 = phi i32 [ 0, %0 ], [ %25, %23 ]
%8 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %3, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %6
%13 = load i32, i32* @LOOP_GET_STATUS64
%14 = call i64 @ioctl(i32 %10, i32 %13, %struct.loop_info64* %2)
%15 = icmp eq i64 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENXIO
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @close(i32 %10)
%22 = call i8* @strdup(i8* %3)
br label %27
23: ; preds = %16, %12
%24 = call i32 @close(i32 %10)
%25 = add nuw nsw i32 %7, 1
%26 = icmp eq i32 %25, 256
br i1 %26, label %27, label %6
27: ; preds = %23, %6, %20
%28 = phi i8* [ %22, %20 ], [ null, %6 ], [ null, %23 ]
ret i8* %28
}