// File: /workspace/github_c_makefile_star2_O2/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 0xb137
movq 0x30(%rdx), %rbx
testq %rbx, %rbx
je 0xb13c
cmpb $0x23, %cl
setb %al
movabsq $0x700000734, %rdx      # imm = 0x700000734
shrq %cl, %rdx
andb %al, %dl
movzbl %dl, %eax
movl %eax, 0xc(%rsp)
movb 0x202(%rdi), %al
movb %al, 0xb(%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 0x6a69
movl %eax, %ecx
sarq %cl, %r13
cmpl $0x0, 0xc(%rsp)
je 0xb01e
testq %r14, %r14
js 0xb01e
movq %r13, %rdi
movl %r12d, %esi
callq 0x6ad8
movq %rax, %r13
addq %r13, %r15
movq %r15, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
callq 0x6593
testl %eax, %eax
je 0xb15e
movl %ebp, %edx
andl $0x7, %edx
movl %edx, %eax
addl %r12d, %eax
movl $0xff, %r11d
je 0xb12b
shrl $0x3, %ebp
leal (%rdx,%r12), %esi
addl $0x7, %esi
shrl $0x3, %esi
cmpb $0x1, 0xb(%rsp)
jne 0xb0c4
negb %al
andb $0x7, %al
movl $0xff, %r8d
movl %eax, %ecx
shll %cl, %r8d
movq %r15, %rdi
shlq %cl, %rdi
movb $0x8, %cl
subb %dl, %cl
pushq $-0x1
popq %rdx
shll %cl, %edx
movl %esi, %eax
notb %dl
addq 0x18(%rsp), %rbp
movzbl %dl, %ecx
testl %eax, %eax
je 0xb12b
cmpl $0x1, %eax
movl $0xff, %edx
cmovel %ecx, %edx
andb %r8b, %dl
movb (%rbp,%rax), %sil
movl %edi, %r8d
xorb %sil, %r8b
andb %dl, %r8b
xorb %sil, %r8b
movb %r8b, (%rbp,%rax)
sarq $0x8, %rdi
decq %rax
movb $-0x1, %r8b
jmp 0xb08c
movl $0xff, %r8d
movl %edx, %ecx
shll %cl, %r8d
movq %r15, %rdi
shlq %cl, %rdi
addq 0x10(%rsp), %rbp
andl $0x7, %eax
pushq $-0x1
popq %rdx
movl %eax, %ecx
shll %cl, %edx
cmpl $0x1, %eax
movl $0x0, %eax
sbbl %eax, %eax
notb %dl
orb %dl, %al
leaq -0x1(%rsi), %rcx
xorl %edx, %edx
cmpl %edx, %esi
je 0xb12b
cmpl %edx, %ecx
movzbl %al, %r9d
cmovnel %r11d, %r9d
andb %r9b, %r8b
movb (%rbp,%rdx), %r9b
movl %edi, %r10d
xorb %r9b, %r10b
andb %r8b, %r10b
xorb %r9b, %r10b
movb %r10b, (%rbp,%rdx)
sarq $0x8, %rdi
incq %rdx
movb $-0x1, %r8b
jmp 0xb0f8
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0xafe2
xorl %r15d, %r15d
jmp 0xb15e
movq 0x20(%rdx), %rax
movq 0x28(%rdx), %r8
leaq 0x15609(%rip), %rdi     # 0x20754
movl %ecx, %esi
movq %rax, %rdx
movq %r8, %rcx
xorl %eax, %eax
callq 0x6b2c
pushq $-0x1
popq %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq