#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int ra_reg_read(int offset) {
  int fd;
  fd = open("/dev/rdm0", O_RDONLY);
  if (fd < 0) {
    printf("Open pseudo device failed\n");
    return -1;
  }
  if (ioctl(fd, 0x6B03, &offset) < 0) {
    printf("ioctl error\n");
    close(fd);
    return -1;
  }
  close(fd);
  return offset;
}


x86 assembly (clang, -O0):
<ra_reg_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
lea    0xea5(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1185 <ra_reg_read+0x45>
lea    0xe96(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <ra_reg_read+0x8e>
mov    -0xc(%rbp),%edi
mov    $0x6b03,%esi
lea    -0x8(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11c0 <ra_reg_read+0x80>
lea    0xe7e(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ce <ra_reg_read+0x8e>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ra_reg_read>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,0xc(%rsp)
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     1187 <ra_reg_read+0x47>
mov    %eax,%ebx
lea    0xc(%rsp),%rdx
mov    %eax,%edi
mov    $0x6b03,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1195 <ra_reg_read+0x55>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0xc(%rsp),%eax
jmp    11ad <ra_reg_read+0x6d>
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
jmp    11a8 <ra_reg_read+0x68>
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/rdm0\00"
@O_RDONLY = global i32 0
@.str.1 = [27 x i8] c"Open pseudo device failed\0A\00"
@.str.2 = [13 x i8] c"ioctl error\0A\00"
define i32 @ra_reg_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %23
11: ; preds = %1
%12 = load i32, i32* %4
%13 = call i64 @ioctl(i32 %12, i32 27395, i32* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
store i32 -1, i32* %2
br label %23
19: ; preds = %11
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %3
store i32 %22, i32* %2
br label %23
23: ; preds = %19, %15, %9
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/rdm0\00"
@O_RDONLY = global i32 0
@str = [12 x i8] c"ioctl error\00"
@str.3 = [26 x i8] c"Open pseudo device failed\00"
define i32 @ra_reg_read(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.3, i64 0, i64 0))
br label %17
8: ; preds = %1
%9 = call i64 @ioctl(i32 %4, i32 27395, i32* %2)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%13 = call i32 @close(i32 %4)
br label %17
14: ; preds = %8
%15 = call i32 @close(i32 %4)
%16 = load i32, i32* %2
br label %17
17: ; preds = %14, %11, %6
%18 = phi i32 [ -1, %6 ], [ -1, %11 ], [ %16, %14 ]
ret i32 %18
}