// File: /workspace/github_c_makefile_star2_O2/lengjingzju[P]json/json_test.c
// Function: main
int main(int argc, char *argv[])
{
    int ret = 0;
    size_t size = 0;
    int nitem = 0, nflag = 0;
    int choice = 0;
    char *file = NULL;
    char *data = NULL;
    json_object *json = NULL;
    json_mem_t mem = {0};
    unsigned int ms[8] = {0};

    pjson_memory_init(&mem);

#if JSON_SAX_APIS_SUPPORT
    if (argc == 2) {
        choice = atoi(argv[1]);
        if (choice == 0) {
            test_json_sax_print();
            return 0;
        } else {
            goto err;
        }
    }
#endif
    if (argc != 3)
        goto err;

    file = argv[1];
    if (strlen(file) == 0 || access(file, F_OK) != 0) {
        printf("%s is not exist!\n", file);
        goto err;
    }
    choice = atoi(argv[2]);
    if (choice < 1 || choice > 7)
        goto err;

    ms[0] = _system_ms_get();
    switch(choice)
    {
    case 1: case 2: case 3:
#if JSON_SAX_APIS_SUPPORT
    case 6:
#endif
        if (read_file_to_data(file, &data, &size) < 0) {
            printf("read file %s failed!\n", file);
            goto err;
        }
        break;
    default:
        break;
    }

    ms[1] = _system_ms_get();
    switch(choice)
    {
    case 1: json = json_parse_str(data, size); break;
    case 2: json = json_fast_parse_str(data, size, &mem); nflag = 1; break;
    case 3: json = json_reuse_parse_str(data, size, &mem); nflag = 1; break;
    case 4: json = json_parse_file(file); break;
    case 5: json = json_fast_parse_file(file, &mem); nflag = 1; break;

#if JSON_SAX_APIS_SUPPORT
    case 6:
        s_sax_item_num = (int)(size >> 5);
        if (json_sax_parse_str(data, size, _sax_parser_cb) < 0) {
            printf("json_sax_parse_str failed!\n");
            ret = -1;
            goto end;
        }

        ms[2] = _system_ms_get();
        if (s_print_str) {
            snprintf(s_dst_json_path, sizeof(s_dst_json_path), "%s-%d.format.json", file, choice);
            copy_data_to_file(s_print_str, s_print_size, s_dst_json_path);
            json_memory_free(s_print_str);
            s_print_str = NULL;
        }

        ms[3] = _system_ms_get();
        printf("[sax] %d. read=%-5d parse+format=%-5d       write=%-5d\n", choice, ms[1]-ms[0], ms[2]-ms[1], ms[3]-ms[2]);
        goto end;
    case 7:
        s_sax_for_file = true;
        snprintf(s_dst_json_path, sizeof(s_dst_json_path), "%s-%d.format.json", file, choice);
        if (json_sax_parse_file(file, _sax_parser_cb) < 0) {
            printf("json_sax_parse_file failed!\n");
            ret = -1;
            goto end;
        }

        ms[2] = _system_ms_get();
        printf("[sax] %d. read+parse+format+write=%-5d\n", choice, ms[2]-ms[1]);
        goto end;
#endif
    default:
        break;
    }

    if (!json) {
        printf("json parse failed!\n");
        ret = -1;
        goto end;
    }

    ms[2] = _system_ms_get();
    snprintf(s_dst_json_path, sizeof(s_dst_json_path), "%s-%d.format.json", file, choice);
    if (nflag)
        nitem = pjson_memory_statistics(&mem.obj_mgr) / sizeof(json_object);
    else
        nitem = (int)(size >> 5);
    switch(choice)
    {
    case 1: case 2: case 3:
        s_print_str = json_print_format(json, nitem, &s_print_size, NULL);
        break;
    case 4: case 5:
        json_fprint_format(json, nitem, s_dst_json_path, NULL);
        break;
    default:
        break;
    }

    ms[3] = _system_ms_get();
    if (s_print_str) {
        copy_data_to_file(s_print_str, s_print_size, s_dst_json_path);
        json_memory_free(s_print_str);
        s_print_str = NULL;
    }
    ms[4] = _system_ms_get();

    //sleep(1);

    ms[5] = _system_ms_get();
    snprintf(s_dst_json_path, sizeof(s_dst_json_path), "%s-%d.unformat.json", file, choice);
    switch(choice)
    {
    case 1: case 2: case 3:
        s_print_str = json_print_unformat(json, nitem, &s_print_size, NULL);
        break;
    case 4: case 5:
        json_fprint_unformat(json, nitem, s_dst_json_path, NULL);
        break;
    default:
        break;
    }

    ms[6] = _system_ms_get();
    if (s_print_str) {
        copy_data_to_file(s_print_str, s_print_size, s_dst_json_path);
        json_memory_free(s_print_str);
        s_print_str = NULL;
    }
    ms[7] = _system_ms_get();

    switch(choice)
    {
    case 1: case 2: case 3:
        printf("[dom] %d. read=%-5d parse=%-5d format=%-5d write=%-5d unformat=%-5d write=%-5d\n",
            choice, ms[1]-ms[0], ms[2]-ms[1], ms[3]-ms[2], ms[4]-ms[3], ms[6]-ms[5], ms[7]-ms[6]);
        break;
    case 4: case 5:
        printf("[dom] %d. read+parse=%-5d       format+write=%-5d       unformat+write=%-5d\n",
            choice, ms[2]-ms[1], ms[3]-ms[2], ms[6]-ms[5]);
        break;
    default:
        break;
    }

    if (!nflag && json)
        json_del_object(json);
    pjson_memory_free(&mem);
end:
    if (data)
        read_file_data_free(&data, &size);
    return ret;

err:
    usage_print(argv[0]);
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
andq $0x0, 0x18(%rsp)
movq %rsi, %r13
andq $0x0, 0x10(%rsp)
movl %edi, %ebp
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rbx
movaps %xmm0, 0x50(%rbx)
movaps %xmm0, 0x40(%rbx)
movaps %xmm0, 0x30(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, (%rbx)
andq $0x0, 0x60(%rbx)
movq %rbx, %rdi
callq 0x201c
cmpl $0x3, %ebp
je 0x18eb
cmpl $0x2, %ebp
jne 0x191b
movq 0x8(%r13), %rdi
callq 0x11d0
testl %eax, %eax
jne 0x191b
callq 0x146f
xorl %ebx, %ebx
jmp 0x19da
movq 0x8(%r13), %r14
leaq 0x6798(%rip), %r15      # 0x808e
cmpb $0x0, (%r14)
je 0x190e
movq %r14, %rdi
xorl %esi, %esi
callq 0x11b0
testl %eax, %eax
je 0x19ee
movq %r15, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x10c0
movq (%r13), %rbx
leaq 0x68b3(%rip), %rdi      # 0x81d9
leaq 0x68c2(%rip), %r14      # 0x81ef
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x68b5(%rip), %rdi      # 0x81f6
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x68c5(%rip), %rdi      # 0x821a
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x68da(%rip), %rdi      # 0x8243
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x68f0(%rip), %rdi      # 0x826d
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x6901(%rip), %rdi      # 0x8292
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x6917(%rip), %rdi      # 0x82bc
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x692b(%rip), %rdi      # 0x82e4
movq %rbx, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0x6940(%rip), %rdi      # 0x830d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x10c0
pushq $-0x1
popq %rbx
movl %ebx, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%r13), %rdi
callq 0x11d0
movl %eax, %r12d
leal -0x8(%r12), %eax
cmpl $-0x7, %eax
jb 0x191b
movl %r12d, %r15d
callq 0x1f21
movl %eax, %ebp
cmpl $0x6, %r12d
ja 0x1a37
pushq $0x4e
popq %rax
btl %r15d, %eax
jae 0x1a37
leaq 0x10(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x1368
testl %eax, %eax
js 0x1a79
callq 0x1f21
movl %eax, %r13d
decl %r12d
leaq 0x65df(%rip), %rax      # 0x8028
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, (%rdi)
movq %rax, 0x30(%rdi)
movq %rcx, 0x10(%rdi)
jmp 0x1af5
leaq 0x6620(%rip), %r15      # 0x80a0
jmp 0x190e
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x20(%rdi)
andq $0x0, 0x30(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %r14, 0x28(%rdi)
movq %rbx, 0x20(%rdi)
jmp 0x1b2b
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %rax, 0x30(%rdi)
movq %rbx, 0x20(%rdi)
movq %rcx, 0x10(%rdi)
movb $0x1, 0x18(%rdi)
jmp 0x1b2b
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
andq $0x0, 0x30(%rdi)
movq %r14, 0x28(%rdi)
callq 0x3801
movb $0x1, %cl
movl %ecx, 0xc(%rsp)
jmp 0x1b38
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %rax, 0x30(%rdi)
movq %rbx, 0x20(%rdi)
movq %rcx, 0x10(%rdi)
callq 0x3801
movl $0x0, 0xc(%rsp)
testq %rax, %rax
je 0x1b85
movq %rax, 0x28(%rsp)
movl %r13d, 0x3c(%rsp)
callq 0x1f21
movl %eax, 0x24(%rsp)
leaq 0xc5c9(%rip), %rbx      # 0xe120
leaq 0x6558(%rip), %rdx      # 0x80b6
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movq %r14, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x10d0
cmpb $0x0, 0xc(%rsp)
je 0x1b99
movq 0x18(%rsp), %r13
shrq $0x5, %r13
jmp 0x1bb3
leaq 0x67e4(%rip), %rdi      # 0x8370
callq 0x1060
pushq $-0x1
popq %rbx
jmp 0x1db6
leaq 0x88(%rsp), %rdi
callq 0x20d2
cltq
pushq $0x28
popq %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r13
cmpl $0x2, %r12d
movq 0x28(%rsp), %rdi
xorps %xmm0, %xmm0
ja 0x1bf4
leaq 0x40(%rsp), %rbx
movaps %xmm0, 0x10(%rbx)
movaps %xmm0, 0x20(%rbx)
movaps %xmm0, (%rbx)
movb $0x1, 0x1c(%rbx)
movl %r13d, 0x18(%rbx)
movq %rbx, %rsi
callq 0x21d0
movq (%rbx), %rcx
movq %rcx, 0xc635(%rip)      # 0xe220
movq %rax, 0xc516(%rip)      # 0xe108
jmp 0x1c16
leaq 0x40(%rsp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movb $0x1, 0x1c(%rsi)
movl %r13d, 0x18(%rsi)
movq %rbx, 0x28(%rsi)
andq $0x0, 0x20(%rsi)
callq 0x21d0
movl %ebp, 0x38(%rsp)
callq 0x1f21
movl %eax, %ebx
movq 0xc4e0(%rip), %rdi      # 0xe108
testq %rdi, %rdi
je 0x1c54
movq 0xc5ec(%rip), %rsi      # 0xe220
leaq 0xc4e5(%rip), %rdx      # 0xe120
callq 0x12e9
movq 0xc4c1(%rip), %rdi      # 0xe108
callq 0x1f50
andq $0x0, 0xc4b4(%rip)      # 0xe108
callq 0x1f21
movl %eax, 0x34(%rsp)
callq 0x1f21
movl %eax, 0x20(%rsp)
leaq 0xc4b3(%rip), %rbp      # 0xe120
leaq 0x64b4(%rip), %rdx      # 0x8128
movl $0x100, %esi            # imm = 0x100
movq %rbp, %rdi
movq %r14, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x10d0
xorps %xmm0, %xmm0
cmpl $0x2, %r12d
ja 0x1cc9
leaq 0x40(%rsp), %r14
movaps %xmm0, 0x10(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, (%r14)
movl %r13d, 0x18(%r14)
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x21d0
movq (%r14), %rcx
movq %rcx, 0xc560(%rip)      # 0xe220
movq %rax, 0xc441(%rip)      # 0xe108
jmp 0x1cec
leaq 0x40(%rsp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movl %r13d, 0x18(%rsi)
movq %rbp, 0x28(%rsi)
andq $0x0, 0x20(%rsi)
movq 0x28(%rsp), %rdi
callq 0x21d0
callq 0x1f21
movl %eax, %r14d
movq 0xc40d(%rip), %rdi      # 0xe108
testq %rdi, %rdi
movl 0x3c(%rsp), %ebp
je 0x1d2b
movq 0xc515(%rip), %rsi      # 0xe220
leaq 0xc40e(%rip), %rdx      # 0xe120
callq 0x12e9
movq 0xc3ea(%rip), %rdi      # 0xe108
callq 0x1f50
andq $0x0, 0xc3dd(%rip)      # 0xe108
callq 0x1f21
cmpl $0x2, %r12d
ja 0x1d73
movl %eax, %r10d
movl 0x34(%rsp), %r9d
subl %ebx, %r9d
movl 0x24(%rsp), %ecx
subl %ecx, %ebx
subl %ebp, %ecx
subl 0x38(%rsp), %ebp
subl %r14d, %r10d
subl 0x20(%rsp), %r14d
leaq 0x63e0(%rip), %rdi      # 0x813c
movl %r15d, %esi
movl %ebp, %edx
movl %ebx, %r8d
xorl %eax, %eax
pushq %r10
pushq %r14
callq 0x10c0
popq %rcx
popq %rdx
jmp 0x1d96
movl 0x24(%rsp), %edx
subl %edx, %ebx
subl %ebp, %edx
subl 0x20(%rsp), %r14d
leaq 0x6405(%rip), %rdi      # 0x818c
movl %r15d, %esi
movl %ebx, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x10c0
cmpb $0x0, 0xc(%rsp)
movq 0x28(%rsp), %rdi
je 0x1da7
callq 0x1f55
leaq 0x80(%rsp), %rdi
callq 0x2065
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
callq 0x1030
jmp 0x19da
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %rcx, 0x10(%rdi)
leaq -0x752(%rip), %rcx      # 0x1697
movq %rcx, 0x20(%rdi)
movq %rax, 0x8(%rdi)
shrq $0x5, %rax
movl %eax, 0xc321(%rip)      # 0xe11c
callq 0x5040
testl %eax, %eax
js 0x1f09
movl %ebp, %r15d
callq 0x1f21
movl %eax, %ebp
cmpq $0x0, 0xc2ee(%rip)      # 0xe108
je 0x1e6a
leaq 0xc2fd(%rip), %rbx      # 0xe120
leaq 0x628c(%rip), %rdx      # 0x80b6
pushq $0x6
popq %r8
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movq %r14, %rcx
xorl %eax, %eax
callq 0x10d0
movq 0xc2c1(%rip), %rdi      # 0xe108
movq 0xc3d2(%rip), %rsi      # 0xe220
movq %rbx, %rdx
callq 0x12e9
movq 0xc2ab(%rip), %rdi      # 0xe108
callq 0x1f50
andq $0x0, 0xc29e(%rip)      # 0xe108
callq 0x1f21
subl %ebp, %eax
subl %r13d, %ebp
subl %r15d, %r13d
leaq 0x624a(%rip), %rdi      # 0x80c8
xorl %ebx, %ebx
pushq $0x6
popq %rsi
movl %r13d, %edx
movl %ebp, %ecx
movl %eax, %r8d
xorl %eax, %eax
callq 0x10c0
jmp 0x1db6
movb $0x1, 0xc27a(%rip)      # 0xe118
leaq 0xc27b(%rip), %rdi      # 0xe120
leaq 0x620a(%rip), %rdx      # 0x80b6
xorl %ebx, %ebx
pushq $0x7
popq %r8
movl $0x100, %esi            # imm = 0x100
movq %r14, %rcx
xorl %eax, %eax
callq 0x10d0
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rdi
andq $0x0, 0x10(%rdi)
movaps %xmm0, (%rdi)
movq %r14, 0x18(%rdi)
leaq -0x845(%rip), %rax      # 0x1697
movq %rax, 0x20(%rdi)
callq 0x5040
testl %eax, %eax
js 0x1f15
callq 0x1f21
subl %r13d, %eax
leaq 0x6208(%rip), %rdi      # 0x8100
pushq $0x7
popq %rsi
movl %eax, %edx
xorl %eax, %eax
callq 0x10c0
jmp 0x1db6
leaq 0x6445(%rip), %rdi      # 0x8355
jmp 0x1b8c
leaq 0x641d(%rip), %rdi      # 0x8339
jmp 0x1b8c