// File: /workspace/github_c_makefile_star2_O1/Nico01[P]obj2asm/odisasm.c
// Function: mod_reg
int mod_reg( byte, text, class )
    uchar   byte;
    char            *text;
    int             class;
{
    char            *opcode;
    int             dir;
    int             size;
    int             reg_size;
    int             size_needed;
    int             type;
    int             method;
    int             group;
    dword   mod_reg;
    uchar   mod;
    uchar   r_m;
    uchar   reg;
    int             additional;
    char            mem_text[50];
    char            reg_text[50];
    char            third_text[50];
    char            operands[80];
    int             ref_mode;

    if ( get_checkc( &mod_reg ) ) {
        return( 0 );
    }

    mod = (uchar)(((int)mod_reg & 0xC0) >> 6);
    reg = (uchar)(((int)mod_reg & 0x38) >> 3);
    r_m = (uchar)(((int)mod_reg & 0x07) >> 0);

    dir         = modrm_class[class].dir;
    size        = modrm_class[class].size;
    method      = modrm_class[class].method;
    group       = modrm_class[class].group;
    size_needed = modrm_class[class].size_needed;
    type        = modrm_class[class].type;

    ref_mode = 0;

    if ( group == 6 ) {
        if ( mod == 3 ) {
            if ( reg == 0 || reg == 1 || reg == 2 || reg == 3 ) {
                addr_large = FALSE;         /* 286 only instructions */
                size_large = FALSE;
            }
        }
        type = WORD_PTR;
        size_needed = TRUE;
    }

    reg_size = size;                /* 386 Segments effect register names */
    if ( reg_size == 1 && size_large ) {
        reg_size = 2;
    }
    if ( type == UNKNOWN ) {
        type = reg_size_to_type( reg_size );
    }

    if ( group == 0 ) {
        opcode = text;
    } else {
        opcode = op_grp[group-1][reg];
    }
    if ( strlen(opcode) == 0 ) {
        return( 0 );
    }

    if ( class == 26 ) {
        reg_size = 1;
    }

    if ( byte == 0x62 || byte == 0x63 ) {
        if ( byte == 0x63 ) {
            type = WORD_PTR;
        }
        size_needed = TRUE;     /* LAR, LSL, BOUND, ARPL need size */
    }
    if ( byte == 0xC4 || byte == 0xC5
           || byte == 0xB2 || byte == 0xB4 || byte == 0xB5 ) {
        size_needed = TRUE;
    }

    if ( group == 3 ) {         /* Group 3 has a test instruction which    */
        if ( reg == 0 ) {       /* needs an immediate value (byte or word) */
            method = 5 + size;  /* the rest of group 3 needs nothing       */
            dir = 0;
            size_needed = TRUE;
        } else {
            method = 0;
            if ( reg == 2 || reg == 3 ) {
                size_needed = FALSE;
            } else {
                size_needed = TRUE;
            }
        }
    }

    if ( group == 5 ) {                 /* Group 5 has call far and jump far */
        if ( reg == 3 || reg == 5 ) {   /* instructions which are dwords     */
                                        /* the rest of group 5 is words      */
            if ( size_large ) {
                type = FWORD_PTR;
            } else {
                type = DWORD_PTR;
            }
            size_needed = TRUE;
        } else {
            if ( reg == 0 || reg == 1 ) {
                size_needed = TRUE;
            }
        }
    }

    if ( group == 7 ) {                 /* Group 7 has mostly FWORD sized    */
        if ( reg == 4 || reg == 6 ) {   /* EAs, but these two are WORD sized */
            type = WORD_PTR;
        }
    }

    if ( dir == 0 && mod == 3 && (class == 0 || class == 1) ) {
        /*
        ** Ambiguous!   eg. OBJ 20F8 = and al,bh => MASM 22C7
        ** This is because MASM prefers the first operand to be
        ** a register and the other operand to be a mem/reg.
        */
        return(0);
    }

    mem_text[0] = '\0';
    additional = do_mod_rm( mem_text, mod, r_m, type, size_needed, ref_mode );
    if ( additional == -1 ) {
        return( 0 );
    }

    switch( method ) {
        case 0:         /* No additional information (one operand only) */
            break;
        case 1:         /* Other operand is a register */
                        /* LES,LDS,BOUND,LSS,LFS,LGS, ARPL instructions */
                        /* revert to Word */
            if ( byte == 0xC4 || byte == 0xC5 || byte == 0x62 || byte == 0x63
                   || byte == 0xB2 || byte == 0xB4 || byte == 0xB5 ) {
                reg_size = 1;
            }

            strcpy( reg_text, regs[reg_size][reg] );
            break;
        case 2:         /* Other operand is a segment register */
            if ( reg > 5 ) {    /* 0=ES,1=CS,2=SS,3=DS,4=FS,5=GS */
                return( 0 );
            }
            strcpy( reg_text, sregs[reg] );
            break;
        case 3:         /* Other operand is 1 */
            strcpy( reg_text, "1" );
            break;
        case 4:         /* Other operand is register CL */
            strcpy( reg_text, "cl" );
            break;
        case 5:         /* Immediate byte value */
            if ( byte_immed(reg_text, additional,FALSE) ) {
                return( 0 );
            }
            additional++;
            break;
        case 6:         /* Immediate word value (could be fixupp) */
            if ( word_immed(reg_text, additional) ) {
                return( 0 );
            }
            if ( size_large ) {
                additional += 4;
            } else {
                additional += 2;
            }
            break;
        case 7:         /* Immediate signed byte value */
            if ( byte_immed(reg_text,additional,TRUE) ) {
                return( 0 );
            }
            additional++;
            break;
        case 8:         /* IMUL (3 operands [reg,mem,immed word]) */
            strcpy( reg_text, regs[reg_size][reg] );
            if ( word_immed(third_text, additional) ) {
                return(0);
            }
            additional += 2;
            break;
        case 9:         /* IMUL (3 operands [reg,mem,immed signed byte]) */
            strcpy( reg_text, regs[reg_size][reg] );
            if ( byte_immed(third_text, additional,TRUE) ) {
                return(0);
            }
            additional += 1;
            break;
        case 10:        /* Control Register (CR0...) */
            strcpy( reg_text, cr_regs[reg] );
            break;
        case 11:        /* Debug Register (DR0...) */
            strcpy( reg_text, dr_regs[reg] );
            break;
        case 12:
            strcpy( reg_text, tr_regs[reg] );
            break;
        case 13:
            strcpy( reg_text, regs[reg_size][reg] );
            strcpy( third_text, "cl" );
            break;
    }

    if ( dir & 0x01 ) {
        sprintf( operands, dir_fmt[dir], reg_text, mem_text, third_text );
    } else {
        sprintf( operands, dir_fmt[dir], mem_text, reg_text, third_text );
    }

    adjust_assumes();
    instr_opcode( opcode );
    instr_operand( operands );

    return( 2+additional );
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movl %edx, %r15d
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x68(%rsp), %rdi
callq 0x9787
xorl %r8d, %r8d
testl %eax, %eax
je 0xb281
movl %r8d, %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, 0x8(%rsp)
movb 0x68(%rsp), %r10b
movl %r10d, %r9d
shrb $0x6, %r9b
movl %r10d, %ebx
shrb $0x3, %bl
andb $0x7, %bl
movslq %r15d, %rax
leaq (%rax,%rax,2), %rcx
leaq 0xe407(%rip), %rdx      # 0x196b0
movl 0x4(%rdx,%rcx,8), %ebp
movslq 0xc(%rdx,%rcx,8), %r11
cmpq $0x6, %r11
jne 0xb2ea
cmpb $0x3, %r9b
sete %al
cmpb $0x4, %bl
setb %sil
andb %al, %sil
movl $0x1, %r13d
movl $0x4, %eax
cmpb $0x1, %sil
jne 0xb2f3
xorl %esi, %esi
movl %esi, 0x11ba6(%rip)     # 0x1ce88
movl %esi, 0x11b9c(%rip)     # 0x1ce84
jmp 0xb2f3
movl 0x10(%rdx,%rcx,8), %r13d
movl 0x14(%rdx,%rcx,8), %eax
movl (%rdx,%rcx,8), %edi
cmpl $0x0, 0x11b87(%rip)     # 0x1ce84
movl 0x8(%rdx,%rcx,8), %ecx
movq %rcx, 0x18(%rsp)
movl $0x2, %r12d
cmovel %ebp, %r12d
cmpl $0x1, %ebp
cmovnel %ebp, %r12d
testl %eax, %eax
jne 0xb35c
movb %bl, 0x17(%rsp)
movl %r14d, 0x2c(%rsp)
movl %edi, 0x24(%rsp)
movl %r12d, %edi
movl %r15d, 0x28(%rsp)
movl %r9d, %r15d
movl %r10d, %ebx
movq %r11, %r14
callq 0xf61e
movq %r14, %r11
movl %ebx, %r10d
movl %r15d, %r9d
movl 0x28(%rsp), %r15d
xorl %r8d, %r8d
movl 0x24(%rsp), %edi
movl 0x2c(%rsp), %r14d
movb 0x17(%rsp), %bl
testl %r11d, %r11d
je 0xb37f
movq %r11, %rcx
shlq $0x6, %rcx
leaq 0xe061(%rip), %rdx      # 0x193d0
addq %rcx, %rdx
movzbl %bl, %ecx
movq -0x40(%rdx,%rcx,8), %rcx
movq %rcx, 0x8(%rsp)
movq 0x8(%rsp), %rcx
cmpb $0x0, (%rcx)
je 0xb26c
cmpl $0x1a, %r15d
movl $0x1, %ecx
cmovel %ecx, %r12d
movl %r14d, %edx
andb $-0x2, %dl
cmpb $0x63, %r14b
movl $0x4, %esi
cmovel %esi, %eax
cmpb $-0x4c, %dl
cmovel %ecx, %r13d
cmpb $-0x4e, %r14b
cmovel %ecx, %r13d
cmpb $-0x3c, %dl
cmovel %ecx, %r13d
cmpb $0x62, %dl
cmovel %ecx, %r13d
cmpl $0x3, %r11d
jne 0xb400
testb %bl, %bl
je 0xb3ee
movl %r10d, %ecx
andb $0x30, %cl
xorl %r13d, %r13d
cmpb $0x10, %cl
setne %r13b
movq $0x0, 0x18(%rsp)
jmp 0xb400
addl $0x5, %ebp
xorl %edi, %edi
movl $0x1, %r13d
movl %ebp, %ecx
movq %rcx, 0x18(%rsp)
cmpl $0x5, %r11d
jne 0xb436
cmpb $0x5, %bl
je 0xb413
movzbl %bl, %ecx
cmpl $0x3, %ecx
jne 0xb42a
cmpl $0x1, 0x11a6a(%rip)     # 0x1ce84
movl $0x6, %eax
sbbl $0x0, %eax
movl $0x1, %r13d
jmp 0xb436
cmpb $0x2, %bl
movl $0x1, %ecx
cmovbl %ecx, %r13d
cmpl $0x1, %r15d
ja 0xb44a
testl %edi, %edi
jne 0xb44a
cmpb $0x3, %r9b
je 0xb26c
movl %edi, %ebp
movl %r10d, %edx
andb $0x7, %dl
andb $0x28, %r10b
cmpb $0x20, %r10b
movl $0x4, %ecx
cmovnel %eax, %ecx
cmpl $0x7, %r11d
cmovnel %eax, %ecx
leaq 0xb0(%rsp), %rdi
movb $0x0, (%rdi)
movzbl %r9b, %esi
movzbl %dl, %edx
movl %r13d, %r8d
callq 0xaf2d
cmpl $-0x1, %eax
je 0xb4eb
movl %eax, %r15d
movq 0x18(%rsp), %rcx
decl %ecx
cmpl $0xc, %ecx
ja 0xb62d
leaq 0x7bb2(%rip), %rax      # 0x13054
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movzbl %r14b, %eax
leal -0xb2(%rax), %ecx
cmpl $0x13, %ecx
ja 0xb4c4
movl $0xc000d, %edx          # imm = 0xC000D
btl %ecx, %edx
jb 0xb4cc
addl $-0x62, %eax
cmpl $0x2, %eax
jae 0xb4d2
movl $0x1, %r12d
movslq %r12d, %rax
shlq $0x6, %rax
leaq 0xdc30(%rip), %rcx      # 0x19110
addq %rax, %rcx
movzbl %bl, %eax
jmp 0xb61f
xorl %r8d, %r8d
jmp 0xb26c
movzbl %bl, %eax
leaq 0xddf3(%rip), %rcx      # 0x192f0
jmp 0xb61f
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
xorl %edx, %edx
callq 0xb0f3
xorl %r8d, %r8d
testl %eax, %eax
jne 0xb26c
jmp 0xb6ea
movslq %r12d, %rax
shlq $0x6, %rax
leaq 0xdbe1(%rip), %rcx      # 0x19110
addq %rax, %rcx
movzbl %bl, %eax
movq (%rcx,%rax,8), %rsi
leaq 0x30(%rsp), %rdi
callq 0x9070
movw $0x6c63, 0x70(%rsp)     # imm = 0x6C63
movb $0x0, 0x72(%rsp)
jmp 0xb62d
movzbl %bl, %eax
leaq 0xdd12(%rip), %rcx      # 0x19270
jmp 0xb61f
movw $0x31, 0x30(%rsp)
jmp 0xb62d
movb $0x0, 0x32(%rsp)
movw $0x6c63, 0x30(%rsp)     # imm = 0x6C63
jmp 0xb62d
movslq %r12d, %rax
shlq $0x6, %rax
leaq 0xdb82(%rip), %rcx      # 0x19110
addq %rax, %rcx
movzbl %bl, %eax
movq (%rcx,%rax,8), %rsi
leaq 0x30(%rsp), %rdi
callq 0x9070
leaq 0x70(%rsp), %rdi
movl %r15d, %esi
callq 0xb176
testl %eax, %eax
movl $0x0, %r8d
jne 0xb26c
jmp 0xb6f2
cmpb $0x5, %bl
movl $0x0, %r8d
ja 0xb26c
movzbl %bl, %eax
leaq 0xdc35(%rip), %rcx      # 0x19210
jmp 0xb61f
leaq 0x30(%rsp), %rdi
movl %r15d, %esi
callq 0xb176
testl %eax, %eax
movl $0x0, %r8d
jne 0xb26c
cmpl $0x0, 0x11885(%rip)     # 0x1ce84
je 0xb6f2
addl $0x4, %r15d
jmp 0xb62d
leaq 0x30(%rsp), %rdi
jmp 0xb6cf
movzbl %bl, %eax
leaq 0xdc91(%rip), %rcx      # 0x192b0
movq (%rcx,%rax,8), %rsi
leaq 0x30(%rsp), %rdi
callq 0x9070
movq 0x8(%rsp), %rbx
movslq %ebp, %rax
leaq 0xe044(%rip), %rcx      # 0x19680
movq (%rcx,%rax,8), %rsi
leaq 0xf0(%rsp), %rdi
testb $0x1, %bpl
jne 0xb65d
leaq 0xb0(%rsp), %rdx
leaq 0x30(%rsp), %rcx
jmp 0xb66a
leaq 0x30(%rsp), %rdx
leaq 0xb0(%rsp), %rcx
leaq 0x70(%rsp), %r8
xorl %eax, %eax
callq 0x91b0
callq 0xf919
movq %rbx, %rdi
callq 0x97c1
leaq 0x118de(%rip), %rax     # 0x1cf68
cmpl $0x3, (%rax)
jne 0xb69c
leaq 0xf0(%rsp), %rdi
callq 0xc9d9
addl $0x2, %r15d
movl %r15d, %r8d
jmp 0xb26c
movslq %r12d, %rax
shlq $0x6, %rax
leaq 0xda5a(%rip), %rcx      # 0x19110
addq %rax, %rcx
movzbl %bl, %eax
movq (%rcx,%rax,8), %rsi
leaq 0x30(%rsp), %rdi
callq 0x9070
leaq 0x70(%rsp), %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0xb0f3
testl %eax, %eax
movl $0x0, %r8d
jne 0xb26c
incl %r15d
jmp 0xb62d
addl $0x2, %r15d
jmp 0xb62d