// File: /workspace/github_c_makefile_star2_O1/deplinenoise[P]vlink/targets.c
// Function: writesection
lword writesection(struct GlobalVars *gv,uint8_t *dest,struct Reloc *r,lword v)
/* Write 'v' into the bit-field defined by the relocation type in 'r'.
   Do range checks first, depending on the reloc type.
   Returns 0 on success or the masked and normalized value which failed
   on the range check. */
{
  struct RelocInsert *ri;

  if (r->rtype == R_NONE)
    return 0;

  if (ri = r->insert) {
    lword lastval = 0;  /* add reloc-addends to this value */
    uint8_t t = r->rtype;
    bool signedval = t==R_PC||t==R_GOTPC||t==R_GOTOFF||t==R_PLTPC||t==R_PLTOFF
                     ||t==R_SD||t==R_SD2||t==R_SD21||t==R_MOSDREL;
    bool be = gv->endianess == _BIG_ENDIAN_;

    do {
      /* first mask and normalize the value, then check if it
         fits into the bitfield */
      lword mask = ri->mask;
      lword insval = v & mask;
      uint8_t *p = dest + (ri->bpos >> 3);
      int bpos = (int)ri->bpos & 7;
      int bsiz = (int)ri->bsiz;
      int n;

      insval >>= lshiftcnt(mask);  /* normalize according mask */
      if (mask>=0 && signedval)
        insval = sign_extend(insval,bsiz);
      insval += lastval;
      lastval = insval;
      if (!checkrange(insval,signedval,bsiz))
        return insval;  /* range check failed on 'insval' */

      /* insert into bitfield, obeying target endianess */
      if (n = (bpos + bsiz + 7) >> 3) {
        if (be) {  /* write for big-endian target */
          int sh = (8 - ((bpos + bsiz) & 7)) & 7;
          uint8_t m = 0xff << sh;  /* mask for LSB */
          uint8_t b;

          insval <<= sh;  /* shift to fit bitfield */
          p += n;
          while (n--) {
            if (!n)
              m &= (1 << (8-(bpos&7))) - 1;  /* apply mask for MSB */

            b = *(--p) & ~m;
            *p = b | ((uint8_t)insval & m);
            insval >>= 8;
            m = 0xff;
          }
        }
        else {  /* write for little-endian target */
          uint8_t m = 0xff << bpos;  /* mask for LSB */
          uint8_t b;

          insval <<= bpos;  /* shift to fit bitfield */
          while (n--) {
            if (!n && ((bpos+bsiz)&7)!=0)
              m &= (1 << ((bpos+bsiz)&7)) - 1;  /* apply mask for MSB */

            b = *p & ~m;
            *p++ = b | ((uint8_t)insval & m);
            insval >>= 8;
            m = 0xff;
          }
        }
      }
    }
    while (ri = ri->next);

    return 0;
  }

  ierror("writesection(): Reloc (type=%d offs=%lu add=%lld) without "
         " insert field def.\n",(int)r->rtype,r->offset,r->addend);
  return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, 0x20(%rsp)
movq %rsi, 0x10(%rsp)
movzbl 0x38(%rdx), %ecx
testl %ecx, %ecx
je 0xb78d
movq 0x30(%rdx), %rbx
testq %rbx, %rbx
je 0xb791
cmpb $0x23, %cl
setb %al
movabsq $0x700000734, %rdx      # imm = 0x700000734
shrq %cl, %rdx
andb %al, %dl
movzbl %dl, %eax
movl %eax, 0x4(%rsp)
movb 0x202(%rdi), %al
movb %al, 0x3(%rsp)
movq 0x10(%rsp), %rax
decq %rax
movq %rax, 0x18(%rsp)
xorl %r15d, %r15d
movq 0x10(%rbx), %r14
movq %r14, %r13
andq 0x20(%rsp), %r13
movzwl 0x8(%rbx), %ebp
movzwl 0xa(%rbx), %r12d
movq %r14, %rdi
callq 0x6e9c
movl %eax, %ecx
sarq %cl, %r13
cmpl $0x0, 0x4(%rsp)
je 0xb653
testq %r14, %r14
js 0xb653
movq %r13, %rdi
movl %r12d, %esi
callq 0x6f17
movq %rax, %r13
addq %r13, %r15
movq %r15, %rdi
movl 0x4(%rsp), %esi
movl %r12d, %edx
callq 0x68e4
movq %r15, %rcx
testl %eax, %eax
je 0xb778
movl %ebp, %esi
andl $0x7, %esi
movl %esi, %edx
addl %r12d, %edx
je 0xb773
shrl $0x3, %ebp
leal (%rsi,%r12), %edi
addl $0x7, %edi
cmpb $0x1, 0x3(%rsp)
jne 0xb701
shrl $0x3, %edi
negb %dl
andb $0x7, %dl
movq %r15, %r8
movl %edx, %ecx
shlq %cl, %r8
movl $0xff, %r9d
shll %cl, %r9d
movl %edi, %edx
movb $0x8, %cl
subb %sil, %cl
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
shll %cl, %esi
notb %sil
addq 0x18(%rsp), %rbp
addq %rdx, %rbp
movzbl %sil, %ecx
movq 0x8(%rsp), %r10
cmpl $0x1, %edi
movl $0xff, %edx
cmovel %ecx, %edx
andb %r9b, %dl
movb (%rbp), %sil
movl %r8d, %r9d
xorb %sil, %r9b
andb %dl, %r9b
xorb %sil, %r9b
sarq $0x8, %r8
movb %r9b, (%rbp)
decq %rbp
movb $-0x1, %r9b
decl %edi
jne 0xb6cc
movq %r10, %rcx
jmp 0xb778
movq %r15, %r8
movl %esi, %ecx
shlq %cl, %r8
movl $0xff, %r9d
shll %cl, %r9d
addq 0x10(%rsp), %rbp
andl $0x7, %edx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl %edx, %ecx
shll %cl, %esi
notb %sil
cmpl $0x1, %edx
movl $0x0, %ecx
sbbl %ecx, %ecx
orb %sil, %cl
shrl $0x3, %edi
leaq -0x1(%rdi), %rdx
xorl %esi, %esi
movl $0xff, %r14d
cmpl %esi, %edx
movzbl %cl, %r10d
cmovnel %r14d, %r10d
andb %r9b, %r10b
movb (%rbp,%rsi), %r9b
movl %r8d, %r11d
xorb %r9b, %r11b
andb %r10b, %r11b
xorb %r9b, %r11b
movb %r11b, (%rbp,%rsi)
sarq $0x8, %r8
incq %rsi
movb $-0x1, %r9b
cmpl %esi, %edi
jne 0xb742
movq 0x8(%rsp), %rcx
testl %eax, %eax
je 0xb7b6
movq (%rbx), %rbx
movq %rcx, 0x8(%rsp)
testq %rbx, %rbx
jne 0xb617
xorl %ecx, %ecx
jmp 0xb7b6
movq 0x20(%rdx), %rax
movq 0x28(%rdx), %r8
leaq 0x16fb4(%rip), %rdi     # 0x22754
movl %ecx, %esi
movq %rax, %rdx
movq %r8, %rcx
xorl %eax, %eax
callq 0x52b0
movq $-0x1, %rcx
movq %rcx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq