#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_write(int offset, int value) {
  int fd;
  int method = 0x6B02 | (offset << 16);
  fd = open("/dev/rdm0", O_RDONLY);
  if (fd < 0) {
    printf("Open pseudo device failed\n");
    return -1;
  }
  if (ioctl(fd, method, &value) < 0) {
    printf("ioctl error\n");
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<ra_reg_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x10,%eax
or     $0x6b02,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1196 <ra_reg_write+0x56>
lea    0xe85(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <ra_reg_write+0x9e>
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
lea    -0xc(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11cf <ra_reg_write+0x8f>
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11de <ra_reg_write+0x9e>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ra_reg_write>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %esi,0x4(%rsp)
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     118b <ra_reg_write+0x4b>
mov    %eax,%ebx
shl    $0x10,%ebp
or     $0x6b02,%ebp
lea    0x4(%rsp),%rdx
mov    %eax,%edi
mov    %ebp,%esi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1199 <ra_reg_write+0x59>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    11b1 <ra_reg_write+0x71>
lea    0xe84(%rip),%rdi
call   1030 <puts@plt>
jmp    11ac <ra_reg_write+0x6c>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
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_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = shl i32 %8, 16
%10 = or i32 27394, %9
store i32 %10, i32* %7
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %29
17: ; preds = %2
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = call i64 @ioctl(i32 %18, i32 %19, i32* %5)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
store i32 -1, i32* %3
br label %29
26: ; preds = %17
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
store i32 0, i32* %3
br label %29
29: ; preds = %26, %22, %15
%30 = load i32, i32* %3
ret i32 %30
}

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_write(i32 %0, i32 %1) {
%3 = alloca i32
store i32 %1, i32* %3
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.3, i64 0, i64 0))
br label %19
9: ; preds = %2
%10 = shl i32 %0, 16
%11 = or i32 %10, 27394
%12 = call i64 @ioctl(i32 %5, i32 %11, i32* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%16 = call i32 @close(i32 %5)
br label %19
17: ; preds = %9
%18 = call i32 @close(i32 %5)
br label %19
19: ; preds = %17, %14, %7
%20 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %17 ]
ret i32 %20
}