// File: /workspace/github_c_makefile_star2_O0/bokunimowakaru[P]petit15term/lpc21isp/adprog.c
// Function: AnalogDevicesFormPacket
static void AnalogDevicesFormPacket(ISP_ENVIRONMENT *IspEnvironment,
                                                char cmd, int no_bytes, unsigned int address,
                                                const void *data, AD_PACKET *packet)
{
    BINARY checksum;
    const BINARY *data_in;
    int i;

    (void)IspEnvironment; /* never used in this function */

    /*  Some sanity checking on the arguments.  These should only   */
    /* fail if there is a bug in the caller.                        */
    /*  Check 1) that the number of data bytes is in an acceptable  */
    /* range, 2) that we have a non-null pointer if data is being   */
    /* put in the packet and 3) that we have a non-null pointer to  */
    /* the packet to be filled. We just exit with an error message  */
    /* if any of these tests fail.                                  */
    if ((no_bytes < 0) || (no_bytes > 250))
    {
        DebugPrintf(1,
            "The number of bytes (%d) passed to FormPacket is invalid.\n",
            no_bytes);
        exit(-1);
    }
    if ((data == 0) && (no_bytes != 0))
    {
        DebugPrintf(1,
            "A null pointer to data paased to FormPacket when data was expected.\n");
        exit(-1);
    }
    if (packet == 0)
    {
        DebugPrintf(1,
            "A null packet pointer was passed to FormPacket.\n");
        exit(-1);
    }

    checksum = 0;               /*  Checksum starts at zero.            */

    data_in = (BINARY*) data;             /*  Pointer pun so we can walk through  */
    /* the data.                            */

    packet->start1 = 0x7;       /*  The start of the packet is constant.*/
    packet->start2 = 0xE;

    /*  Fill in the rest of the packet and calculate the checksum   */
    /* as we go.                                                    */

    /* The number of bytes is the number of data bytes + the        */
    /* address bytes + the command byte.                            */
    packet->bytes = (BINARY)(no_bytes + 5);

    checksum += packet->bytes;

    /*  The command for the packet being sent.  No error checking   */
    /* done on this.                                                */
    packet->cmd = cmd;

    checksum += cmd;

    /*  Now break up the address and place in the proper packet     */
    /* locations.                                                   */
    packet->address_l = (BINARY)(address & 0xFF);
    packet->address_m = (BINARY)((address >> 8) & 0xFF);
    packet->address_u = (BINARY)((address >> 16) & 0xFF);
    packet->address_h = (BINARY)((address >> 24) & 0xFF);

    checksum += packet->address_l;
    checksum += packet->address_m;
    checksum += packet->address_u;
    checksum += packet->address_h;

    /*  Copy the data bytes into the packet.  We could use memcpy   */
    /* but we have to calculate the checksum anyway.                */
    for (i = 0; i < no_bytes; i++)
    {
        packet->data[i] = data_in[i];
        checksum += data_in[i];
    }

    /*  Finally, add the checksum to the end of the packet.         */
    packet->data[i] = (BINARY)-checksum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
movb %al, -0x9(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
cmpl $0x0, -0x10(%rbp)
jl 0x6f6f
cmpl $0xfa, -0x10(%rbp)
jle 0x6f8f
movl -0x10(%rbp), %edx
movl $0x1, %edi
leaq 0x513f(%rip), %rsi      # 0xc0bd
movb $0x0, %al
callq 0x44e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4250
cmpq $0x0, -0x20(%rbp)
jne 0x6fb9
cmpl $0x0, -0x10(%rbp)
je 0x6fb9
movl $0x1, %edi
leaq 0x5150(%rip), %rsi      # 0xc0f8
movb $0x0, %al
callq 0x44e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4250
cmpq $0x0, -0x28(%rbp)
jne 0x6fdd
movl $0x1, %edi
leaq 0x5171(%rip), %rsi      # 0xc13d
movb $0x0, %al
callq 0x44e0
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4250
movb $0x0, -0x29(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movb $0x7, (%rax)
movq -0x28(%rbp), %rax
movb $0xe, 0x1(%rax)
movl -0x10(%rbp), %eax
addl $0x5, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x2(%rax)
movq -0x28(%rbp), %rax
movzbl 0x2(%rax), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movb -0x9(%rbp), %cl
movq -0x28(%rbp), %rax
movb %cl, 0x3(%rax)
movsbl -0x9(%rbp), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movl -0x14(%rbp), %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x7(%rax)
movl -0x14(%rbp), %eax
shrl $0x8, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x6(%rax)
movl -0x14(%rbp), %eax
shrl $0x10, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x5(%rax)
movl -0x14(%rbp), %eax
shrl $0x18, %eax
andl $0xff, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x28(%rbp), %rax
movzbl 0x7(%rax), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x6(%rax), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x5(%rax), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movq -0x28(%rbp), %rax
movzbl 0x4(%rax), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x7106
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb (%rax,%rcx), %dl
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, 0x8(%rax,%rcx)
movq -0x38(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movzbl (%rax,%rcx), %ecx
movzbl -0x29(%rbp), %eax
addl %ecx, %eax
movb %al, -0x29(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x70c7
movzbl -0x29(%rbp), %ecx
xorl %eax, %eax
subl %ecx, %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, 0x8(%rax,%rcx)
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)