// File: /workspace/github_c_makefile_star2_O0/hamsternz[P]emulate-risc-v/ram.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, "ram_%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) {
    fprintf(stderr, "File '%s' not present\n",fname);
    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 $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl $0x0, 0x14(%rsp)
movq 0x30(%rsp), %rax
movq 0x38(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rax
movl 0x8(%rax), %edx
leaq 0x20(%rsp), %rdi
leaq 0x5f53(%rip), %rsi      # 0xafd6
movb $0x0, %al
callq 0x2190
cmpl $0x1, %eax
jge 0x50a0
leaq 0x5f4d(%rip), %rdi      # 0xafe3
callq 0x6b20
jmp 0x535b
cmpq $0x0, 0x20(%rsp)
jne 0x50b9
leaq 0x5f5f(%rip), %rdi      # 0xb00e
callq 0x6b20
jmp 0x535b
movq 0x20(%rsp), %rdi
leaq 0x5f72(%rip), %rsi      # 0xb037
callq 0x21a0
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jne 0x5103
movq 0x8f1a(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movq 0x20(%rsp), %rdx
leaq 0x5f4d(%rip), %rsi      # 0xb03a
movb $0x0, %al
callq 0x2130
movq 0x20(%rsp), %rdi
callq 0x2040
jmp 0x535b
movq 0x20(%rsp), %rdi
callq 0x2040
movl $0x0, 0x8(%rsp)
movq 0x18(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
movb $0x1, %al
cmpl $0xa, 0xc(%rsp)
movb %al, 0x7(%rsp)
je 0x513c
cmpl $0xd, 0xc(%rsp)
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x5146
jmp 0x5156
movq 0x18(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
jmp 0x5123
cmpl $-0x1, 0xc(%rsp)
jne 0x5174
movl 0x14(%rsp), %esi
leaq 0x5ee9(%rip), %rdi      # 0xb051
movb $0x0, %al
callq 0x20b0
jmp 0x5351
cmpl $0x30, 0xc(%rsp)
jl 0x518f
cmpl $0x39, 0xc(%rsp)
jg 0x518f
movl 0xc(%rsp), %eax
subl $0x30, %eax
movl %eax, 0x8(%rsp)
jmp 0x51f5
cmpl $0x41, 0xc(%rsp)
jl 0x51ad
cmpl $0x46, 0xc(%rsp)
jg 0x51ad
movl 0xc(%rsp), %eax
subl $0x41, %eax
addl $0xa, %eax
movl %eax, 0x8(%rsp)
jmp 0x51f3
cmpl $0x61, 0xc(%rsp)
jl 0x51cb
cmpl $0x66, 0xc(%rsp)
jg 0x51cb
movl 0xc(%rsp), %eax
subl $0x61, %eax
addl $0xa, %eax
movl %eax, 0x8(%rsp)
jmp 0x51f1
cmpl $0x20, 0xc(%rsp)
je 0x51ef
movq 0x8e1f(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x5e89(%rip), %rsi      # 0xb06c
movb $0x0, %al
callq 0x2130
jmp 0x535b
jmp 0x51f1
jmp 0x51f3
jmp 0x51f5
movl $0x0, 0x10(%rsp)
cmpl $0x7, 0x10(%rsp)
jge 0x52fd
movq 0x18(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
cmpl $0x30, 0xc(%rsp)
jl 0x5243
cmpl $0x39, 0xc(%rsp)
jg 0x5243
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 0x52eb
cmpl $0x41, 0xc(%rsp)
jl 0x5270
cmpl $0x46, 0xc(%rsp)
jg 0x5270
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 0x52e9
cmpl $0x61, 0xc(%rsp)
jl 0x529d
cmpl $0x66, 0xc(%rsp)
jg 0x529d
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 0x52e7
cmpl $0x20, 0xc(%rsp)
je 0x52b2
cmpl $0x9, 0xc(%rsp)
je 0x52b2
cmpl $0xa, 0xc(%rsp)
jne 0x52d9
movl 0x14(%rsp), %eax
shll $0x2, %eax
addl $0x3, %eax
movq 0x30(%rsp), %rcx
cmpl 0xc(%rcx), %eax
jb 0x52d7
leaq 0x5dbe(%rip), %rdi      # 0xb08b
callq 0x6b20
jmp 0x535b
jmp 0x52fd
leaq 0x5dcb(%rip), %rdi      # 0xb0ab
callq 0x6b20
jmp 0x535b
jmp 0x52e9
jmp 0x52eb
jmp 0x52ed
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x51fd
movl 0x8(%rsp), %edx
movq 0x28(%rsp), %rax
movslq 0x14(%rsp), %rcx
movl %edx, (%rax,%rcx,4)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
xorl %eax, %eax
cmpl $0xa, 0xc(%rsp)
movb %al, 0x6(%rsp)
je 0x5332
cmpl $-0x1, 0xc(%rsp)
setne %al
movb %al, 0x6(%rsp)
movb 0x6(%rsp), %al
testb $0x1, %al
jne 0x533c
jmp 0x534c
movq 0x18(%rsp), %rdi
callq 0x2100
movl %eax, 0xc(%rsp)
jmp 0x5319
jmp 0x510d
movq 0x18(%rsp), %rdi
callq 0x2080
addq $0x38, %rsp
retq