// File: /workspace/github_cpp_makefile_star2_O3/Finsenty54[P]fast_collision/my_first_try.cpp
// Function: find_collision(unsigned int const*, unsigned int*, unsigned int*, unsigned int*, unsigned int*, bool)
void find_collision(const uint32 IV[], uint32 msg1block0[], uint32 msg1block1[], uint32 msg2block0[], uint32 msg2block1[], bool verbose)
{
	if (verbose)
		cout << "Generating first block: " << flush;
	find_block0(msg1block0, IV);

	uint32 IHV[4] = { IV[0], IV[1], IV[2], IV[3] };
    uint32 IHV_TE[4] = { IV[0], IV[1], IV[2], IV[3] };
    //计算得到新的IHV
	md5_compress(IHV, msg1block0);

	if (verbose)
		cout << endl << "Generating second block: " << flush;
	find_block1(msg1block1, IHV);

    md5_compress(IHV, msg1block1);

	for (int t = 0; t < 16; ++t)
	{
		msg2block0[t] = msg1block0[t];
		msg2block1[t] = msg1block1[t];
	}

	//加上差分，得到m2
	msg2block0[4] += 1 << 31; msg2block0[11] += 1 << 15; msg2block0[14] += 1 << 31;
	msg2block1[4] += 1 << 31; msg2block1[11] -= 1 << 15; msg2block1[14] += 1 << 31;
	
    md5_compress(IHV_TE,msg2block0);
    md5_compress(IHV_TE,msg2block1);

    cout<<"\n\nmsg1block0\n";
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg1block0[i]<<' ';
    }
    cout<<endl;
    cout<<"\nmsg1block1"<<endl;
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg1block1[i]<<' ';
    }
    cout<<endl;

    cout<<"\n\nmsg2block0\n";
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg2block0[i]<<' ';
    }
    cout<<endl;
    cout<<"\nmsg2block1"<<endl;
    for (size_t i = 0; i < 16; i++)
    {
        cout<<hex<<msg2block1[i]<<' ';
    }
    cout<<endl;

    //验证消息是否碰撞
    cout<<"\n碰撞hash值： ";
    for (size_t i = 0; i < 4; i++)
    {
        cout<<IHV[i];
        if (IHV[i]!=IHV_TE[i])
        {
            cout<<"not success\n";
        }
        
    }
    cout<<"\n\n时间：\n";
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %r9d, %r14d
movq %r8, %r15
movq %rcx, -0x48(%rbp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbx
testl %r9d, %r9d
je 0x14fa
movq 0x9afb(%rip), %rdi      # 0xafd8
leaq 0x7b3c(%rip), %rsi      # 0x9020
movl $0x18, %edx
callq 0x10d0
movq 0x9ae3(%rip), %rdi      # 0xafd8
callq 0x1070
movq %r13, %rdi
movq %rbx, %rsi
callq 0x18d8
movl (%rbx), %eax
leaq -0x60(%rbp), %rdi
movl %eax, (%rdi)
movl 0x4(%rbx), %ecx
movl %ecx, 0x4(%rdi)
movl 0x8(%rbx), %edx
movl %edx, 0x8(%rdi)
movl 0xc(%rbx), %esi
movl %esi, 0xc(%rdi)
movl %eax, -0x40(%rbp)
movl %ecx, -0x3c(%rbp)
movl %edx, -0x38(%rbp)
movl %esi, -0x34(%rbp)
movq %r13, %rsi
callq 0x2d34
testb %r14b, %r14b
je 0x1585
movq 0x9a99(%rip), %rbx      # 0xafd8
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
movq %rax, %rbx
leaq 0x7ac9(%rip), %rsi      # 0x9039
movl $0x19, %edx
movq %rax, %rdi
callq 0x10d0
movq %rbx, %rdi
callq 0x1070
leaq -0x60(%rbp), %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2bb8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2d34
xorl %eax, %eax
movq -0x48(%rbp), %rsi
movl (%r13,%rax,4), %ecx
movl %ecx, (%rsi,%rax,4)
movl (%r12,%rax,4), %ecx
movl %ecx, (%r15,%rax,4)
incq %rax
cmpq $0x10, %rax
jne 0x15a5
xorb $-0x80, 0x13(%rsi)
addl $0x8000, 0x2c(%rsi)     # imm = 0x8000
xorb $-0x80, 0x3b(%rsi)
xorb $-0x80, 0x13(%r15)
addl $0xffff8000, 0x2c(%r15) # imm = 0xFFFF8000
xorb $-0x80, 0x3b(%r15)
leaq -0x40(%rbp), %rbx
movq %rbx, %rdi
callq 0x2d34
movq %rbx, %rdi
movq %r15, -0x68(%rbp)
movq %r15, %rsi
callq 0x2d34
movq 0x99d7(%rip), %r14      # 0xafd8
leaq 0x7a4b(%rip), %rsi      # 0x9053
movl $0xd, %edx
movq %r14, %rdi
callq 0x10d0
xorl %r15d, %r15d
leaq -0x29(%rbp), %rbx
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movl (%r13,%r15,4), %esi
movq %r14, %rdi
callq 0x1060
movb $0x20, -0x29(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10d0
incq %r15
cmpq $0x10, %r15
jne 0x161c
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq 0x79d6(%rip), %rsi      # 0x9061
movl $0xb, %edx
movq %r14, %rdi
callq 0x10d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
xorl %r15d, %r15d
leaq -0x29(%rbp), %rbx
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movl (%r12,%r15,4), %esi
movq %r14, %rdi
callq 0x1060
movb $0x20, -0x29(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10d0
incq %r15
cmpq $0x10, %r15
jne 0x16c6
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq 0x7939(%rip), %rsi      # 0x906d
movl $0xd, %edx
movq %r14, %rdi
callq 0x10d0
xorl %r15d, %r15d
leaq -0x29(%rbp), %rbx
movq -0x48(%rbp), %r12
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movl (%r12,%r15,4), %esi
movq %r14, %rdi
callq 0x1060
movb $0x20, -0x29(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10d0
incq %r15
cmpq $0x10, %r15
jne 0x174c
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq 0x78c1(%rip), %rsi      # 0x907b
movl $0xb, %edx
movq %r14, %rdi
callq 0x10d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
xorl %r15d, %r15d
leaq -0x29(%rbp), %rbx
movq -0x68(%rbp), %r12
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x18(%r14,%rax), %ecx
andl $-0x4b, %ecx
orl $0x8, %ecx
movl %ecx, 0x18(%r14,%rax)
movl (%r12,%r15,4), %esi
movq %r14, %rdi
callq 0x1060
movb $0x20, -0x29(%rbp)
movl $0x1, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10d0
incq %r15
cmpq $0x10, %r15
jne 0x17f9
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x10a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x1030
movq %rax, %rdi
callq 0x1070
leaq 0x7820(%rip), %rsi      # 0x9087
movl $0x12, %edx
movq %r14, %rdi
callq 0x10d0
xorl %r15d, %r15d
leaq 0x781c(%rip), %rbx      # 0x909a
movl -0x60(%rbp,%r15,4), %esi
movq %r14, %rdi
callq 0x1060
movl -0x60(%rbp,%r15,4), %eax
cmpl -0x40(%rbp,%r15,4), %eax
je 0x18a7
movl $0xc, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10d0
incq %r15
cmpq $0x4, %r15
jne 0x187e
movq 0x9721(%rip), %rdi      # 0xafd8
leaq 0x77e9(%rip), %rsi      # 0x90a7
movl $0xc, %edx
callq 0x10d0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop