// File: /workspace/github_c_makefile_star2_O2/rchiossi[P]dexterity/lib/dex_string.c
// Function: dx_string_add_stringid
void dx_string_add_stringid(Dex* dx, uint32_t index) {
  DexStringIdItem* sid;
  int32_t delta;
  unsigned int i;

  if (dx == NULL || index >= dx->header->string_ids_size) return; 

  sid = (DexStringIdItem*) malloc_s(sizeof(DexStringIdItem));

  sid->meta.corrupted = false;

  delta = sizeof(DexStringIdItem) - sizeof(Metadata);

  if (index == dx->header->string_ids_size) {
    sid->meta.offset = dx->string_ids[index-1]->meta.offset;
    sid->meta.offset += sizeof(DexStringIdItem) - sizeof(Metadata);
  } else {
    sid->meta.offset = dx->string_ids[index]->meta.offset;
  }

  //Fix references
  dx_shift_offset(dx,sid->meta.offset,delta);
  dx_shift_stringid(dx,index,1);

  if (index == dx->header->string_ids_size) {
    sid->string_data_off  = dx->string_ids[index-1]->string_data_off;
    sid->string_data_off += dx->string_data_list[index-1]->size.size;
    sid->string_data_off += ul128toui(dx->string_data_list[index-1]->size) + 1;
  } else {
    sid->string_data_off = dx->string_ids[index]->string_data_off;
  }  

  //Add new stringid to the string_ids list
  if (dx->header->string_ids_size >= dx->meta.string_ids_alloc)
    DX_REALLOC_LIST(DexStringIdItem*,dx->string_ids,dx->meta.string_ids_alloc);
  
  for (i=dx->header->string_ids_size; i>index; i--)
    dx->string_ids[i] = dx->string_ids[i-1];

  dx->header->string_ids_size++;  

  dx->string_ids[index] = sid;

  //Fix Maps
  for (i=0; i<dx->map_list->size; i++) {
    if (dx->map_list->list[i]->type == 0x0001) {
      dx->map_list->list[i]->size++;
    }    
  }

  //Fix header
  if (index == 0) 
    dx->header->string_ids_off = sid->meta.offset;

  //Fix filesize
  dx->header->file_size += delta;
}

// Assembly:
testq %rdi, %rdi
je 0x8182
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r14
movq (%rdi), %rax
cmpl %esi, 0x40(%rax)
jbe 0x8177
pushq $0xc
popq %rdi
callq 0x46a0
movq %rax, %r15
movb $0x0, (%rax)
movq (%r14), %rcx
movq 0x10(%r14), %rax
movl %ebx, %r12d
cmpl %ebx, 0x40(%rcx)
jne 0x8075
leal -0x1(%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %esi
addl $0x4, %esi
jmp 0x807c
movq (%rax,%r12,8), %rax
movl 0x4(%rax), %esi
movl %esi, 0x4(%r15)
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x4610
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x42d0
movq (%r14), %rax
movq 0x10(%r14), %rcx
movl 0x40(%rax), %eax
cmpl %ebx, %eax
jne 0x80df
leal -0x1(%rbx), %eax
movq (%rcx,%rax,8), %rcx
movq 0x40(%r14), %rdx
movq (%rdx,%rax,8), %rax
movl 0x10(%rax), %edx
addl 0x8(%rcx), %edx
movl %edx, 0x8(%r15)
movq 0x8(%rax), %rdi
movl 0x10(%rax), %esi
callq 0x4180
movl 0x8(%r15), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x8(%r15)
movq (%r14), %rax
movl 0x40(%rax), %eax
jmp 0x80ea
movq (%rcx,%r12,8), %rcx
movl 0x8(%rcx), %ecx
movl %ecx, 0x8(%r15)
movl %eax, %eax
movq 0x98(%r14), %rsi
cmpq %rax, %rsi
ja 0x812d
movq 0x10(%r14), %rdi
shlq $0x4, %rsi
callq 0x4640
movq %rax, 0x10(%r14)
shlq 0x98(%r14)
movq (%r14), %rax
movl 0x40(%rax), %eax
jmp 0x812d
movq 0x10(%r14), %rcx
leaq -0x1(%rax), %rdx
movl %edx, %esi
movq (%rcx,%rsi,8), %rsi
movq %rsi, (%rcx,%rax,8)
movq %rdx, %rax
cmpq %r12, %rax
ja 0x8118
movq (%r14), %rax
incl 0x40(%rax)
movq 0x10(%r14), %rax
movq %r15, (%rax,%r12,8)
movq 0x8(%r14), %rax
movl 0x8(%rax), %ecx
xorl %edx, %edx
cmpq %rcx, %rdx
jae 0x8165
movq 0x10(%rax), %rsi
movq (%rsi,%rdx,8), %rsi
cmpw $0x1, 0x8(%rsi)
jne 0x8160
incl 0xc(%rsi)
incq %rdx
jmp 0x8149
movq (%r14), %rax
testl %ebx, %ebx
jne 0x8173
movl 0x4(%r15), %ecx
movl %ecx, 0x44(%rax)
addl $0x4, 0x28(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq