// File: /workspace/github_c_makefile_star2_O1/bloomsource[P]dns_update/dns_update.c
// Function: main
int main( int argc, char* argv[] )
{
    int rc;
    char json[1024];
    char buf[1024];
    char ip[16];
    int port;
    int json_len;
    struct pollfd pfd;
    char* p;
    cJSON *json_root, *json_retcode;
    
    if( argc < 4 )
    {
        usage();
        return 1;
    }
    
    
    strcpy( log_file, "dns_update.log" );
    
    snprintf( server, sizeof(server), "%s", argv[1] );
    p = strchr( server, ':' );
    if( !p )
    {
        usage();
        return 1;
    }
    if( strlen( p+1 ) == 0 )
    {
        usage();
        return 1;
    }
    
    p[0] = 0;
    
    cmd_port = atoi( p+1 );
    snprintf( auth_key, sizeof(auth_key), "%s", argv[2] );
    
    
    fd = udp_create();
    if( fd == -1 )
    {
        printf( "create udp socket failed! err:%s\n", ERR );
        write_log( "[ERR] create udp socket failed! err:%s", ERR );
        return 1;
    }
    
    if( resolve_host_ip( server, ip ) )
    {
        printf( "resolve server ip failed!\n" );
        write_log( "[ERR] resolve server ip failed!" );
        return 1;
    }
    
    if( argc > 4 )
    {
        sprintf( buf, ", \"ip\":\"%s\" ", argv[4] );
    }
    
    sprintf( json, "{ \"cmd\":\"update_domain\", \"domain\":\"%s\" %s }", argv[3], argc > 4 ? buf : " " );
    
    json_len = strlen( json );
    pack_update_pkg( auth_key, buf, json, json_len );
    
    rc = udp_send( fd, ip, cmd_port, buf, json_len + sizeof(upd_pack_hdr) );
    if( rc != json_len + sizeof(upd_pack_hdr) )
    {
        printf( "send udp pack failed! err:%s\n", ERR );
        write_log( "[ERR] send udp pack failed! err:%s", ERR );
        return 1;
    }
    
    pfd.fd = fd;
    pfd.events = POLLIN;
    
    rc = poll( &pfd, 1, 1000 );
    if( rc <= 0 )
    {
        printf( "recv udp response timeout!\n" );
        write_log( "[ERR] recv udp response timeout." );
        return 1;
    }
    
    rc = udp_recv( fd, buf, sizeof(buf), ip, &port );
    if( rc <= 0 )
    {
        printf( "recv udp response failed! err:%s\n", ERR );
        write_log( "[ERR] recv udp response failed! err:%s", ERR );
        return 1;
    }
    buf[rc] = 0;
    
    if( unpack_update_pkg( auth_key, TIME_DIFF, buf, rc ) )
    {
        printf( "recv invalid response!\n" );
        write_log( "[ERR] recv invalid response!" );
        return 1;
    }
    
        
    json_root = cJSON_Parse( buf + sizeof(upd_pack_hdr) );
    if( !json_root )
    {
        printf( "parse response json failed!\n" );
        write_log( "[ERR] parse response json failed! resp:[%s]", buf );
        return 1;
    }
    
    json_retcode = cJSON_GetObjectItem( json_root, "retcode" );
    if( !json_retcode )
    {
        printf( "invlaid json response.\n" );
        write_log( "[ERR] invalid json response, resp:[%s]", buf );
        return 1;
    }
    
    if( json_retcode->valueint )
    {
        printf( "update domain failed, retcode:%d\n", (int)json_retcode->valueint );
        write_log( "[ERR] update domain fialed, resp:[%s]", buf );
        return 1;
    }
    
    printf( "update domain %s ok!\n", argv[3] );
    write_log( "update domain %s ok!", argv[3] );
    close( fd );
    cJSON_Delete( json_root );
    
    
    
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x838, %rsp            # imm = 0x838
cmpl $0x3, %edi
jle 0x24e0
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xddc2(%rip), %rax      # 0x101d0
movabsq $0x676f6c2e657461, %rcx # imm = 0x676F6C2E657461
movq %rcx, 0x7(%rax)
movabsq $0x616470755f736e64, %rcx # imm = 0x616470755F736E64
movq %rcx, (%rax)
movq 0x8(%rsi), %rcx
leaq 0xde0c(%rip), %r14      # 0x10240
leaq 0x5d22(%rip), %rdx      # 0x815d
movl $0x64, %esi
movq %r14, %rdi
xorl %eax, %eax
callq 0x2120
movq %r14, %rdi
movl $0x3a, %esi
callq 0x2100
testq %rax, %rax
je 0x24e0
cmpb $0x0, 0x1(%rax)
je 0x24e0
movq %rax, %rdi
incq %rdi
movb $0x0, (%rax)
callq 0x2260
leaq 0xde29(%rip), %r13      # 0x102a4
movl %eax, (%r13)
movq 0x10(%rbx), %rcx
leaq 0xde26(%rip), %rdi      # 0x102b0
leaq 0x5ccc(%rip), %rdx      # 0x815d
movl $0x64, %esi
xorl %eax, %eax
callq 0x2120
xorl %eax, %eax
callq 0x5e2c
movl %eax, 0xdcfa(%rip)      # 0x101a4
cmpl $-0x1, %eax
je 0x2505
leaq 0xdd8a(%rip), %rdi      # 0x10240
leaq 0x10(%rsp), %rsi
callq 0x5f9c
testl %eax, %eax
je 0x2538
leaq 0x5b97(%rip), %rdi      # 0x8062
callq 0x2070
leaq 0x5b85(%rip), %rdi      # 0x805c
xorl %eax, %eax
callq 0x5fec
jmp 0x24ec
leaq 0x5d64(%rip), %rdi      # 0x824b
callq 0x2070
movl $0x1, %ebp
movl %ebp, %eax
addq $0x838, %rsp            # imm = 0x838
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x22a0
leaq 0x5af8(%rip), %rdi      # 0x8013
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x22a0
leaq 0x5b02(%rip), %rdi      # 0x8035
jmp 0x268f
cmpl $0x4, %ebp
jne 0x2546
leaq 0x5f3b(%rip), %r14      # 0x847f
jmp 0x2560
movq 0x20(%rbx), %rdx
leaq 0x5b2b(%rip), %rsi      # 0x807c
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2270
movq 0x18(%rbx), %rdx
leaq 0x5b1e(%rip), %rsi      # 0x8089
leaq 0x430(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2270
movq %r15, %rdi
callq 0x20f0
movq %rax, %r14
leaq 0xdd1e(%rip), %rdi      # 0x102b0
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
movl %r14d, %ecx
callq 0x6598
movl 0xdbf9(%rip), %edi      # 0x101a4
movl (%r13), %edx
movslq %r14d, %r14
addq $0xc, %r14
leaq 0x10(%rsp), %rsi
movq %r12, %rcx
movl %r14d, %r8d
callq 0x5ee5
cltq
cmpq %rax, %r14
jne 0x2661
movl 0xdbcd(%rip), %eax      # 0x101a4
leaq 0x28(%rsp), %rdi
movl %eax, (%rdi)
movw $0x1, 0x4(%rdi)
movl $0x1, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2210
testl %eax, %eax
jle 0x269e
movl 0xdba3(%rip), %edi      # 0x101a4
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rcx
leaq 0xc(%rsp), %r8
movl $0x400, %edx            # imm = 0x400
callq 0x5f3a
testl %eax, %eax
jle 0x26b6
movl %eax, %ecx
movb $0x0, 0x30(%rsp,%rcx)
leaq 0xdc80(%rip), %rdi      # 0x102b0
leaq 0x30(%rsp), %rdx
movl $0xa, %esi
movl %eax, %ecx
callq 0x66a2
testl %eax, %eax
je 0x26e6
leaq 0x5b16(%rip), %rdi      # 0x8166
callq 0x2070
leaq 0x5b04(%rip), %rdi      # 0x8160
jmp 0x24d7
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x22a0
leaq 0x5a3e(%rip), %rdi      # 0x80b5
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x22a0
leaq 0x5a44(%rip), %rdi      # 0x80d3
movq %rax, %rsi
xorl %eax, %eax
callq 0x5fec
jmp 0x24ec
leaq 0x5c0b(%rip), %rdi      # 0x82b0
callq 0x2070
leaq 0x5a45(%rip), %rdi      # 0x80f6
jmp 0x24d7
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x22a0
leaq 0x5a4b(%rip), %rdi      # 0x8117
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x22a0
leaq 0x5a55(%rip), %rdi      # 0x8139
jmp 0x268f
leaq 0x3c(%rsp), %rdi
callq 0x3fcd
testq %rax, %rax
je 0x272c
movq %rax, %r14
leaq 0x5aaa(%rip), %rsi      # 0x81a9
movq %rax, %rdi
callq 0x4723
testq %rax, %rax
je 0x2741
movq 0x28(%rax), %rsi
testq %rsi, %rsi
je 0x275e
leaq 0x5abc(%rip), %rdi      # 0x81d8
xorl %eax, %eax
callq 0x2110
leaq 0x5ad0(%rip), %rdi      # 0x81fa
jmp 0x2754
leaq 0x5b4a(%rip), %rdi      # 0x827d
callq 0x2070
leaq 0x5a3e(%rip), %rdi      # 0x817d
jmp 0x2754
leaq 0x5b51(%rip), %rdi      # 0x8299
callq 0x2070
leaq 0x5a5d(%rip), %rdi      # 0x81b1
leaq 0x30(%rsp), %rsi
jmp 0x2692
movq 0x18(%rbx), %rsi
leaq 0x5ab7(%rip), %rdi      # 0x8220
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2110
movq 0x18(%rbx), %rsi
leaq 0x5ab9(%rip), %rdi      # 0x8236
xorl %eax, %eax
callq 0x5fec
movl 0xda1a(%rip), %edi      # 0x101a4
callq 0x2150
movq %r14, %rdi
callq 0x39b5
jmp 0x24f1