// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x108, %rsp            # imm = 0x108
movl $0x0, 0x104(%rsp)
movl %edi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movl $0x0, 0xf4(%rsp)
movq $0x0, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movl $0x0, 0xe0(%rsp)
movl $0x0, 0xdc(%rsp)
movq $0x0, 0xd0(%rsp)
movq $0x0, 0xc8(%rsp)
movq $0x0, 0xc0(%rsp)
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x10f0
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x10f0
leaq 0x58(%rsp), %rdi
callq 0x2df0
cmpl $0x2, 0x100(%rsp)
jne 0x1edf
movq 0xf8(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x11d0
movl %eax, 0xdc(%rsp)
cmpl $0x0, 0xdc(%rsp)
jne 0x1eda
callq 0x1610
movl $0x0, 0x104(%rsp)
jmp 0x25d7
jmp 0x25bc
cmpl $0x3, 0x100(%rsp)
je 0x1eee
jmp 0x25bc
movq 0xf8(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x10b0
cmpq $0x0, %rax
je 0x1f29
movq 0xd0(%rsp), %rdi
xorl %esi, %esi
callq 0x11b0
cmpl $0x0, %eax
je 0x1f44
movq 0xd0(%rsp), %rsi
leaq 0xe156(%rip), %rdi      # 0x1008e
movb $0x0, %al
callq 0x10c0
jmp 0x25bc
movq 0xf8(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x11d0
movl %eax, 0xdc(%rsp)
cmpl $0x1, 0xdc(%rsp)
jl 0x1f70
cmpl $0x7, 0xdc(%rsp)
jle 0x1f75
jmp 0x25bc
callq 0x25f0
movl %eax, 0x30(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x2c(%rsp)
decl %eax
subl $0x3, %eax
jb 0x1f9d
jmp 0x1f92
movl 0x2c(%rsp), %eax
subl $0x6, %eax
jne 0x1fdc
jmp 0x1f9d
movq 0xd0(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
leaq 0xe8(%rsp), %rdx
callq 0x13e0
cmpl $0x0, %eax
jge 0x1fda
movq 0xd0(%rsp), %rsi
leaq 0xe0d2(%rip), %rdi      # 0x100a0
movb $0x0, %al
callq 0x10c0
jmp 0x25bc
jmp 0x1fde
jmp 0x1fde
callq 0x25f0
movl %eax, 0x34(%rsp)
movl 0xdc(%rsp), %eax
decl %eax
movl %eax, %ecx
movq %rcx, 0x20(%rsp)
subl $0x6, %eax
ja 0x226e
movq 0x20(%rsp), %rax
leaq 0xe01c(%rip), %rcx      # 0x10028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xc8(%rsp), %rdi
movq 0xe8(%rsp), %rsi
callq 0x2630
movq %rax, 0xc0(%rsp)
jmp 0x2270
movq 0xc8(%rsp), %rdi
movq 0xe8(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x2670
movq %rax, 0xc0(%rsp)
movl $0x1, 0xe0(%rsp)
jmp 0x2270
movq 0xc8(%rsp), %rdi
movq 0xe8(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x26d0
movq %rax, 0xc0(%rsp)
movl $0x1, 0xe0(%rsp)
jmp 0x2270
movq 0xd0(%rsp), %rdi
callq 0x2730
movq %rax, 0xc0(%rsp)
jmp 0x2270
movq 0xd0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2770
movq %rax, 0xc0(%rsp)
movl $0x1, 0xe0(%rsp)
jmp 0x2270
movq 0xe8(%rsp), %rax
shrq $0x5, %rax
movl %eax, 0x140db(%rip)     # 0x161cc
movq 0xc8(%rsp), %rdi
movq 0xe8(%rsp), %rsi
leaq -0x7a8(%rip), %rdx      # 0x1960
callq 0x27b0
cmpl $0x0, %eax
jge 0x2130
leaq 0xdf9d(%rip), %rdi      # 0x100b6
movb $0x0, %al
callq 0x10c0
movl $0xffffffff, 0xf4(%rsp) # imm = 0xFFFFFFFF
jmp 0x258c
callq 0x25f0
movl %eax, 0x38(%rsp)
cmpq $0x0, 0x14077(%rip)     # 0x161b8
je 0x219e
movq 0xd0(%rsp), %rcx
movl 0xdc(%rsp), %r8d
leaq 0x14076(%rip), %rdi     # 0x161d0
movl $0x100, %esi            # imm = 0x100
leaq 0xdf6c(%rip), %rdx      # 0x100d2
movb $0x0, %al
callq 0x10d0
movq 0x14044(%rip), %rdi     # 0x161b8
movq 0x14155(%rip), %rsi     # 0x162d0
leaq 0x1404e(%rip), %rdx     # 0x161d0
callq 0x12f0
movq 0x1402a(%rip), %rdi     # 0x161b8
callq 0x2b00
movq $0x0, 0x1401a(%rip) # 0x161b8
callq 0x25f0
movl %eax, 0x3c(%rsp)
movl 0xdc(%rsp), %esi
movl 0x34(%rsp), %edx
subl 0x30(%rsp), %edx
movl 0x38(%rsp), %ecx
subl 0x34(%rsp), %ecx
movl 0x3c(%rsp), %r8d
subl 0x38(%rsp), %r8d
leaq 0xdf15(%rip), %rdi      # 0x100e4
movb $0x0, %al
callq 0x10c0
jmp 0x258c
movb $0x1, 0x13fe6(%rip)     # 0x161c8
movq 0xd0(%rsp), %rcx
movl 0xdc(%rsp), %r8d
leaq 0x13fd7(%rip), %rdi     # 0x161d0
movl $0x100, %esi            # imm = 0x100
leaq 0xdecd(%rip), %rdx      # 0x100d2
movb $0x0, %al
callq 0x10d0
movq 0xd0(%rsp), %rdi
leaq -0x8bb(%rip), %rsi      # 0x1960
callq 0x2810
cmpl $0x0, %eax
jge 0x2243
leaq 0xdef0(%rip), %rdi      # 0x1011c
movb $0x0, %al
callq 0x10c0
movl $0xffffffff, 0xf4(%rsp) # imm = 0xFFFFFFFF
jmp 0x258c
callq 0x25f0
movl %eax, 0x38(%rsp)
movl 0xdc(%rsp), %esi
movl 0x38(%rsp), %edx
subl 0x34(%rsp), %edx
leaq 0xded7(%rip), %rdi      # 0x10139
movb $0x0, %al
callq 0x10c0
jmp 0x258c
jmp 0x2270
cmpq $0x0, 0xc0(%rsp)
jne 0x2299
leaq 0xdedf(%rip), %rdi      # 0x10161
movb $0x0, %al
callq 0x10c0
movl $0xffffffff, 0xf4(%rsp) # imm = 0xFFFFFFFF
jmp 0x258c
callq 0x25f0
movl %eax, 0x38(%rsp)
movq 0xd0(%rsp), %rcx
movl 0xdc(%rsp), %r8d
leaq 0x13f17(%rip), %rdi     # 0x161d0
movl $0x100, %esi            # imm = 0x100
leaq 0xde0d(%rip), %rdx      # 0x100d2
movb $0x0, %al
callq 0x10d0
cmpl $0x0, 0xe0(%rsp)
je 0x22f9
leaq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x2f50
cltq
movl $0x28, %ecx
xorl %edx, %edx
divq %rcx
movl %eax, 0xe4(%rsp)
jmp 0x230c
movq 0xe8(%rsp), %rax
shrq $0x5, %rax
movl %eax, 0xe4(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x1c(%rsp)
decl %eax
subl $0x3, %eax
jb 0x232e
jmp 0x2320
movl 0x1c(%rsp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0x2356
jmp 0x2377
movq 0xc0(%rsp), %rdi
movl 0xe4(%rsp), %esi
leaq 0x13f8c(%rip), %rdx     # 0x162d0
xorl %eax, %eax
movl %eax, %ecx
callq 0x2850
movq %rax, 0x13e64(%rip)     # 0x161b8
jmp 0x2379
movq 0xc0(%rsp), %rdi
movl 0xe4(%rsp), %esi
leaq 0x13e64(%rip), %rdx     # 0x161d0
xorl %eax, %eax
movl %eax, %ecx
callq 0x28d0
jmp 0x2379
jmp 0x2379
callq 0x25f0
movl %eax, 0x3c(%rsp)
cmpq $0x0, 0x13e2e(%rip)     # 0x161b8
je 0x23bd
movq 0x13e25(%rip), %rdi     # 0x161b8
movq 0x13f36(%rip), %rsi     # 0x162d0
leaq 0x13e2f(%rip), %rdx     # 0x161d0
callq 0x12f0
movq 0x13e0b(%rip), %rdi     # 0x161b8
callq 0x2b00
movq $0x0, 0x13dfb(%rip) # 0x161b8
callq 0x25f0
movl %eax, 0x40(%rsp)
callq 0x25f0
movl %eax, 0x44(%rsp)
movq 0xd0(%rsp), %rcx
movl 0xdc(%rsp), %r8d
leaq 0x13dea(%rip), %rdi     # 0x161d0
leaq 0xdd88(%rip), %rdx      # 0x10175
xorl %eax, %eax
movl $0x100, %esi            # imm = 0x100
callq 0x10d0
movl 0xdc(%rsp), %eax
movl %eax, 0x18(%rsp)
decl %eax
subl $0x3, %eax
jb 0x241b
jmp 0x240d
movl 0x18(%rsp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0x2443
jmp 0x2464
movq 0xc0(%rsp), %rdi
movl 0xe4(%rsp), %esi
leaq 0x13e9f(%rip), %rdx     # 0x162d0
xorl %eax, %eax
movl %eax, %ecx
callq 0x2930
movq %rax, 0x13d77(%rip)     # 0x161b8
jmp 0x2466
movq 0xc0(%rsp), %rdi
movl 0xe4(%rsp), %esi
leaq 0x13d77(%rip), %rdx     # 0x161d0
xorl %eax, %eax
movl %eax, %ecx
callq 0x29b0
jmp 0x2466
jmp 0x2466
callq 0x25f0
movl %eax, 0x48(%rsp)
cmpq $0x0, 0x13d41(%rip)     # 0x161b8
je 0x24aa
movq 0x13d38(%rip), %rdi     # 0x161b8
movq 0x13e49(%rip), %rsi     # 0x162d0
leaq 0x13d42(%rip), %rdx     # 0x161d0
callq 0x12f0
movq 0x13d1e(%rip), %rdi     # 0x161b8
callq 0x2b00
movq $0x0, 0x13d0e(%rip) # 0x161b8
callq 0x25f0
movl %eax, 0x4c(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x14(%rsp)
decl %eax
subl $0x3, %eax
jb 0x24d8
jmp 0x24c7
movl 0x14(%rsp), %eax
addl $-0x4, %eax
subl $0x2, %eax
jb 0x252d
jmp 0x255e
movl 0xdc(%rsp), %esi
movl 0x34(%rsp), %edx
subl 0x30(%rsp), %edx
movl 0x38(%rsp), %ecx
subl 0x34(%rsp), %ecx
movl 0x3c(%rsp), %r8d
subl 0x38(%rsp), %r8d
movl 0x40(%rsp), %r9d
subl 0x3c(%rsp), %r9d
movl 0x48(%rsp), %r10d
subl 0x44(%rsp), %r10d
movl 0x4c(%rsp), %eax
subl 0x48(%rsp), %eax
leaq 0xdc6d(%rip), %rdi      # 0x10189
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x10c0
jmp 0x2560
movl 0xdc(%rsp), %esi
movl 0x38(%rsp), %edx
subl 0x34(%rsp), %edx
movl 0x3c(%rsp), %ecx
subl 0x38(%rsp), %ecx
movl 0x48(%rsp), %r8d
subl 0x44(%rsp), %r8d
leaq 0xdc84(%rip), %rdi      # 0x101d9
movb $0x0, %al
callq 0x10c0
jmp 0x2560
jmp 0x2560
cmpl $0x0, 0xe0(%rsp)
jne 0x2582
cmpq $0x0, 0xc0(%rsp)
je 0x2582
movq 0xc0(%rsp), %rdi
callq 0x2b20
leaq 0x58(%rsp), %rdi
callq 0x2e50
cmpq $0x0, 0xc8(%rsp)
je 0x25ac
leaq 0xc8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x15b0
movl 0xf4(%rsp), %eax
movl %eax, 0x104(%rsp)
jmp 0x25d7
movq 0xf8(%rsp), %rax
movq (%rax), %rdi
callq 0x2a10
movl $0xffffffff, 0x104(%rsp) # imm = 0xFFFFFFFF
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)