#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int get_property_int32(void *, char *);
char *get_property_string(void *, char *);
void *memory_region_get_ram_ptr(void *);
int /*<<< orphan*/ memory_region_init_ram(void *, int /*<<< orphan*/ *, char *,
                                          int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void generic_rom_inst(void *dev, void *opaque) {
  char *name;
  char *file;
  int size;
  void *rom_dev = opaque;
  size = get_property_int32(dev, "/device_size");
  name = get_property_string(dev, "name");
  file = get_property_string(dev, "file");
  memory_region_init_ram(rom_dev, NULL, name, size, NULL);
  void *ram_ptr = memory_region_get_ram_ptr(rom_dev);
  if (ram_ptr) {
    int fsize = 0;
    FILE *fbin = fopen(file, "rb");
    if (fbin) {
      fseek(fbin, 0L, SEEK_END);
      fsize = ftell(fbin);
      fseek(fbin, 0L, SEEK_SET);
      fread(ram_ptr, fsize, 1, fbin);
      fclose(fbin);
    } else {
      fprintf(stderr, "Could not open file %s\n", file);
    }
  } else {
  }
}


x86 assembly (clang, -O0):
<generic_rom_inst>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1090 <get_property_int32@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1060 <get_property_string@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1060 <get_property_string@plt>
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x24(%rbp),%ecx
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rsi
call   10a0 <memory_region_init_ram@plt>
mov    -0x30(%rbp),%rdi
call   1050 <memory_region_get_ram_ptr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     12c2 <generic_rom_inst+0x122>
movl   $0x0,-0x3c(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12a4 <generic_rom_inst+0x104>
mov    -0x48(%rbp),%rdi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x48(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x48(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12bd <generic_rom_inst+0x11d>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd62(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12c7 <generic_rom_inst+0x127>
jmp    12c7 <generic_rom_inst+0x127>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generic_rom_inst>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe4b(%rip),%rsi
call   1090 <get_property_int32@plt>
mov    %eax,%r12d
lea    0xe49(%rip),%rsi
mov    %rbx,%rdi
call   1060 <get_property_string@plt>
mov    %rax,%rbp
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <get_property_string@plt>
mov    %rax,%r15
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
mov    %r12d,%ecx
xor    %r8d,%r8d
call   10a0 <memory_region_init_ram@plt>
mov    %r14,%rdi
call   1050 <memory_region_get_ram_ptr@plt>
test   %rax,%rax
je     126d <generic_rom_inst+0xcd>
mov    %rax,%r14
lea    0xe0c(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1276 <generic_rom_inst+0xd6>
mov    %rax,%rbx
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebp
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/device_size\00"
@.str.1 = [5 x i8] c"name\00"
@.str.2 = [5 x i8] c"file\00"
@.str.3 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.4 = [24 x i8] c"Could not open file %s\0A\00"
define void @generic_rom_inst(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %4
store i8* %12, i8** %8
%13 = load i8*, i8** %3
%14 = call i32 @get_property_int32(i8* %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %7
%15 = load i8*, i8** %3
%16 = call i8* @get_property_string(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %5
%17 = load i8*, i8** %3
%18 = call i8* @get_property_string(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %18, i8** %6
%19 = load i8*, i8** %8
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = call i32 @memory_region_init_ram(i8* %19, i32* null, i8* %20, i32 %21, i32* null)
%23 = load i8*, i8** %8
%24 = call i8* @memory_region_get_ram_ptr(i8* %23)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %52
27: ; preds = %2
store i32 0, i32* %10
%28 = load i8*, i8** %6
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %29, i32** %11
%30 = load i32*, i32** %11
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %47
32: ; preds = %27
%33 = load i32*, i32** %11
%34 = load i32, i32* @SEEK_END
%35 = call i32 @fseek(i32* %33, i64 0, i32 %34)
%36 = load i32*, i32** %11
%37 = call i32 @ftell(i32* %36)
store i32 %37, i32* %10
%38 = load i32*, i32** %11
%39 = load i32, i32* @SEEK_SET
%40 = call i32 @fseek(i32* %38, i64 0, i32 %39)
%41 = load i8*, i8** %9
%42 = load i32, i32* %10
%43 = load i32*, i32** %11
%44 = call i32 @fread(i8* %41, i32 %42, i32 1, i32* %43)
%45 = load i32*, i32** %11
%46 = call i32 @fclose(i32* %45)
br label %51
47: ; preds = %27
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %6
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %49)
br label %51
51: ; preds = %47, %32
br label %53
52: ; preds = %2
br label %53
53: ; preds = %52, %51
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/device_size\00"
@.str.1 = [5 x i8] c"name\00"
@.str.2 = [5 x i8] c"file\00"
@.str.3 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str.4 = [24 x i8] c"Could not open file %s\0A\00"
define void @generic_rom_inst(i8* %0, i8* %1) {
%3 = tail call i32 @get_property_int32(i8* %0, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @get_property_string(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i8* @get_property_string(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @memory_region_init_ram(i8* %1, i32* null, i8* %4, i32 %3, i32* null)
%7 = tail call i8* @memory_region_get_ram_ptr(i8* %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = load i32, i32* @SEEK_END
%14 = tail call i32 @fseek(i32* %10, i64 0, i32 %13)
%15 = tail call i32 @ftell(i32* %10)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @fseek(i32* %10, i64 0, i32 %16)
%18 = tail call i32 @fread(i8* %7, i32 %15, i32 1, i32* %10)
%19 = tail call i32 @fclose(i32* %10)
br label %23
20: ; preds = %9
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %23
23: ; preds = %12, %20, %2
ret void
}