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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned int);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, unsigned int *);
int /*<<< orphan*/ printf(char *);
mv_reg_read(unsigned int reg) {
  unsigned int value;
  FILE *resource_dump;
  resource_dump = fopen("/proc/resource_dump", "w");
  if (!resource_dump) {
    printf("Eror opening file /proc/resource_dump\n");
    exit(-1);
  }
  fprintf(resource_dump, "register  r %08x", reg);
  fclose(resource_dump);
  resource_dump = fopen("/proc/resource_dump", "r");
  if (!resource_dump) {
    printf("Eror opening file /proc/resource_dump\n");
    exit(-1);
  }
  fscanf(resource_dump, "%x", &value);
  fclose(resource_dump);
  return value;
}


x86 assembly (clang, -O0):
<mv_reg_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe8e(%rip),%rdi
lea    0xe9b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a3 <mv_reg_read+0x43>
lea    0xe82(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8c(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xe3a(%rip),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <mv_reg_read+0x97>
lea    0xe2e(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x8(%rbp),%rdx
call   1050 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mv_reg_read>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
lea    0xebe(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11db <mv_reg_read+0x7b>
mov    %rax,%rbx
lea    0xe7b(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe8a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11db <mv_reg_read+0x7b>
mov    %rax,%rbx
lea    0xe5a(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"/proc/resource_dump\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [39 x i8] c"Eror opening file /proc/resource_dump\0A\00"
@.str.3 = [17 x i8] c"register r %08x\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"%x\00"
define i32 @mv_reg_read(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 %0, i32* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %1
%12 = load i32*, i32** %4
%13 = load i32, i32* %2
%14 = call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = load i32*, i32** %4
%16 = call i32 @fclose(i32* %15)
%17 = call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %17, i32** %4
%18 = load i32*, i32** %4
%19 = icmp ne i32* %18, null
br i1 %19, label %23, label %20
20: ; preds = %11
%21 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %11
%24 = load i32*, i32** %4
%25 = call i32 @fscanf(i32* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%26 = load i32*, i32** %4
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [20 x i8] c"/proc/resource_dump\00"
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [17 x i8] c"register r %08x\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [3 x i8] c"%x\00"
@str.6 = [38 x i8] c"Eror opening file /proc/resource_dump\00"
define i32 @mv_reg_read(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %1
%10 = tail call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%11 = tail call i32 @fclose(i32* %4)
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.6, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %9
%18 = call i32 @fscanf(i32* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %2)
%19 = call i32 @fclose(i32* %12)
%20 = load i32, i32* %2
ret i32 %20
}