// File: /workspace/github_c_makefile_star2_O0/hamsternz[P]emulate-risc-v/rom.c
// Function: attempt_to_read
static void attempt_to_read(struct region *r) {
  uint32_t *data;
  char *fname;
  FILE *f;
  int a = 0;
  int i;

  data = (uint32_t *)(r->data);

  if(asprintf(&fname, "rom_%08x.img",r->base) < 1) {
    display_log("Unable to print file name to memory region");
    return;
  }

  if(fname == NULL) {
    display_log("Unable to allocate memory to read region");
    return;
  }

  f = fopen(fname,"rb");
  if( f == NULL) {
    char buffer[100];
    sprintf(buffer, "File '%s' not present\n",fname);
    display_log(buffer);
    free(fname);
    return;
  }
  free(fname);

  while(1) {
    int c;
    uint32_t d = 0;
 
    c = fgetc(f);
    while(c == '\n' || c == '\r') {
      c = fgetc(f);
    }

    if(c == EOF) {
       printf("End of file at address %i\n", a);
       break;
    }

    if(c >= '0' && c <= '9') {
      d = c - '0';
    } else if(c >= 'A' && c <= 'F') {
      d = c - 'A' + 10;
    } else if(c >= 'a' && c <= 'f') {
      d = c - 'a' + 10;
    } else if(c != ' ') {
      fprintf(stderr, "unexpected characters in file\n");
      return;
    }

    for(i = 0; i < 7; i++) {
      c = fgetc(f);
      if(c >= '0' && c <= '9') {
	d *= 16;
        d += c - '0';
      } else if(c >= 'A' && c <= 'F') {
	d *= 16;
        d += c - 'A' + 10;
      } else if(c >= 'a' && c <= 'f') {
	d *= 16;
        d += c - 'a' + 10;
      } else if(c == ' ' || c == '\t' || c == '\n') {
	if(a*4+3 >= r->size) {
          display_log("Too much data for memory region");
          return;
	}
	break;
      } else {
        display_log("unexpected characters in file");
        return;
      }
    }

    data[a] = d;
    a++;

    while(c != '\n' && c != EOF) {
      c = fgetc(f);
    }
  } 

  fclose(f);
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0xa0(%rsp)
movl $0x0, 0x84(%rsp)
movq 0xa0(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rax
movl 0x8(%rax), %edx
leaq 0x90(%rsp), %rdi
leaq 0x4074(%rip), %rsi      # 0xb6cc
movb $0x0, %al
callq 0x2190
cmpl $0x1, %eax
jge 0x7675
leaq 0x3978(%rip), %rdi      # 0xafe3
callq 0x6b20
jmp 0x7980
cmpq $0x0, 0x90(%rsp)
jne 0x7691
leaq 0x3987(%rip), %rdi      # 0xb00e
callq 0x6b20
jmp 0x7980
movq 0x90(%rsp), %rdi
leaq 0x3997(%rip), %rsi      # 0xb037
callq 0x21a0
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x76ef
leaq 0x10(%rsp), %rdi
movq 0x90(%rsp), %rdx
leaq 0x396e(%rip), %rsi      # 0xb03a
movb $0x0, %al
callq 0x21e0
leaq 0x10(%rsp), %rdi
callq 0x6b20
movq 0x90(%rsp), %rdi
callq 0x2040
jmp 0x7980
movq 0x90(%rsp), %rdi
callq 0x2040
movl $0x0, 0x8(%rsp)
movq 0x88(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
movb $0x1, %al
cmpl $0xa, 0xc(%rsp)
movb %al, 0x7(%rsp)
je 0x772e
cmpl $0xd, 0xc(%rsp)
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x7738
jmp 0x774b
movq 0x88(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
jmp 0x7715
cmpl $-0x1, 0xc(%rsp)
jne 0x776c
movl 0x84(%rsp), %esi
leaq 0x38f1(%rip), %rdi      # 0xb051
movb $0x0, %al
callq 0x20b0
jmp 0x7973
cmpl $0x30, 0xc(%rsp)
jl 0x7787
cmpl $0x39, 0xc(%rsp)
jg 0x7787
movl 0xc(%rsp), %eax
subl $0x30, %eax
movl %eax, 0x8(%rsp)
jmp 0x77ed
cmpl $0x41, 0xc(%rsp)
jl 0x77a5
cmpl $0x46, 0xc(%rsp)
jg 0x77a5
movl 0xc(%rsp), %eax
subl $0x41, %eax
addl $0xa, %eax
movl %eax, 0x8(%rsp)
jmp 0x77eb
cmpl $0x61, 0xc(%rsp)
jl 0x77c3
cmpl $0x66, 0xc(%rsp)
jg 0x77c3
movl 0xc(%rsp), %eax
subl $0x61, %eax
addl $0xa, %eax
movl %eax, 0x8(%rsp)
jmp 0x77e9
cmpl $0x20, 0xc(%rsp)
je 0x77e7
movq 0x6827(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x3891(%rip), %rsi      # 0xb06c
movb $0x0, %al
callq 0x2130
jmp 0x7980
jmp 0x77e9
jmp 0x77eb
jmp 0x77ed
movl $0x0, 0x80(%rsp)
cmpl $0x7, 0x80(%rsp)
jge 0x7910
movq 0x88(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
cmpl $0x30, 0xc(%rsp)
jl 0x7844
cmpl $0x39, 0xc(%rsp)
jg 0x7844
movl 0x8(%rsp), %eax
shll $0x4, %eax
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
subl $0x30, %eax
addl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x78f8
cmpl $0x41, 0xc(%rsp)
jl 0x7874
cmpl $0x46, 0xc(%rsp)
jg 0x7874
movl 0x8(%rsp), %eax
shll $0x4, %eax
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
subl $0x41, %eax
addl $0xa, %eax
addl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x78f6
cmpl $0x61, 0xc(%rsp)
jl 0x78a1
cmpl $0x66, 0xc(%rsp)
jg 0x78a1
movl 0x8(%rsp), %eax
shll $0x4, %eax
movl %eax, 0x8(%rsp)
movl 0xc(%rsp), %eax
subl $0x61, %eax
addl $0xa, %eax
addl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x78f4
cmpl $0x20, 0xc(%rsp)
je 0x78b6
cmpl $0x9, 0xc(%rsp)
je 0x78b6
cmpl $0xa, 0xc(%rsp)
jne 0x78e3
movl 0x84(%rsp), %eax
shll $0x2, %eax
addl $0x3, %eax
movq 0xa0(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jb 0x78e1
leaq 0x37b4(%rip), %rdi      # 0xb08b
callq 0x6b20
jmp 0x7980
jmp 0x7910
leaq 0x37c1(%rip), %rdi      # 0xb0ab
callq 0x6b20
jmp 0x7980
jmp 0x78f6
jmp 0x78f8
jmp 0x78fa
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x77f8
movl 0x8(%rsp), %edx
movq 0x98(%rsp), %rax
movslq 0x84(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
xorl %eax, %eax
cmpl $0xa, 0xc(%rsp)
movb %al, 0x6(%rsp)
je 0x7951
cmpl $-0x1, 0xc(%rsp)
setne %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
testb $0x1, %al
jne 0x795b
jmp 0x796e
movq 0x88(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
jmp 0x7938
jmp 0x76fc
movq 0x88(%rsp), %rdi
callq 0x2080
addq $0xa8, %rsp
retq
nopl (%rax,%rax)