// File: /workspace/github_c_makefile_star2_O3/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 0xb6aa
movq 0x30(%rdx), %rbx
testq %rbx, %rbx
je 0xb6af
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 0x6e27
movl %eax, %ecx
sarq %cl, %r13
cmpl $0x0, 0xc(%rsp)
je 0xb590
testq %r14, %r14
js 0xb590
movq %r13, %rdi
movl %r12d, %esi
callq 0x6ea5
movq %rax, %r13
addq %r13, %r15
movq %r15, %rdi
movl 0xc(%rsp), %esi
movl %r12d, %edx
callq 0x68b0
testl %eax, %eax
je 0xb6d4
movl %ebp, %edx
andl $0x7, %edx
movl %edx, %eax
addl %r12d, %eax
movl $0xff, %r11d
je 0xb69e
shrl $0x3, %ebp
leal (%rdx,%r12), %esi
addl $0x7, %esi
cmpb $0x1, 0xb(%rsp)
jne 0xb634
shrl $0x3, %esi
negb %al
andb $0x7, %al
movq %r15, %rdi
movl %eax, %ecx
shlq %cl, %rdi
movl $0xff, %r8d
shll %cl, %r8d
movl %esi, %eax
movb $0x8, %cl
subb %dl, %cl
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
shll %cl, %edx
notb %dl
addq 0x18(%rsp), %rbp
addq %rax, %rbp
movzbl %dl, %eax
cmpl $0x1, %esi
movl $0xff, %ecx
cmovel %eax, %ecx
andb %r8b, %cl
movb (%rbp), %dl
movl %edi, %r8d
xorb %dl, %r8b
andb %cl, %r8b
xorb %dl, %r8b
sarq $0x8, %rdi
movb %r8b, (%rbp)
decq %rbp
movb $-0x1, %r8b
decl %esi
jne 0xb603
jmp 0xb69e
movq %r15, %rdi
movl %edx, %ecx
shlq %cl, %rdi
movl $0xff, %r8d
shll %cl, %r8d
addq 0x10(%rsp), %rbp
andl $0x7, %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl %eax, %ecx
shll %cl, %edx
notb %dl
cmpl $0x1, %eax
movl $0x0, %eax
sbbl %eax, %eax
orb %dl, %al
shrl $0x3, %esi
leaq -0x1(%rsi), %rcx
xorl %edx, %edx
cmpl %edx, %ecx
movzbl %al, %r9d
cmovnel %r11d, %r9d
andb %r8b, %r9b
movb (%rbp,%rdx), %r8b
movl %edi, %r10d
xorb %r8b, %r10b
andb %r9b, %r10b
xorb %r8b, %r10b
movb %r10b, (%rbp,%rdx)
sarq $0x8, %rdi
incq %rdx
movb $-0x1, %r8b
cmpl %edx, %esi
jne 0xb66d
movq (%rbx), %rbx
testq %rbx, %rbx
jne 0xb554
xorl %r15d, %r15d
jmp 0xb6d4
movq 0x20(%rdx), %rax
movq 0x28(%rdx), %r8
leaq 0x17086(%rip), %rdi     # 0x22744
movl %ecx, %esi
movq %rax, %rdx
movq %r8, %rcx
xorl %eax, %eax
callq 0x52b0
movq $-0x1, %r15
movq %r15, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq