// File: /workspace/github_c_makefile_star2_O2/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 0x24c6
movq %rsi, %rbx
movl %edi, %ebp
leaq 0xcdc2(%rip), %rax      # 0xf1c0
movabsq $0x676f6c2e657461, %rcx # imm = 0x676F6C2E657461
movq %rcx, 0x7(%rax)
movabsq $0x616470755f736e64, %rcx # imm = 0x616470755F736E64
movq %rcx, (%rax)
movq 0x8(%rsi), %rcx
leaq 0xce0c(%rip), %r14      # 0xf230
leaq 0x4d32(%rip), %rdx      # 0x715d
pushq $0x64
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x2120
pushq $0x3a
popq %rsi
movq %r14, %rdi
callq 0x2100
testq %rax, %rax
je 0x24c6
cmpb $0x0, 0x1(%rax)
je 0x24c6
movq %rax, %rdi
incq %rdi
movb $0x0, (%rax)
callq 0x2250
leaq 0xce31(%rip), %r13      # 0xf294
movl %eax, (%r13)
movq 0x10(%rbx), %rcx
leaq 0xce2e(%rip), %rdi      # 0xf2a0
leaq 0x4ce4(%rip), %rdx      # 0x715d
pushq $0x64
popq %rsi
xorl %eax, %eax
callq 0x2120
xorl %eax, %eax
callq 0x572c
movl %eax, 0xcd04(%rip)      # 0xf194
cmpl $-0x1, %eax
je 0x24e2
leaq 0xcd94(%rip), %rdi      # 0xf230
leaq 0x10(%rsp), %rsi
callq 0x5883
testl %eax, %eax
je 0x2515
leaq 0x4bb1(%rip), %rdi      # 0x7062
callq 0x2070
leaq 0x4b9f(%rip), %rdi      # 0x705c
xorl %eax, %eax
callq 0x58d1
jmp 0x24cb
callq 0x23cc
pushq $0x1
popq %rbp
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 0x4b1b(%rip), %rdi      # 0x7013
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x2290
leaq 0x4b25(%rip), %rdi      # 0x7035
jmp 0x2668
cmpl $0x4, %ebp
jne 0x2523
leaq 0x4f5e(%rip), %r14      # 0x747f
jmp 0x253d
movq 0x20(%rbx), %rdx
leaq 0x4b4e(%rip), %rsi      # 0x707c
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2260
movq 0x18(%rbx), %rdx
leaq 0x4b41(%rip), %rsi      # 0x7089
leaq 0x430(%rsp), %r15
movq %r15, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2260
movq %r15, %rdi
callq 0x20f0
movq %rax, %r14
leaq 0xcd31(%rip), %rdi      # 0xf2a0
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r15, %rdx
movl %r14d, %ecx
callq 0x5dc3
movl 0xcc0c(%rip), %edi      # 0xf194
movl (%r13), %edx
movslq %r14d, %r14
addq $0xc, %r14
leaq 0x10(%rsp), %rsi
movq %r12, %rcx
movl %r14d, %r8d
callq 0x57ce
cltq
cmpq %rax, %r14
jne 0x263a
movl 0xcbe0(%rip), %eax      # 0xf194
leaq 0x28(%rsp), %rdi
movl %eax, (%rdi)
movw $0x1, 0x4(%rdi)
pushq $0x1
popq %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x2200
testl %eax, %eax
jle 0x2677
movl 0xcbb8(%rip), %edi      # 0xf194
leaq 0x30(%rsp), %rsi
leaq 0x10(%rsp), %rcx
leaq 0xc(%rsp), %r8
movl $0x400, %edx            # imm = 0x400
callq 0x5821
testl %eax, %eax
jle 0x268f
movl %eax, %ecx
movb $0x0, 0x30(%rsp,%rcx)
leaq 0xcc95(%rip), %rdi      # 0xf2a0
pushq $0xa
popq %rsi
leaq 0x30(%rsp), %rdx
movl %eax, %ecx
callq 0x5ed3
testl %eax, %eax
je 0x26bf
leaq 0x4b3d(%rip), %rdi      # 0x7166
callq 0x2070
leaq 0x4b2b(%rip), %rdi      # 0x7160
jmp 0x24bd
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x2290
leaq 0x4a65(%rip), %rdi      # 0x70b5
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x2290
leaq 0x4a6b(%rip), %rdi      # 0x70d3
movq %rax, %rsi
xorl %eax, %eax
callq 0x58d1
jmp 0x24cb
leaq 0x4c32(%rip), %rdi      # 0x72b0
callq 0x2070
leaq 0x4a6c(%rip), %rdi      # 0x70f6
jmp 0x24bd
callq 0x2040
movq %rax, %rbx
movl (%rax), %edi
callq 0x2290
leaq 0x4a72(%rip), %rdi      # 0x7117
movq %rax, %rsi
xorl %eax, %eax
callq 0x2110
movl (%rbx), %edi
callq 0x2290
leaq 0x4a7c(%rip), %rdi      # 0x7139
jmp 0x2668
leaq 0x3c(%rsp), %rdi
callq 0x3f86
testq %rax, %rax
je 0x2705
movq %rax, %r14
leaq 0x4ad1(%rip), %rsi      # 0x71a9
movq %rax, %rdi
callq 0x460c
testq %rax, %rax
je 0x271a
movq 0x28(%rax), %rsi
testq %rsi, %rsi
je 0x2737
leaq 0x4ae3(%rip), %rdi      # 0x71d8
xorl %eax, %eax
callq 0x2110
leaq 0x4af7(%rip), %rdi      # 0x71fa
jmp 0x272d
leaq 0x4b71(%rip), %rdi      # 0x727d
callq 0x2070
leaq 0x4a65(%rip), %rdi      # 0x717d
jmp 0x272d
leaq 0x4b78(%rip), %rdi      # 0x7299
callq 0x2070
leaq 0x4a84(%rip), %rdi      # 0x71b1
leaq 0x30(%rsp), %rsi
jmp 0x266b
movq 0x18(%rbx), %rsi
leaq 0x4ade(%rip), %rdi      # 0x7220
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2110
movq 0x18(%rbx), %rsi
leaq 0x4ae0(%rip), %rdi      # 0x7236
xorl %eax, %eax
callq 0x58d1
movl 0xca31(%rip), %edi      # 0xf194
callq 0x2140
movq %r14, %rdi
callq 0x3923
jmp 0x24ce
nopl (%rax)