// File: rockytriton[P]dreamsys_emu_gb/src/op_handler.cpp
// Function: dsemu::cpu::setFlags(unsigned char, unsigned char, bool, bool)
void setFlags(byte first, byte second, bool add, bool withCarry) {
    if (add) {
        unsigned int a = first + second + (withCarry ? getFlag(FlagC) : 0);
        byte cf = getFlag(FlagC);
        //setFlag(FlagZ, (a & 0xFF) == 0);
        setFlag(FlagZ, !(a & 0xFF));
        //cout << "AF: 2: " << Byte(regAF.lo) << endl;
        //setFlag(FlagC, a > 0xFF);
        setFlag(FlagC, a >= 0x100);
        //cout << "AF: 3: " << Byte(regAF.lo) << endl;
        setFlag(FlagN, false);
        //cout << "AF: 4: " << Byte(regAF.lo) << endl;
        setFlag(FlagH, ((first&0xF) + (second&0xF) + (withCarry ? cf : 0)) >= 0x10);
        //cout << "AF: 5: " << Byte(regAF.lo) << " - " << Short(a) << endl;
        //setFlag(FlagH, (first & 0xF) + (second & 0xF) + (withCarry && getFlag(FlagC)) > 0xF);
    } else {
        int a = first - second - (withCarry && getFlag(FlagC));
        byte cf = getFlag(FlagC);
        setFlag(FlagZ, (a & 0xFF) == 0);
        setFlag(FlagC, a < 0);
        setFlag(FlagN, true);
        setFlag(FlagH, (first & 0xF) - (second & 0xF) - (withCarry ? cf : 0) < 0);
    }

    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %cl, %al
movb %dl, %cl
movb %sil, %dl
movb %dil, %sil
movb %sil, -0x1(%rbp)
movb %dl, -0x2(%rbp)
andb $0x1, %cl
movb %cl, -0x3(%rbp)
andb $0x1, %al
movb %al, -0x4(%rbp)
testb $0x1, -0x3(%rbp)
je 0x1bc88
movzbl -0x1(%rbp), %eax
movzbl -0x2(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x18(%rbp)
testb $0x1, -0x4(%rbp)
je 0x1bbd5
movl $0x4, %edi
callq 0x1dac0
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1bbdc
xorl %eax, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1bbdc
movl -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x8(%rbp)
movl $0x4, %edi
callq 0x1dac0
andb $0x1, %al
movb %al, -0x9(%rbp)
movl -0x8(%rbp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x7, %edi
callq 0x1da80
cmpl $0x100, -0x8(%rbp)      # imm = 0x100
setae %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x4, %edi
callq 0x1da80
movl $0x6, %edi
xorl %esi, %esi
callq 0x1da80
movzbl -0x1(%rbp), %eax
andl $0xf, %eax
movzbl -0x2(%rbp), %ecx
andl $0xf, %ecx
addl %ecx, %eax
movl %eax, -0x20(%rbp)
testb $0x1, -0x4(%rbp)
je 0x1bc5e
movzbl -0x9(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x1bc65
xorl %eax, %eax
movl %eax, -0x24(%rbp)
jmp 0x1bc65
movl -0x20(%rbp), %eax
movl -0x24(%rbp), %ecx
addl %ecx, %eax
cmpl $0x10, %eax
setge %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x5, %edi
callq 0x1da80
jmp 0x1bd58
movzbl -0x1(%rbp), %eax
movzbl -0x2(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
xorl %eax, %eax
testb $0x1, -0x4(%rbp)
movb %al, -0x25(%rbp)
je 0x1bcad
movl $0x4, %edi
callq 0x1dac0
movb %al, -0x25(%rbp)
movl -0x2c(%rbp), %eax
movb -0x25(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
subl %ecx, %eax
movl %eax, -0x10(%rbp)
movl $0x4, %edi
callq 0x1dac0
andb $0x1, %al
movb %al, -0x11(%rbp)
movl -0x10(%rbp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
sete %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x7, %edi
callq 0x1da80
cmpl $0x0, -0x10(%rbp)
setl %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x4, %edi
callq 0x1da80
movl $0x6, %edi
movl $0x1, %esi
callq 0x1da80
movzbl -0x1(%rbp), %eax
andl $0xf, %eax
movzbl -0x2(%rbp), %ecx
andl $0xf, %ecx
subl %ecx, %eax
movl %eax, -0x30(%rbp)
testb $0x1, -0x4(%rbp)
je 0x1bd33
movzbl -0x11(%rbp), %eax
movl %eax, -0x34(%rbp)
jmp 0x1bd3a
xorl %eax, %eax
movl %eax, -0x34(%rbp)
jmp 0x1bd3a
movl -0x30(%rbp), %eax
movl -0x34(%rbp), %ecx
subl %ecx, %eax
cmpl $0x0, %eax
setl %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x5, %edi
callq 0x1da80
addq $0x40, %rsp
popq %rbp
retq
nop