// File: /workspace/github_c_makefile_star2_O3/deplinenoise[P]vlink/t_elf32ppcbe.c
// Function: ppc32_reloc_vlink2elf
static uint8_t ppc32_reloc_vlink2elf(struct Reloc *r)
/* Try to map vlink-internal representation to a valid ELF reloc type */
{
  lword m32 = 0xffffffff;
  struct RelocInsert *ri;
  uint8_t rt = R_NONE;

  if (ri = r->insert) {
    int pos = (int)ri->bpos;
    int size = (int)ri->bsiz;
    lword mask = ri->mask;
    struct RelocInsert *ri2 = ri->next;
    int f = 0;  /* field type */

    r->offset += (unsigned long)pos >> 3;
    pos &= 7;
    ri->bpos = (uint16_t)pos;

    if (pos==0 && (size==16 || size==32) && mask==-1 && ri2==NULL) {
      f = FSTD;
    }
    else if (pos==6 && size==24 && (mask&0x3ffffff)==0x3fffffc && ri2==NULL) {
      f = FB24;
    }
    else if (pos==0 && size==16 && (mask&m32)==0xffff && ri2==NULL) {
      f = FLO;
    }
    else if (pos==0 && size==16 && (mask&m32)==0xffff0000 && ri2==NULL) {
      f = FHI;
    }
    else if (pos==0 && size==16 && (mask&m32)==0xffff0000 && ri2!=NULL) {
      if (ri2->bpos==0 && ri2->bsiz==16 &&
          (ri2->mask&m32)==0x8000 && ri2->next==NULL)
        f = FHA;
    }
    else if (pos==0 && size==14 && (mask&0xffff)==0xfffc && ri2==NULL) {
      f = FB14;
      r->offset -= 2;
      ri->bpos += 16;
    }
    else if (pos==0 && size==30 && ri2==NULL) {
      f = FW30;
    }
    else
      f = 0;  /* no usable insert-field description found */

    switch (r->rtype) {

      case R_ABS:
        switch (f) {
          case FSTD: rt = size==32 ? R_PPC_ADDR32 : R_PPC_ADDR16; break;
          case FLO: rt = R_PPC_ADDR16_LO; break;
          case FHI: rt = R_PPC_ADDR16_HI; break;
          case FHA: rt = R_PPC_ADDR16_HA; break;
          case FB24: rt = R_PPC_ADDR24; break;
          case FB14: rt = R_PPC_ADDR14; break;
          case FW30: rt = R_PPC_ADDR30; break;
        }
        break;

      case R_PC:
        switch (f) {
          case FSTD:
            if (size == 32) {
              rt = R_PPC_REL32;
            }
            else if ((r->addend & 3) == 0) {
              r->offset -= 2;
              ri->bpos += 16;
              rt = R_PPC_REL14;
            }
            break;
          case FB24: rt = R_PPC_REL24; break;
          case FB14: rt = R_PPC_REL14; break;  /* @@@ BRTAKEN/BRNTAKEN */
        }
        break;

      case R_GOT:
        switch (f) {
          case FSTD:
            if (size == 16)
              rt = R_PPC_GOT16;
            break;
          case FLO: rt = R_PPC_GOT16_LO; break;
          case FHI: rt = R_PPC_GOT16_HI; break;
          case FHA: rt = R_PPC_GOT16_HA; break;
        }
        break;

      case R_PLT:
        switch (f) {
          case FSTD:
            if (size == 32)
              rt = R_PPC_PLT32;
            break;
          case FLO: rt = R_PPC_PLT16_LO; break;
          case FHI: rt = R_PPC_PLT16_HI; break;
          case FHA: rt = R_PPC_PLT16_HA; break;
        }
        break;

      case R_PLTPC:
        switch (f) {
          case FSTD:
            if (size == 32)
              rt = R_PPC_PLTREL32;
            break;
          case FB24: rt = R_PPC_PLTREL24; break;
        }
        break;

      case R_SECOFF:
        switch (f) {
          case FSTD:
            if (size == 16)
              rt = R_PPC_SECTOFF;
            break;
          case FLO: rt = R_PPC_SECTOFF_LO; break;
          case FHI: rt = R_PPC_SECTOFF_HI; break;
          case FHA: rt = R_PPC_SECTOFF_HA; break;
        }
        break;
      
      case R_COPY:
        rt = R_PPC_COPY;
        break;
      
      case R_GLOBDAT:
        rt = R_PPC_GLOB_DAT;
        break;
      
      case R_JMPSLOT:
        rt = R_PPC_JMP_SLOT;
        break;
      
      case R_LOADREL:
        rt = R_PPC_RELATIVE;
        break;
      
      case R_LOCALPC:
        if (f == FB24)
          rt = R_PPC_LOCAL24PC;
        break;

      case R_SD:
        if (f==FSTD && size==16)
          rt = R_PPC_SDAREL16;
        break;

       case R_UABS:
         rt = size==32 ? R_PPC_UADDR32 : R_PPC_UADDR16;
         break;

      /* Embedded and OS-specific relocs */

      case R_SD2:
        if (f==FSTD && size==16)
          rt = R_PPC_EMB_SDA2REL;
        break;

      case R_SD21:
        if (f==FSTD && size==16) {
          r->offset -= 2;
          ri->bpos += 16;
          rt = R_PPC_EMB_SDA21;
        }
        break;

      case R_MOSDREL:
        switch (f) {
          case FSTD:
            if (size == 16)
              rt = R_PPC_MORPHOS_DREL;
            break;
          case FLO: rt = R_PPC_MORPHOS_DREL_LO; break;
          case FHI: rt = R_PPC_MORPHOS_DREL_HI; break;
          case FHA: rt = R_PPC_MORPHOS_DREL_HA; break;
        }
        break;

      case R_AOSBREL:
        switch (f) {
          case FSTD:
            if (size == 16)
              rt = R_PPC_AMIGAOS_BREL;
            break;
          case FLO: rt = R_PPC_AMIGAOS_BREL_LO; break;
          case FHI: rt = R_PPC_AMIGAOS_BREL_HI; break;
          case FHA: rt = R_PPC_AMIGAOS_BREL_HA; break;
        }
        break;
    }
  }

  return (rt);
}

