// File: /workspace/github_c_makefile_star2_O0/bloomsource[P]dns_update/util.c
// Function: pack_update_pkg
int pack_update_pkg( char* passwd, char* buf, char* msg, int msglen )
{
    aes_context ctx;
    
    upd_pack_hdr *hdr;
    char tmp[1024];
    unsigned char aes_key[32];
    unsigned char iv[16];
    unsigned int  checksum;
    unsigned int  timestamp;
    int offset = 0;
    int left = msglen + sizeof(upd_pack_hdr);
    int proc_len;
    
    int iv_off = 0;
    
    hdr = (upd_pack_hdr*)buf;
    
    crypto_derive_key( passwd, aes_key, 32 );
    md5( (unsigned char*)passwd, strlen( passwd ), iv );
    
    timestamp = time( NULL );
    hdr->time_stamp = htonl( timestamp );
    hdr->checksum   = 0;
    hdr->rnd        = rand();

    memcpy( buf + sizeof(upd_pack_hdr), msg, msglen );
    
    checksum = crc32( 0, buf, sizeof(upd_pack_hdr) + msglen );
    hdr->checksum = htonl( checksum );
    aes_setkey_enc( &ctx, aes_key, 256 );
    
    while( left )
    {
        proc_len = left > sizeof(tmp) ? sizeof(tmp) : left;
        
        aes_crypt_cfb128( &ctx, AES_ENCRYPT, proc_len, &iv_off, iv, (unsigned char*)buf + offset, (unsigned char*)tmp );
        memcpy( buf + offset, tmp, proc_len );
        
        left -= proc_len;
        offset += proc_len;
    }
    
    
    return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x6c0, %rsp            # imm = 0x6C0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movl $0x0, -0x69c(%rbp)
movslq -0x1c(%rbp), %rax
addq $0xc, %rax
movl %eax, -0x6a0(%rbp)
movl $0x0, -0x6a8(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x680(%rbp), %rsi
movl $0x20, %edx
callq 0x9df0
movq -0x8(%rbp), %rax
movq %rax, -0x6b0(%rbp)
movq -0x8(%rbp), %rdi
callq 0x20e0
movq -0x6b0(%rbp), %rdi
movl %eax, %esi
leaq -0x690(%rbp), %rdx
callq 0x4c70
xorl %eax, %eax
movl %eax, %edi
callq 0x21d0
movl %eax, -0x698(%rbp)
movl -0x698(%rbp), %edi
callq 0x2130
movl %eax, %ecx
movq -0x258(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x258(%rbp), %rax
movl $0x0, 0x4(%rax)
callq 0x22d0
movl %eax, %ecx
movq -0x258(%rbp), %rax
movl %ecx, (%rax)
movq -0x10(%rbp), %rdi
addq $0xc, %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
callq 0x21c0
movq -0x10(%rbp), %rsi
movslq -0x1c(%rbp), %rdx
addq $0xc, %rdx
xorl %edi, %edi
callq 0xc2d0
movl %eax, -0x694(%rbp)
movl -0x694(%rbp), %edi
callq 0x2130
movl %eax, %ecx
movq -0x258(%rbp), %rax
movl %ecx, 0x4(%rax)
leaq -0x680(%rbp), %rsi
leaq -0x250(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0xa370
cmpl $0x0, -0x6a0(%rbp)
je 0xa115
movslq -0x6a0(%rbp), %rax
cmpq $0x400, %rax            # imm = 0x400
jbe 0xa070
movl $0x400, %eax            # imm = 0x400
movq %rax, -0x6b8(%rbp)
jmp 0xa07e
movslq -0x6a0(%rbp), %rax
movq %rax, -0x6b8(%rbp)
movq -0x6b8(%rbp), %rax
movl %eax, -0x6a4(%rbp)
movl -0x6a4(%rbp), %edx
leaq -0x690(%rbp), %r8
movq -0x10(%rbp), %r9
movslq -0x69c(%rbp), %rax
addq %rax, %r9
leaq -0x660(%rbp), %rax
leaq -0x250(%rbp), %rdi
movl $0x1, %esi
leaq -0x6a8(%rbp), %rcx
movq %rax, (%rsp)
callq 0xc180
movq -0x10(%rbp), %rdi
movslq -0x69c(%rbp), %rax
addq %rax, %rdi
leaq -0x660(%rbp), %rsi
movslq -0x6a4(%rbp), %rdx
callq 0x21c0
movl -0x6a4(%rbp), %ecx
movl -0x6a0(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x6a0(%rbp)
movl -0x6a4(%rbp), %eax
addl -0x69c(%rbp), %eax
movl %eax, -0x69c(%rbp)
jmp 0xa046
xorl %eax, %eax
addq $0x6c0, %rsp            # imm = 0x6C0
popq %rbp
retq