// File: /workspace/github_c_makefile_star2_O3/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 0x24d0
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xddc2(%rip), %rax      # 0x101c0
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      # 0x10230
leaq 0x5d32(%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 0x24d0
cmpb $0x0, 0x1(%rax)
je 0x24d0
movq %rax, %rdi
incq %rdi
movb $0x0, (%rax)
callq 0x2250
leaq 0xde29(%rip), %r13      # 0x10294
movl %eax, (%r13)
movq 0x10(%rbx), %rcx
leaq 0xde26(%rip), %rdi      # 0x102a0
leaq 0x5cdc(%rip), %rdx      # 0x815d
movl $0x64, %esi
xorl %eax, %eax
callq 0x2120
xorl %eax, %eax
callq 0x5da0
movl %eax, 0xdcfa(%rip)      # 0x10194
cmpl $-0x1, %eax
je 0x24f5
leaq 0xdd8a(%rip), %rdi      # 0x10230
leaq 0x10(%rsp), %rsi
callq 0x5f10
testl %eax, %eax
je 0x2528
leaq 0x5ba7(%rip), %rdi      # 0x8062
callq 0x2070
leaq 0x5b95(%rip), %rdi      # 0x805c
xorl %eax, %eax
callq 0x5f60
jmp 0x24dc
leaq 0x5d74(%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 0x2290
leaq 0x5b08(%rip), %rdi      # 0x8013
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x2290
leaq 0x5b12(%rip), %rdi      # 0x8035
jmp 0x267f
cmpl $0x4, %ebp
jne 0x2536
leaq 0x5f4b(%rip), %r14      # 0x847f
jmp 0x2550
movq 0x20(%rbx), %rdx
leaq 0x5b3b(%rip), %rsi      # 0x807c
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2260
movq 0x18(%rbx), %rdx
leaq 0x5b2e(%rip), %rsi      # 0x8089
leaq 0x430(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2260
movq %r15, %rdi
callq 0x20f0
movq %rax, %r14
leaq 0xdd1e(%rip), %rdi      # 0x102a0
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
movl %r14d, %ecx
callq 0x650c
movl 0xdbf9(%rip), %edi      # 0x10194
movl (%r13), %edx
movslq %r14d, %r14
addq $0xc, %r14
leaq 0x10(%rsp), %rsi
movq %r12, %rcx
movl %r14d, %r8d
callq 0x5e59
cltq
cmpq %rax, %r14
jne 0x2651
movl 0xdbcd(%rip), %eax      # 0x10194
leaq 0x28(%rsp), %rdi
movl %eax, (%rdi)
movw $0x1, 0x4(%rdi)
movl $0x1, %esi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2200
testl %eax, %eax
jle 0x268e
movl 0xdba3(%rip), %edi      # 0x10194
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rcx
leaq 0xc(%rsp), %r8
movl $0x400, %edx            # imm = 0x400
callq 0x5eae
testl %eax, %eax
jle 0x26a6
movl %eax, %ecx
movb $0x0, 0x30(%rsp,%rcx)
leaq 0xdc80(%rip), %rdi      # 0x102a0
leaq 0x30(%rsp), %rdx
movl $0xa, %esi
movl %eax, %ecx
callq 0x6616
testl %eax, %eax
je 0x26d6
leaq 0x5b26(%rip), %rdi      # 0x8166
callq 0x2070
leaq 0x5b14(%rip), %rdi      # 0x8160
jmp 0x24c7
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x2290
leaq 0x5a4e(%rip), %rdi      # 0x80b5
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x2290
leaq 0x5a54(%rip), %rdi      # 0x80d3
movq %rax, %rsi
xorl %eax, %eax
callq 0x5f60
jmp 0x24dc
leaq 0x5c1b(%rip), %rdi      # 0x82b0
callq 0x2070
leaq 0x5a55(%rip), %rdi      # 0x80f6
jmp 0x24c7
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x2290
leaq 0x5a5b(%rip), %rdi      # 0x8117
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x2290
leaq 0x5a65(%rip), %rdi      # 0x8139
jmp 0x267f
leaq 0x3c(%rsp), %rdi
callq 0x3fa7
testq %rax, %rax
je 0x271c
movq %rax, %r14
leaq 0x5aba(%rip), %rsi      # 0x81a9
movq %rax, %rdi
callq 0x470e
testq %rax, %rax
je 0x2731
movq 0x28(%rax), %rsi
testq %rsi, %rsi
je 0x274e
leaq 0x5acc(%rip), %rdi      # 0x81d8
xorl %eax, %eax
callq 0x2110
leaq 0x5ae0(%rip), %rdi      # 0x81fa
jmp 0x2744
leaq 0x5b5a(%rip), %rdi      # 0x827d
callq 0x2070
leaq 0x5a4e(%rip), %rdi      # 0x817d
jmp 0x2744
leaq 0x5b61(%rip), %rdi      # 0x8299
callq 0x2070
leaq 0x5a6d(%rip), %rdi      # 0x81b1
leaq 0x30(%rsp), %rsi
jmp 0x2682
movq 0x18(%rbx), %rsi
leaq 0x5ac7(%rip), %rdi      # 0x8220
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2110
movq 0x18(%rbx), %rsi
leaq 0x5ac9(%rip), %rdi      # 0x8236
xorl %eax, %eax
callq 0x5f60
movl 0xda1a(%rip), %edi      # 0x10194
callq 0x2140
movq %r14, %rdi
callq 0x39f7
jmp 0x24e1