// Assembly:
pushq %r14
pushq %rbx
movq 0x30(%rdi), %rdx
testq %rdx, %rdx
je 0x1a09d
movzwl 0x8(%rdx), %r8d
movzwl 0xa(%rdx), %ecx
movq (%rdx), %rax
movq 0x10(%rdx), %r9
movl %r8d, %esi
shrl $0x3, %esi
addq 0x20(%rdi), %rsi
movq %rsi, 0x20(%rdi)
andw $0x7, %r8w
movw %r8w, 0x8(%rdx)
movzwl %r8w, %r11d
je 0x19d82
movl %r11d, %r8d
xorl $0x6, %r8d
movl %ecx, %r10d
xorl $0x18, %r10d
orw %r8w, %r10w
jne 0x19e82
andl $0x3ffffff, %r9d        # imm = 0x3FFFFFF
xorq $0x3fffffc, %r9         # imm = 0x3FFFFFC
orq %rax, %r9
jne 0x19e82
movl $0x5, %r9d
movb $0x17, %r11b
movw $0x16, %r8w
xorl %r10d, %r10d
jmp 0x19eab
leal -0x10(%rcx), %r8d
testl $0xffef, %r8d           # imm = 0xFFEF
sete %r8b
cmpq $-0x1, %r9
sete %r10b
andb %r8b, %r10b
testq %rax, %rax
sete %bl
movw $0x10, %r8w
testb %bl, %r10b
je 0x19dba
movl $0x1, %r9d
movb $0x1, %r10b
jmp 0x19ea8
cmpl $0xe, %ecx
je 0x19de2
cmpl $0x10, %ecx
jne 0x19e82
movl %r9d, %r9d
movq %r9, %r10
xorq $0xffff, %r10           # imm = 0xFFFF
orq %rax, %r10
jne 0x19e11
movl $0x2, %r9d
jmp 0x19e25
movzwl %r9w, %r8d
xorq $0xfffc, %r8            # imm = 0xFFFC
orq %rax, %r8
jne 0x19e82
addq $-0x2, %rsi
movq %rsi, 0x20(%rdi)
movw $0x10, 0x8(%rdx)
movl $0x6, %r9d
movw $0x20, %r8w
jmp 0x19e25
movl $0xffff0000, %r10d      # imm = 0xFFFF0000
xorq %r9, %r10
orq %rax, %r10
jne 0x19e2a
movl $0x3, %r9d
xorl %r10d, %r10d
jmp 0x19ea8
cmpl $0xffff0000, %r9d       # imm = 0xFFFF0000
sete %r9b
testq %rax, %rax
setne %r10b
andb %r9b, %r10b
cmpb $0x1, %r10b
jne 0x19e82
xorl %r10d, %r10d
cmpw $0x0, 0x8(%rax)
jne 0x1a0e7
xorl %r10d, %r10d
cmpw $0x10, 0xa(%rax)
jne 0x1a0e7
xorl %r10d, %r10d
cmpl $0x8000, 0x10(%rax)     # imm = 0x8000
jne 0x1a0e7
xorl %r9d, %r9d
cmpq $0x0, (%rax)
sete %r9b
shll $0x2, %r9d
jmp 0x19e25
movl %ecx, %r8d
xorl $0x1e, %r8d
xorl %r10d, %r10d
testq %rax, %rax
movl $0x7, %r9d
cmovnel %r10d, %r9d
orw %r11w, %r8w
cmovnel %r10d, %r9d
orl $0x10, %r11d
movl %r11d, %r8d
xorl %r11d, %r11d
movzbl 0x38(%rdi), %ebx
xorl %eax, %eax
decl %ebx
cmpl $0x22, %ebx
ja 0x1a09f
leaq 0x9c05(%rip), %r14      # 0x23ac8
movslq (%r14,%rbx,4), %rbx
addq %r14, %rbx
jmpq *%rbx
movl %r9d, %eax
leaq 0x9cce(%rip), %rdx      # 0x23ba4
movslq (%rdx,%rax,4), %rax
addq %rdx, %rax
jmpq *%rax
cmpl $0x20, %ecx
setne %al
addb %al, %al
incb %al
jmp 0x1a09f
xorl %edx, %edx
cmpw $0x10, %cx
movl $0x6c, %eax
cmovnel %edx, %eax
testb %r10b, %r10b
cmovel %edx, %eax
jmp 0x1a09f
movb $0x13, %al
jmp 0x1a09f
decl %r9d
cmpl $0x3, %r9d
ja 0x1a09d
leaq 0x9c62(%rip), %rax      # 0x23b84
movslq (%rax,%r9,4), %rdx
addq %rax, %rdx
jmpq *%rdx
xorl %edx, %edx
cmpl $0x20, %ecx
movl $0x1b, %eax
jmp 0x1a098
decl %r9d
cmpl $0x3, %r9d
ja 0x1a09d
leaq 0x9c26(%rip), %rax      # 0x23b74
movslq (%rax,%r9,4), %rdx
addq %rax, %rdx
jmpq *%rdx
xorl %edx, %edx
cmpl $0x10, %ecx
movl $0x21, %eax
jmp 0x1a098
cmpl $0x6, %r9d
je 0x19fa2
cmpl $0x5, %r9d
je 0x1a0a7
cmpl $0x1, %r9d
jne 0x1a09d
movb $0x1a, %al
cmpl $0x20, %ecx
je 0x1a09f
testb $0x3, 0x28(%rdi)
jne 0x1a09d
addq $-0x2, %rsi
movq %rsi, 0x20(%rdi)
movw %r8w, 0x8(%rdx)
movb $0xb, %al
jmp 0x1a09f
cmpl $0x5, %r9d
je 0x1a0a3
cmpl $0x1, %r9d
jne 0x1a09d
xorl %edx, %edx
cmpl $0x20, %ecx
movl $0x1c, %eax
jmp 0x1a098
movb $0x15, %al
jmp 0x1a09f
movb $0x14, %al
jmp 0x1a09f
decl %r9d
cmpl $0x3, %r9d
ja 0x1a09d
leaq 0x9ba6(%rip), %rax      # 0x23b94
movslq (%rax,%r9,4), %rdx
addq %rax, %rdx
jmpq *%rdx
xorl %edx, %edx
cmpl $0x10, %ecx
movl $0xe, %eax
jmp 0x1a098
cmpw $0x10, %cx
sete %al
andb %al, %r10b
shlb $0x5, %r10b
movl %r10d, %eax
jmp 0x1a09f
cmpl $0x20, %ecx
sete %al
xorb $0x19, %al
jmp 0x1a09f
cmpw $0x10, %cx
sete %al
andb %al, %r10b
cmpb $0x1, %r10b
jne 0x1a09d
addq $-0x2, %rsi
movq %rsi, 0x20(%rdi)
movw %r8w, 0x8(%rdx)
movb $0x6d, %al
jmp 0x1a09f
decl %r9d
cmpl $0x3, %r9d
ja 0x1a09d
leaq 0x9b0d(%rip), %rax      # 0x23b64
movslq (%rax,%r9,4), %rdx
addq %rax, %rdx
jmpq *%rdx
xorl %edx, %edx
cmpl $0x10, %ecx
movl $0xc8, %eax
jmp 0x1a098
movb $0x16, %al
jmp 0x1a09f
movl %r11d, %eax
jmp 0x1a09f
decl %r9d
cmpl $0x3, %r9d
ja 0x1a09d
leaq 0x9acf(%rip), %rax      # 0x23b54
movslq (%rax,%r9,4), %rdx
addq %rax, %rdx
jmpq *%rdx
xorl %edx, %edx
cmpl $0x10, %ecx
movl $0xd2, %eax
cmovnel %edx, %eax
jmp 0x1a09f
xorl %eax, %eax
popq %rbx
popq %r14
retq
movb $0x12, %al
jmp 0x1a09f
movb $0xa, %al
jmp 0x1a09f
movb $0x1e, %al
jmp 0x1a09f
movb $0x23, %al
jmp 0x1a09f
movb $0x1f, %al
jmp 0x1a09f
movb $0x24, %al
jmp 0x1a09f
movb $0x1d, %al
jmp 0x1a09f
movb $0x22, %al
jmp 0x1a09f
movb $0x10, %al
jmp 0x1a09f
movb $0x11, %al
jmp 0x1a09f
movb $-0x36, %al
jmp 0x1a09f
movb $0xf, %al
jmp 0x1a09f
movb $-0x35, %al
jmp 0x1a09f
movb $-0x37, %al
jmp 0x1a09f
movb $-0x2c, %al
jmp 0x1a09f
movb $-0x2b, %al
jmp 0x1a09f
movb $-0x2d, %al
jmp 0x1a09f
xorl %r11d, %r11d
xorl %r9d, %r9d
jmp 0x19eab
movb $0x6, %al
jmp 0x1a09f
movb $0x4, %al
jmp 0x1a09f
movb $0x5, %al
jmp 0x1a09f
movb $0x25, %al
jmp 0x1a09f
movb $0x2, %al
jmp 0x1a09f
movb $0x7, %al
jmp 0x1a09f