// File: /workspace/github_c_makefile_star2_O0/bokunimowakaru[P]petit15term/lpc21isp/adprog.c
// Function: AnalogDevicesSync
static void AnalogDevicesSync(ISP_ENVIRONMENT *IspEnvironment)
{
    BINARY sync;                        /* Holds sync command.          */
    AD_SYNC_RESPONSE response;          /* Response from micro.         */
    int sync_attempts;                  /* Number of retries.           */

    /*  Make sure we don't read garbage later instead of the        */
    /* response we expect from the micro.                           */
    ClearSerialPortBuffers(IspEnvironment);

    DebugPrintf(2, "Synchronizing\n"); /* Progress report.             */

    sync = ANALOG_DEVICES_SYNC_CHAR;    /* Build up sync command.       */

    /*  Perform the actual sync attempt.  First send the sync       */
    /* character, the attempt to read back the response.  For the   */
    /* AD ARM micro this is a fixed length block.  If response is   */
    /* received attempt to validate it by comparing the first       */
    /* characters to those expected.  If the received block does    */
    /* not validate or is incomplete empty the serial buffer and    */
    /* retry.                                                       */
    for (sync_attempts = 0; sync_attempts < 5; sync_attempts++)
    {
        SendComPortBlock(IspEnvironment, &sync, 1);

        if (ReceiveComPortBlockComplete(IspEnvironment, &response, sizeof(response),
            500) == 0)
        {

            if (memcmp(response.product_id, ANALOG_DEVICES_SYNC_RESPONSE,
                ANALOG_DEVICES_SYNC_SIZE) == 0)
            {
                return;
            }
            else
            {
                DumpString(3, &response, sizeof(response),
                    "Unexpected response to sync attempt ");
            }
        }
        else
        {
            DebugPrintf(3, "No (or incomplete) answer on sync attempt\n");
        }

        ClearSerialPortBuffers(IspEnvironment);
    }

    DebugPrintf(1, "No (or unacceptable) answer on sync attempt\n");
    exit(4);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x48f0
movl $0x2, %edi
leaq 0x5357(%rip), %rsi      # 0xc018
movb $0x0, %al
callq 0x44e0
movb $0x8, -0x9(%rbp)
movl $0x0, -0x28(%rbp)
cmpl $0x5, -0x28(%rbp)
jge 0x6d6d
movq -0x8(%rbp), %rdi
leaq -0x9(%rbp), %rsi
movl $0x1, %edx
callq 0x45c0
movq -0x8(%rbp), %rdi
leaq -0x21(%rbp), %rsi
movl $0x18, %edx
movl $0x1f4, %ecx            # imm = 0x1F4
callq 0x4e10
cmpl $0x0, %eax
jne 0x6d43
leaq -0x21(%rbp), %rax
movl (%rax), %eax
subl $0x43754441, %eax       # imm = 0x43754441
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x6d27
addq $0x30, %rsp
popq %rbp
retq
movl $0x3, %edi
leaq -0x21(%rbp), %rsi
movl $0x18, %edx
leaq 0x52f0(%rip), %rcx      # 0xc02c
callq 0x4630
jmp 0x6d56
movl $0x3, %edi
leaq 0x5302(%rip), %rsi      # 0xc051
movb $0x0, %al
callq 0x44e0
movq -0x8(%rbp), %rdi
callq 0x48f0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x6cd3
movl $0x1, %edi
leaq 0x5303(%rip), %rsi      # 0xc07c
movb $0x0, %al
callq 0x44e0
movl $0x4, %edi
callq 0x4250
nopw (%rax,%rax)