// File: tomfunkhouser[P]gaps/apps/msh2prp/msh2prp.cpp
// Function: ReadMTurkLabelMapping(gaps::RNSymbolTable<MTurkLabel*>&, char const*)
static int
ReadMTurkLabelMapping(RNSymbolTable<MTurkLabel *>& labels, const char *filename)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();
  if (print_verbose) {
    printf("Reading mturk label mapping ...\n");
    fflush(stdout);
  }

  // Open file
  FILE* fp = fopen(filename, "r");
  if (!fp) {
    RNFail("Unable to open label mapping file %s\n", filename);
    return 0;
  }

  // Read keys from first line
  char key_buffer[4096];
  RNArray<char *> keys;
  if (fgets(key_buffer, 4096, fp)) {
    char *token = strtok(key_buffer, ",\t\n");
    while (token) {
      keys.Insert(token);
      token = strtok(NULL, ",\t\n");
    }
  }

  // Extract index of model_id
  int id_index = -1;
  int name_index = -1;
  int nyu40id_index = -1;
  for (int i = 0; i < keys.NEntries(); i++) {
    if (!strcmp(keys[i], "index")) id_index = i;
    else if (!strcmp(keys[i], "id")) id_index = i;
    else if (!strcmp(keys[i], "category")) name_index = i; 
    else if (!strcmp(keys[i], "nyu40id")) nyu40id_index = i; 
  }

  // Check if found key fields in header
  if ((id_index < 0) || (name_index < 0) || (nyu40id_index < 0)) {
    RNFail("Did not find index, category, and nyu40id in header of %s\n", filename);
    return 0;
  }

  // Read subsequent lines of file
  char value_buffer[4096];
  while (fgets(value_buffer, 4096, fp)) {
    // Read values
    RNArray<char *> values;
    char *token = strtok(value_buffer, ",\t\n");
    while (token) {
      values.Insert(token);
      token = strtok(NULL, ",\t\n");
    }

    // Create label
    MTurkLabel *label = new MTurkLabel();
    label->id = atoi(values[id_index]);
    label->name = RNStrdup(values[name_index]);
    label->nyu40id = atoi(values[nyu40id_index]);
    labels.Insert(label->name, label);
  }
  
  // Close file
  fclose(fp);

  // Print statistics
  if (print_verbose) {
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Labels = %d\n", labels.NEntries());
    fflush(stdout);
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x2148, %rsp           # imm = 0x2148
movq %rdi, 0x2138(%rsp)
movq %rsi, 0x2130(%rsp)
leaq 0x2120(%rsp), %rdi
callq 0xe98e0
leaq 0x2120(%rsp), %rdi
callq 0x1f280
cmpl $0x0, 0x1acb68(%rip)    # 0x1ca740
je 0x1dbf7
leaq 0x156b0c(%rip), %rdi    # 0x1746ed
movb $0x0, %al
callq 0x11090
movq 0x1a33a1(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
movq 0x2130(%rsp), %rdi
leaq 0x15de1d(%rip), %rsi    # 0x17ba23
callq 0x11800
movq %rax, 0x2118(%rsp)
cmpq $0x0, 0x2118(%rsp)
jne 0x1dc44
movq 0x2130(%rsp), %rsi
leaq 0x156ae1(%rip), %rdi    # 0x17470e
movb $0x0, %al
callq 0xedb00
movl $0x0, 0x2144(%rsp)
jmp 0x1e1ea
leaq 0x1100(%rsp), %rdi
callq 0x20f00
movq 0x2118(%rsp), %rdx
leaq 0x1110(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x11aa0
movq %rax, 0x80(%rsp)
jmp 0x1dc75
movq 0x80(%rsp), %rax
cmpq $0x0, %rax
je 0x1dcf6
leaq 0x1110(%rsp), %rdi
leaq 0x156aa2(%rip), %rsi    # 0x174734
callq 0x11a60
movq %rax, 0x10e8(%rsp)
cmpq $0x0, 0x10e8(%rsp)
je 0x1dcf4
movq 0x10e8(%rsp), %rsi
leaq 0x1100(%rsp), %rdi
callq 0x20f10
jmp 0x1dcc1
xorl %eax, %eax
movl %eax, %edi
leaq 0x156a68(%rip), %rsi    # 0x174734
callq 0x11a60
movq %rax, 0x10e8(%rsp)
jmp 0x1dc9f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10f8(%rsp)
movl %eax, 0x10f4(%rsp)
jmp 0x1e1db
jmp 0x1dcf6
movl $0xffffffff, 0x10e4(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x10e0(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x10dc(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x10d8(%rsp)
movl 0x10d8(%rsp), %eax
movl %eax, 0x7c(%rsp)
leaq 0x1100(%rsp), %rdi
callq 0x1f300
movl %eax, %ecx
movl 0x7c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1de7a
movl 0x10d8(%rsp), %esi
leaq 0x1100(%rsp), %rdi
callq 0x20f40
movq %rax, 0x70(%rsp)
jmp 0x1dd63
movq 0x70(%rsp), %rax
movq (%rax), %rdi
leaq 0x165fc3(%rip), %rsi    # 0x183d35
callq 0x11990
cmpl $0x0, %eax
jne 0x1dd8f
movl 0x10d8(%rsp), %eax
movl %eax, 0x10e4(%rsp)
jmp 0x1de62
movl 0x10d8(%rsp), %esi
leaq 0x1100(%rsp), %rdi
callq 0x20f40
movq %rax, 0x68(%rsp)
jmp 0x1ddaa
movq 0x68(%rsp), %rax
movq (%rax), %rdi
leaq 0x15737e(%rip), %rsi    # 0x175137
callq 0x11990
cmpl $0x0, %eax
jne 0x1ddd6
movl 0x10d8(%rsp), %eax
movl %eax, 0x10e4(%rsp)
jmp 0x1de60
movl 0x10d8(%rsp), %esi
leaq 0x1100(%rsp), %rdi
callq 0x20f40
movq %rax, 0x60(%rsp)
jmp 0x1ddf1
movq 0x60(%rsp), %rax
movq (%rax), %rdi
leaq 0x156938(%rip), %rsi    # 0x174738
callq 0x11990
cmpl $0x0, %eax
jne 0x1de1a
movl 0x10d8(%rsp), %eax
movl %eax, 0x10e0(%rsp)
jmp 0x1de5e
movl 0x10d8(%rsp), %esi
leaq 0x1100(%rsp), %rdi
callq 0x20f40
movq %rax, 0x58(%rsp)
jmp 0x1de35
movq 0x58(%rsp), %rax
movq (%rax), %rdi
leaq 0x1568fd(%rip), %rsi    # 0x174741
callq 0x11990
cmpl $0x0, %eax
jne 0x1de5c
movl 0x10d8(%rsp), %eax
movl %eax, 0x10dc(%rsp)
jmp 0x1de5e
jmp 0x1de60
jmp 0x1de62
jmp 0x1de64
movl 0x10d8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10d8(%rsp)
jmp 0x1dd22
cmpl $0x0, 0x10e4(%rsp)
jl 0x1de98
cmpl $0x0, 0x10e0(%rsp)
jl 0x1de98
cmpl $0x0, 0x10dc(%rsp)
jge 0x1decb
movq 0x2130(%rsp), %rsi
leaq 0x1568a2(%rip), %rdi    # 0x174749
xorl %eax, %eax
callq 0xedb00
jmp 0x1deb0
movl $0x0, 0x2144(%rsp)
movl $0x1, 0x10d4(%rsp)
jmp 0x1e1cc
jmp 0x1decd
movq 0x2118(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x11aa0
movq %rax, 0x50(%rsp)
jmp 0x1deee
movq 0x50(%rsp), %rax
cmpq $0x0, %rax
je 0x1e139
leaq 0xc0(%rsp), %rdi
callq 0x20f00
jmp 0x1df0c
leaq 0xd0(%rsp), %rdi
leaq 0x156819(%rip), %rsi    # 0x174734
callq 0x11a60
movq %rax, 0xb8(%rsp)
cmpq $0x0, 0xb8(%rsp)
je 0x1df7d
movq 0xb8(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x20f10
jmp 0x1df4a
xorl %eax, %eax
movl %eax, %edi
leaq 0x1567df(%rip), %rsi    # 0x174734
callq 0x11a60
movq %rax, 0xb8(%rsp)
jmp 0x1df28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10f8(%rsp)
movl %eax, 0x10f4(%rsp)
jmp 0x1e127
movl $0x18, %edi
callq 0x11680
movq %rax, 0x48(%rsp)
jmp 0x1df8e
movq 0x48(%rsp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movq %rax, 0xb0(%rsp)
movl 0x10e4(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x20f40
movq %rax, 0x40(%rsp)
jmp 0x1dfc4
movq 0x40(%rsp), %rax
movq (%rax), %rdi
callq 0x11a50
movl %eax, %ecx
movq 0xb0(%rsp), %rax
movl %ecx, (%rax)
movl 0x10e0(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x20f40
movq %rax, 0x38(%rsp)
jmp 0x1dff8
movq 0x38(%rsp), %rax
movq (%rax), %rdi
callq 0xed800
movq %rax, 0x30(%rsp)
jmp 0x1e00c
movq 0x30(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0x8(%rax)
movl 0x10dc(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x20f40
movq %rax, 0x28(%rsp)
jmp 0x1e038
movq 0x28(%rsp), %rax
movq (%rax), %rdi
callq 0x11a50
movl %eax, %ecx
movq 0xb0(%rsp), %rax
movl %ecx, 0x10(%rax)
movq 0x2138(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xb0(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
leaq 0x8f(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x11bc0
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
leaq 0x90(%rsp), %rdi
callq 0x20fa0
jmp 0x1e09b
movq 0x10(%rsp), %rdi
movq 0xb0(%rsp), %rdx
leaq 0x90(%rsp), %rsi
callq 0x20f60
jmp 0x1e0b7
leaq 0x90(%rsp), %rdi
callq 0x11400
leaq 0x8f(%rsp), %rdi
callq 0x11710
leaq 0xc0(%rsp), %rdi
callq 0x21060
jmp 0x1decd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10f8(%rsp)
movl %eax, 0x10f4(%rsp)
jmp 0x1e11a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x10f8(%rsp)
movl %eax, 0x10f4(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x11400
leaq 0x8f(%rsp), %rdi
callq 0x11710
leaq 0xc0(%rsp), %rdi
callq 0x21060
jmp 0x1e1db
movq 0x2118(%rsp), %rdi
callq 0x11670
jmp 0x1e148
cmpl $0x0, 0x1ac5f1(%rip)    # 0x1ca740
je 0x1e1b6
leaq 0x2120(%rsp), %rdi
callq 0xe99a0
movsd %xmm0, 0x8(%rsp)
jmp 0x1e166
movsd 0x8(%rsp), %xmm0
leaq 0x156099(%rip), %rdi    # 0x17420c
movb $0x1, %al
callq 0x11090
jmp 0x1e17c
movq 0x2138(%rsp), %rdi
callq 0x21070
movl %eax, 0x4(%rsp)
jmp 0x1e18f
movl 0x4(%rsp), %esi
leaq 0x1565ea(%rip), %rdi    # 0x174784
xorl %eax, %eax
callq 0x11090
jmp 0x1e1a3
movq 0x1a2de6(%rip), %rax    # 0x1c0f90
movq (%rax), %rdi
callq 0x117f0
jmp 0x1e1b4
jmp 0x1e1b6
movl $0x1, 0x2144(%rsp)
movl $0x1, 0x10d4(%rsp)
leaq 0x1100(%rsp), %rdi
callq 0x21060
jmp 0x1e1ea
leaq 0x1100(%rsp), %rdi
callq 0x21060
jmp 0x1e1f9
movl 0x2144(%rsp), %eax
addq $0x2148, %rsp           # imm = 0x2148
retq
movq 0x10f8(%rsp), %rdi
callq 0x11bb0
nopw %cs:(%rax,%rax)