// File: /workspace/github_c_makefile_star2_O3/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 0x8662
pushq %r15
pushq %r14
pushq %rbx
movl %esi, %ebx
movq %rdi, %r14
movq (%rdi), %rax
cmpl %esi, 0x40(%rax)
jbe 0x865d
movl $0xc, %edi
callq 0x4600
movq %rax, %r15
movb $0x0, (%rax)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpl %ebx, 0x40(%rcx)
jne 0x8542
leal -0x1(%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %esi
addl $0x4, %esi
jmp 0x854b
movl %ebx, %ecx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %esi
movl %esi, 0x4(%r15)
movq %r14, %rdi
movl $0x4, %edx
callq 0x4580
movq %r14, %rdi
movl %ebx, %esi
movl $0x1, %edx
callq 0x4280
movq (%r14), %rax
movq 0x10(%r14), %rdx
movl 0x40(%rax), %ecx
cmpl %ebx, %ecx
jne 0x85b2
leal -0x1(%rbx), %eax
movq (%rdx,%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 0x4170
movl 0x8(%r15), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x8(%r15)
movq (%r14), %rax
movl 0x40(%rax), %ecx
jmp 0x85bf
movl %ebx, %esi
movq (%rdx,%rsi,8), %rdx
movl 0x8(%rdx), %edx
movl %edx, 0x8(%r15)
movl %ecx, %edx
movq 0x98(%r14), %rsi
cmpq %rdx, %rsi
ja 0x85eb
movq 0x10(%r14), %rdi
shlq $0x4, %rsi
callq 0x45b0
movq %rax, 0x10(%r14)
shlq 0x98(%r14)
movq (%r14), %rax
movl 0x40(%rax), %ecx
cmpl %ebx, %ecx
jbe 0x8614
movl %ecx, %eax
movl %ebx, %edx
movq 0x10(%r14), %rcx
movq -0x8(%rcx,%rax,8), %rsi
movq %rsi, (%rcx,%rax,8)
leaq -0x1(%rax), %rcx
movq %rcx, %rax
cmpq %rdx, %rcx
ja 0x85f3
movq (%r14), %rax
movl 0x40(%rax), %ecx
jmp 0x8616
movl %ebx, %edx
incl %ecx
movl %ecx, 0x40(%rax)
movq 0x10(%r14), %rax
movq %r15, (%rax,%rdx,8)
movq 0x8(%r14), %rcx
movl 0x8(%rcx), %eax
testq %rax, %rax
je 0x864b
movq 0x10(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpw $0x1, 0x8(%rsi)
jne 0x8643
incl 0xc(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x8635
movq (%r14), %rax
testl %ebx, %ebx
jne 0x8659
movl 0x4(%r15), %ecx
movl %ecx, 0x44(%rax)
addl $0x4, 0x28(%rax)
popq %rbx
popq %r14
popq %r15
retq