#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 */
typedef int /*<<< orphan*/ mnt;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ __ploop_err(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
scalar_t__ ploop_get_mnt_by_dev(char const *, char *, int);
int open_mount_point(const char *device) {
  int fd;
  char mnt[PATH_MAX];
  if (ploop_get_mnt_by_dev(device, mnt, sizeof(mnt))) {
    __ploop_err(0, "Can't find mount point for %s", device);
    return -1;
  }
  fd = open(mnt, O_RDONLY);
  if (fd < 0) {
    __ploop_err(errno, "Can't open %s", mnt);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_mount_point>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    %eax,%edx
call   1040 <ploop_get_mnt_by_dev@plt>
cmp    $0x0,%rax
je     11a2 <open_mount_point+0x72>
mov    -0x10(%rbp),%rdx
xor    %edi,%edi
lea    0xe76(%rip),%rsi
call   1050 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11fa <open_mount_point+0xca>
mov    -0x38(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1030 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ed <open_mount_point+0xbd>
mov    -0x38(%rbp),%rdx
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1050 <__ploop_err@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11fa <open_mount_point+0xca>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_mount_point>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2eaf(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1040 <ploop_get_mnt_by_dev@plt>
test   %rax,%rax
je     117c <open_mount_point+0x4c>
lea    0xe95(%rip),%rsi
xor    %edi,%edi
mov    %r14,%rdx
call   1050 <__ploop_err@plt>
mov    $0xffffffff,%eax
jmp    1191 <open_mount_point+0x61>
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <open@plt>
test   %eax,%eax
js     119a <open_mount_point+0x6a>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
mov    %rbx,%rdx
jmp    1170 <open_mount_point+0x40>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [30 x i8] c"Can't find mount point for %s\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [14 x i8] c"Can't open %s\00"
define i32 @open_mount_point(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = load i8*, i8** %3
%13 = trunc i64 %9 to i32
%14 = call i64 @ploop_get_mnt_by_dev(i8* %12, i8* %11, i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i32 @__ploop_err(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %29
19: ; preds = %1
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %11, i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @errno
%26 = call i32 @__ploop_err(i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %29
27: ; preds = %19
%28 = load i32, i32* %4
store i32 %28, i32* %2
store i32 1, i32* %7
br label %29
29: ; preds = %27, %24, %16
%30 = load i8*, i8** %5
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [30 x i8] c"Can't find mount point for %s\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [14 x i8] c"Can't open %s\00"
define i32 @open_mount_point(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i64 @ploop_get_mnt_by_dev(i8* %0, i8* %4, i32 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = call i32 @__ploop_err(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %16
9: ; preds = %1
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %4, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = call i32 @__ploop_err(i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %16
16: ; preds = %9, %13, %7
%17 = phi i32 [ -1, %7 ], [ -1, %13 ], [ %11, %9 ]
ret i32 %17
}