#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ S_ISBLK(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
char *crypt_loop_get_device_old();
int ioctl(int, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ sprintf(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
char *crypt_loop_get_device(void) {
  char dev[64];
  int i, loop_fd;
  struct stat st;
  loop_fd = open("/dev/loop-control", O_RDONLY);
  if (loop_fd < 0)
    return crypt_loop_get_device_old();
  i = ioctl(loop_fd, 0x4C82);
  if (i < 0) {
    close(loop_fd);
    return NULL;
  }
  close(loop_fd);
  if (sprintf(dev, "/dev/loop%d", i) < 0)
    return NULL;
  if (stat(dev, &st) || !S_ISBLK(st.st_mode))
    return NULL;
  return strdup(dev);
}


x86 assembly (clang, -O0):
<crypt_loop_get_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
jge    11ba <crypt_loop_get_device+0x3a>
mov    $0x0,%al
call   1060 <crypt_loop_get_device_old@plt>
mov    %rax,-0x8(%rbp)
jmp    125d <crypt_loop_get_device+0xdd>
mov    -0x58(%rbp),%edi
mov    $0x4c82,%esi
call   1030 <ioctl@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    11e9 <crypt_loop_get_device+0x69>
mov    -0x58(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    125d <crypt_loop_get_device+0xdd>
mov    -0x58(%rbp),%edi
call   1040 <close@plt>
lea    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%edx
lea    0xe13(%rip),%rsi
call   1090 <sprintf@plt>
cmp    $0x0,%rax
jge    121b <crypt_loop_get_device+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    125d <crypt_loop_get_device+0xdd>
lea    -0x50(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1243 <crypt_loop_get_device+0xc3>
mov    -0x60(%rbp),%edi
call   1070 <S_ISBLK@plt>
cmp    $0x0,%eax
jne    1250 <crypt_loop_get_device+0xd0>
movq   $0x0,-0x8(%rbp)
jmp    125d <crypt_loop_get_device+0xdd>
lea    -0x50(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_loop_get_device>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11ef <crypt_loop_get_device+0x6f>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4c82,%esi
call   1030 <ioctl@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
test   %ebp,%ebp
js     11e6 <crypt_loop_get_device+0x66>
lea    0xe51(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebp,%edx
call   1090 <sprintf@plt>
test   %rax,%rax
js     11e6 <crypt_loop_get_device+0x66>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     11fc <crypt_loop_get_device+0x7c>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <crypt_loop_get_device_old@plt>
mov    0x8(%rsp),%edi
call   1070 <S_ISBLK@plt>
test   %eax,%eax
je     11e6 <crypt_loop_get_device+0x66>
lea    0x10(%rsp),%rdi
call   10a0 <strdup@plt>
jmp    11e8 <crypt_loop_get_device+0x68>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [18 x i8] c"/dev/loop-control\00"
@O_RDONLY = global i32 0
@.str.1 = [12 x i8] c"/dev/loop%d\00"
define i8* @crypt_loop_get_device() {
%1 = alloca [64 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i8* (...) @crypt_loop_get_device_old()
br label %27
10: ; preds = %0
%11 = tail call i32 @ioctl(i32 %6, i32 19586)
%12 = icmp slt i32 %11, 0
%13 = tail call i32 @close(i32 %6)
br i1 %12, label %27, label %14
14: ; preds = %10
%15 = call i64 @sprintf(i8* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%16 = icmp slt i64 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %14
%18 = call i64 @stat(i8* %3, %struct.stat* %2)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISBLK(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %20
%26 = call i8* @strdup(i8* %3)
br label %27
27: ; preds = %10, %17, %20, %14, %25, %8
%28 = phi i8* [ %9, %8 ], [ %26, %25 ], [ null, %14 ], [ null, %20 ], [ null, %17 ], [ null, %10 ]
ret i8* %28
}