// File: /workspace/github_c_makefile_star2_O1/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 0x8500
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %r14d
movq %rdi, %rbx
movq (%rdi), %rax
cmpl %esi, 0x40(%rax)
jbe 0x84f5
movl $0xc, %edi
callq 0x4600
movq %rax, %r15
movb $0x0, (%rax)
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
movl %r14d, %r12d
cmpl %r14d, 0x40(%rcx)
jne 0x83e3
leal -0x1(%r14), %ecx
movq (%rax,%rcx,8), %rax
movl 0x4(%rax), %eax
addl $0x4, %eax
jmp 0x83ea
movq (%rax,%r12,8), %rax
movl 0x4(%rax), %eax
movl %eax, 0x4(%r15)
movl 0x4(%r15), %esi
movq %rbx, %rdi
movl $0x4, %edx
callq 0x4580
movq %rbx, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0x4280
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpl %r14d, 0x40(%rcx)
jne 0x8450
leal -0x1(%r14), %ecx
movq (%rax,%rcx,8), %rax
movl 0x8(%rax), %eax
movl %eax, 0x8(%r15)
movq 0x40(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
addl 0x10(%rcx), %eax
movl %eax, 0x8(%r15)
movq 0x8(%rcx), %rdi
movl 0x10(%rcx), %esi
callq 0x4170
movl 0x8(%r15), %ecx
addl %ecx, %eax
incl %eax
jmp 0x8457
movq (%rax,%r12,8), %rax
movl 0x8(%rax), %eax
movl %eax, 0x8(%r15)
movq (%rbx), %rax
movq 0x98(%rbx), %rsi
movl 0x40(%rax), %eax
cmpq %rax, %rsi
ja 0x8485
movq 0x10(%rbx), %rdi
shlq $0x4, %rsi
callq 0x45b0
movq %rax, 0x10(%rbx)
shlq 0x98(%rbx)
movq (%rbx), %rax
movl 0x40(%rax), %eax
cmpl %r14d, %eax
jbe 0x84a9
movq 0x10(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdx
movq %rdx, (%rcx,%rax,8)
leaq -0x1(%rax), %rcx
movq %rcx, %rax
cmpq %r12, %rcx
ja 0x8490
movq (%rbx), %rax
incl 0x40(%rax)
movq 0x10(%rbx), %rax
movq %r15, (%rax,%r12,8)
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
testq %rax, %rax
je 0x84df
movq 0x10(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpw $0x1, 0x8(%rsi)
jne 0x84d7
incl 0xc(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x84c9
testl %r14d, %r14d
jne 0x84ee
movl 0x4(%r15), %eax
movq (%rbx), %rcx
movl %eax, 0x44(%rcx)
movq (%rbx), %rax
addl $0x4, 0x28(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq