// File: /workspace/github_c_makefile_star2_O3/loversmile[P]sugarcrm/sugar_client.c
// Function: crmClient
void *crmClient(void *data)
{
    char *server = "http://192.168.124.27/crm/service/v4_1/soap.php";
    //char *server = "http://192.168.124.99/crm/service/v4_1/soap.php";
    
    //struct login_return = NULL;
    //login_return = initLoginReturn(login_return);
    int normal = 1;
    int time_now = 0;
    
    SGCRMInfoTask *info_task = NULL;
    info_task = initSGCRMInfoTask(info_task);
    //info_task->server_info = initSGServerInfo(info_task->server_info);
    info_task->server_info->soap = soap_new();
    info_task->server_info->soap_endpoint = strdup(server);
    
    queueInitialize(&request_crm_queue);
    
    SGCRMTask *crm_task = NULL;
    
    char *username = "admin";
    char *password = "123456";
    int login_ret = -1;
    int ret = -1;
    while(g_crm_task) {
        printf("%d\n", time(NULL));
        printf("%d\n", time_now);
        //This need a function to monitor crmtask
		monitorTaskAll();

        if (normal == 1 && (time(NULL) - time_now) >= 1790 )
        {
            time_now = time(NULL);
            //info_task->login_return = initSGLoginReturn(info_task->login_return);
            login_ret = SGCRMLogin(info_task->server_info, username, password, info_task->login_return); 
        } 
        else if (normal == 0)
        {
            //info_task->login_return = initSGLoginReturn(info_task->login_return);
            login_ret = SGCRMLogin(info_task->server_info, username, password, info_task->login_return);
        }

        if (login_ret == 0 && info_task->login_return && 
                info_task->login_return->session_id && 
                info_task->login_return->user_id)
        {
            printf("session = %s\n", info_task->login_return->session_id);
            printf("user    = %s\n", info_task->login_return->user_id);
            normal = 1;
        } 
        else 
        {
            printf("login error login_ret = %d\n", login_ret);
            normal = 0;
            sleep(1);
            continue;
        }
        
        while(DEQUEUE_SUCCESS_AND_CONTINUE == (ret = dequeueTask(&request_crm_queue, &crm_task)) || DEQUEUE_SUCCESS_AND_EMPTY == ret)
        {
            if (crm_task)
            {
                int res = executeRequest(info_task, crm_task);
                
            }
        }
        cleanSGCRMTask(crm_task);
        sleep(1);
		//break;
    }
	cleanSGCRMInfoTask(info_task);
    cleanQueue(&request_crm_queue);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %edi, %edi
callq 0x4c14f
movq %rax, %rbx
callq 0x3e779
movq 0x8(%rbx), %r14
movq %rax, (%r14)
leaq 0x5b73(%rip), %rdi      # 0x5186c
callq 0x3390
movq %rax, 0x8(%r14)
leaq 0x2661f(%rip), %rdi     # 0x72328
callq 0x4bf58
movq $0x0, (%rsp)
cmpl $0x0, 0x26673(%rip)     # 0x72390
je 0x4be68
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movb $0x1, %r13b
leaq 0x8383(%rip), %r14      # 0x540b6
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %edi, %edi
callq 0x3250
movq %r14, %rdi
movq %rax, %rsi
xorl %eax, %eax
callq 0x3140
movq %r14, %rdi
movl %r12d, %esi
xorl %eax, %eax
callq 0x3140
callq 0x4bc1a
testb $0x1, %r13b
je 0x4bd85
xorl %edi, %edi
callq 0x3250
movslq %r12d, %rcx
subq %rcx, %rax
cmpq $0x6fe, %rax            # imm = 0x6FE
jl 0x4bd85
xorl %edi, %edi
callq 0x3250
movq %rax, %r12
jmp 0x4bd8b
testb $0x1, %bpl
je 0x4bda8
movq (%rbx), %rcx
movq 0x8(%rbx), %rdi
leaq 0x804b(%rip), %rsi      # 0x53de4
leaq 0x804a(%rip), %rdx      # 0x53dea
callq 0x4ef89
movl %eax, %r15d
testl %r15d, %r15d
je 0x4bde0
xorl %r13d, %r13d
leaq 0x8056(%rip), %rdi      # 0x53e0d
movl %r15d, %esi
xorl %eax, %eax
callq 0x3140
movb $0x1, %bpl
movl $0x1, %edi
callq 0x33b0
cmpl $0x0, 0x265bb(%rip)     # 0x72390
jne 0x4bd38
jmp 0x4be68
movq (%rbx), %rax
testq %rax, %rax
je 0x4bdad
movq (%rax), %rsi
testq %rsi, %rsi
je 0x4bdad
cmpq $0x0, 0x8(%rax)
je 0x4bdad
leaq 0x7ff3(%rip), %rdi      # 0x53df1
xorl %eax, %eax
callq 0x3140
movq (%rbx), %rax
movq 0x8(%rax), %rsi
leaq 0x7fec(%rip), %rdi      # 0x53dff
xorl %eax, %eax
callq 0x3140
leaq 0x26507(%rip), %r15     # 0x72328
movq %r15, %rdi
movq %rsp, %rsi
callq 0x4c094
cmpl $0x1, %eax
ja 0x4be52
movq (%rsp), %rsi
testq %rsi, %rsi
je 0x4be42
movq %rbx, %rdi
callq 0x4bc60
movq %r15, %rdi
movq %rsp, %rsi
callq 0x4c094
cmpl $0x2, %eax
jb 0x4be31
movq (%rsp), %rdi
callq 0x4bfda
movb $0x1, %r13b
xorl %r15d, %r15d
xorl %ebp, %ebp
jmp 0x4bdc4
movq %rbx, %rdi
callq 0x4c19b
leaq 0x264b1(%rip), %rdi     # 0x72328
callq 0x4bf93
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq