// File: /workspace/github_cpp_makefile_star2_O2/sisong[P]ApkDiffPatch/src/apk_normalized.cpp
// Function: normalized_cmd_line(int, char const**)
int normalized_cmd_line(int argc, const char * argv[]){
    hpatch_BOOL isNotCompressEmptyFile=_kNULL_VALUE;
    size_t      pageAlignSoFile=_kNULL_SIZE;
    hpatch_BOOL pageAlignSoFileCompatible=_kNULL_VALUE;
    hpatch_BOOL isOutputVersion=_kNULL_VALUE;
    size_t      compressLevel = _kNULL_SIZE;
    size_t      alignSize     = _kNULL_SIZE;
    #define kMax_arg_values_size 2
    const char * arg_values[kMax_arg_values_size]={0};
    int          arg_values_size=0;
    int         i;
    for (i=1; i<argc; ++i) {
        const char* op=argv[i];
        _options_check((op!=0)&&(strlen(op)>0),"?");
        if (op[0]!='-'){
            _options_check(arg_values_size<kMax_arg_values_size,"count");
            arg_values[arg_values_size]=op;
            ++arg_values_size;
            continue;
        }
        _options_check((op!=0)&&(op[0]=='-'),"?");
        switch (op[1]) {
            case 'v':{
                _options_check((isOutputVersion==_kNULL_VALUE)&&(op[2]=='\0'),"-v");
                isOutputVersion=hpatch_TRUE;
            } break;
            case 'q':{
                _options_check((op[2]=='\0'),"-q");
                g_isPrintNormalizingFileName=false;
            } break;
            case 'n':{
                if ((op[2]=='c')&&(op[3]=='e')&&(op[4]=='-')&&((op[5]=='0')||(op[5]=='1'))){
                    _options_check(isNotCompressEmptyFile==_kNULL_VALUE,"-nce-?");
                    isNotCompressEmptyFile=(hpatch_BOOL)(op[5]=='1');
                }else{
                    _options_check(hpatch_FALSE,"-nce?");
                }
            } break;
            case 'c':{
                if ((op[2]=='l')&&(op[3]=='-')){
                    _options_check(compressLevel==_kNULL_SIZE,"-cl-?")
                    const char* pnum=op+4;
                    _options_check(kmg_to_size(pnum,strlen(pnum),&compressLevel),"-cl-?");
                    _options_check((0<=compressLevel)&&(compressLevel<=9),"-cl-?");
                }else{
                    _options_check(hpatch_FALSE,"-cl?");
                }
            } break;
            case 'a':{
                if ((op[2]=='p')&&(op[3]=='-')){
                    _options_check(pageAlignSoFile==_kNULL_SIZE,"-ap-?");
                    const char* pnum=op+4;
                    pageAlignSoFileCompatible=(pnum[0]=='c')||(pnum[0]=='C');
                    if (pageAlignSoFileCompatible) ++pnum;
                    _options_check(kmg_to_size(pnum,strlen(pnum),&pageAlignSoFile),"-ap-?");
                    #define _kMaxPageAlignSize      (64*1024)
                    #define _kDefaultPageAlignSize  (4*1024)
                    if (pageAlignSoFile==1) pageAlignSoFile=_kDefaultPageAlignSize;
                    _options_check((pageAlignSoFile==0)||(pageAlignSoFile==16*1024)
                                 ||(pageAlignSoFile==_kDefaultPageAlignSize)||(pageAlignSoFile==_kMaxPageAlignSize),"-ap-?");
                }else if ((op[2]=='s')&&(op[3]=='-')){
                    _options_check(alignSize==_kNULL_SIZE,"-as-?")
                    const char* pnum=op+4;
                    _options_check(kmg_to_size(pnum,strlen(pnum),&alignSize),"-as-?");
                    _options_check((1<=alignSize)&&(alignSize<=_kMaxPageAlignSize),"-as-?");
                }else{
                    _options_check(hpatch_FALSE,"-a?");
                }
            } break;
            default: {
                _options_check(hpatch_FALSE,"-?");
            } break;
        }//swich
    }
    if (isNotCompressEmptyFile==_kNULL_VALUE)
        isNotCompressEmptyFile=hpatch_TRUE;
    if (pageAlignSoFile==_kNULL_SIZE)
        pageAlignSoFile=_kDefaultPageAlignSize;
    if (pageAlignSoFile==_kDefaultPageAlignSize)
        pageAlignSoFileCompatible=hpatch_TRUE;
    if (compressLevel==_kNULL_SIZE)
        compressLevel=kDefaultZlibCompressLevel;
    if (alignSize==_kNULL_SIZE)
        alignSize=kDefaultZipAlignSize;
    if (isOutputVersion==_kNULL_VALUE)
        isOutputVersion=hpatch_FALSE;
    if (isOutputVersion){
        printf("ApkDiffPatch::ApkNormalized v" APKDIFFPATCH_VERSION_STRING "\n\n");
        if (arg_values_size==0)
            return _kResult_OK;
    }

    if (compressLevel>6)
        printf("WARNING: not recommended 7,8,9, compress ratio is slightly higher, but compress speed is very slow when patching.\n");
        
    _options_check(arg_values_size==2,"count");
    const char* srcApk=arg_values[0];
    const char* dstApk=arg_values[1];
    hpatch_printPath_utf8((std::string("src: \"")+srcApk+"\"\n").c_str());
    hpatch_printPath_utf8((std::string("out: \"")+dstApk+"\"\n").c_str());
    double time0=clock_s();
    int apkFilesRemoved=0;
    if (!ZipNormalized(srcApk,dstApk,(int)alignSize,(int)compressLevel,
                       (bool)isNotCompressEmptyFile,pageAlignSoFile,pageAlignSoFileCompatible,&apkFilesRemoved)){
        printf("\nrun ApkNormalized ERROR!\n");
        return _kResult_NORMALIZED_ERROR;
    }
    printf("run ApkNormalized ok!\n");
    
    TCompressedFilesInfos cInfos;
    if (!getCompressedFilesInfos(dstApk,&cInfos)){
        printf("\nApkNormalized result file open ERROR!\n");
        return _kResult_OPEN_NORMALIZED_ERROR;
    }
    printf("  NOTE: recompressed files count %d, data size %" PRIu64 " compress to %" PRIu64 "\n",
            cInfos.compressedCount,cInfos.sumCompressedUncompressedSize,cInfos.sumCompressedSize);
    
    //check
    if (!getZipIsSame(srcApk,dstApk,apkFilesRemoved)){
        printf("\nApkNormalized result file check ERROR!\n");
        return _kResult_CHECK_NORMALIZED_DATA_ERROR;
    }
    printf("  check ApkNormalized result ok!\n");

    double time1=clock_s();
    printf("\nApkNormalized time: %.3f s\n",(time1-time0));
    return _kResult_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rsi, 0x18(%rsp)
leaq 0x20(%rsp), %rax
orq $-0x1, (%rax)
leaq 0x58(%rsp), %rax
orq $-0x1, (%rax)
leaq 0x50(%rsp), %rax
orq $-0x1, (%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
movslq %edi, %r12
pushq $-0x1
popq %rbp
xorl %r15d, %r15d
pushq $0x1
popq %r14
pushq $-0x1
popq %rax
movq %rax, %rdx
movq %rax, 0x10(%rsp)
movq %rax, %rsi
movl %ebp, 0xc(%rsp)
movl %ebp, 0x8(%rsp)
cmpq %r12, %r14
jge 0x39a3
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %r13
testq %r13, %r13
je 0x3997
movzbl (%r13), %eax
cmpl $0x2d, %eax
je 0x373c
testl %eax, %eax
je 0x3997
cmpl $0x2, %r15d
jge 0x3bc9
movl %r15d, %eax
movq %r13, 0x80(%rsp,%rax,8)
incl %r15d
incq %r14
jmp 0x36f2
movzbl 0x1(%r13), %eax
cmpl $0x61, %eax
je 0x3844
cmpl $0x63, %eax
je 0x377e
cmpl $0x6e, %eax
je 0x37f6
cmpl $0x71, %eax
je 0x37dc
cmpl $0x76, %eax
jne 0x3c29
cmpl $-0x1, %ebp
jne 0x3be3
cmpb $0x0, 0x2(%r13)
pushq $0x1
popq %rbp
je 0x3737
jmp 0x3be3
cmpb $0x6c, 0x2(%r13)
jne 0x3c07
cmpb $0x2d, 0x3(%r13)
jne 0x3c07
cmpq $-0x1, %rdx
jne 0x3c3b
movq %rsi, %rbx
addq $0x4, %r13
movq %r13, %rdi
callq 0x3140
movq %r13, %rdi
movq %rax, %rsi
leaq 0x58(%rsp), %rdx
callq 0x3d0a
testl %eax, %eax
je 0x3c3d
movq 0x58(%rsp), %rdx
cmpq $0xa, %rdx
movq %rbx, %rsi
jb 0x3737
jmp 0x3c3f
cmpb $0x0, 0x2(%r13)
jne 0x3c32
leaq 0x2caf2(%rip), %rax     # 0x302e0
movb $0x0, (%rax)
jmp 0x3737
cmpb $0x63, 0x2(%r13)
jne 0x3bd7
cmpb $0x65, 0x3(%r13)
jne 0x3bd7
cmpb $0x2d, 0x4(%r13)
jne 0x3bd7
movb 0x5(%r13), %al
movl %eax, %ecx
andb $-0x2, %cl
cmpb $0x30, %cl
jne 0x3bd7
cmpl $-0x1, 0x8(%rsp)
jne 0x3c48
xorl %ecx, %ecx
cmpb $0x31, %al
sete %cl
movl %ecx, 0x8(%rsp)
jmp 0x3737
movzbl 0x2(%r13), %eax
cmpl $0x73, %eax
je 0x388b
cmpl $0x70, %eax
jne 0x3c10
cmpb $0x2d, 0x3(%r13)
jne 0x3c10
cmpq $-0x1, %rsi
jne 0x3c61
movb 0x4(%r13), %al
andb $-0x21, %al
cmpb $0x43, %al
sete %bl
movq %rdx, 0x28(%rsp)
je 0x390e
addq $0x4, %r13
jmp 0x3912
cmpb $0x2d, 0x3(%r13)
jne 0x3c10
cmpq $-0x1, 0x10(%rsp)
jne 0x3c63
movq %rsi, 0x98(%rsp)
movq %r12, 0xa0(%rsp)
movl %ebp, %ebx
movq %rdx, %rbp
addq $0x4, %r13
movq %r13, %rdi
callq 0x3140
movq %r13, %rdi
movq %rax, %rsi
leaq 0x50(%rsp), %rdx
callq 0x3d0a
testl %eax, %eax
je 0x3c6e
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsp)
decq %rax
cmpq $0x10000, %rax          # imm = 0x10000
movq %rbp, %rdx
movl %ebx, %ebp
movq 0xa0(%rsp), %r12
movq 0x98(%rsp), %rsi
jb 0x3737
jmp 0x3c70
addq $0x5, %r13
movq %r13, %rdi
callq 0x3140
movq %r13, %rdi
movq %rax, %rsi
leaq 0x20(%rsp), %rdx
callq 0x3d0a
testl %eax, %eax
je 0x3c65
xorl %eax, %eax
movb %bl, %al
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
movl %eax, 0xc(%rsp)
je 0x398d
cmpq $0x10000, %rsi          # imm = 0x10000
movq 0x28(%rsp), %rdx
je 0x3737
cmpq $0x1000, %rsi           # imm = 0x1000
je 0x3737
cmpq $0x4000, %rsi           # imm = 0x4000
je 0x3737
cmpq $0x1, %rsi
jne 0x3c9a
movq $0x1000, 0x20(%rsp)     # imm = 0x1000
movl $0x1000, %esi           # imm = 0x1000
jmp 0x3737
movq 0x28(%rsp), %rdx
jmp 0x3737
leaq 0x1a37d(%rip), %rdi     # 0x1dd1b
jmp 0x3c77
cmpq $-0x1, %rsi
movl $0x1000, %ebx           # imm = 0x1000
cmovneq %rsi, %rbx
cmpq $-0x1, %rdx
pushq $0x6
popq %r13
cmovneq %rdx, %r13
incl %ebp
cmpl $0x2, %ebp
jb 0x39d8
leaq 0x1a21b(%rip), %rdi     # 0x1dbe5
callq 0x3440
testl %r15d, %r15d
je 0x3c02
cmpq $0x7, %r13
jb 0x39ea
leaq 0x1a224(%rip), %rdi     # 0x1dc09
callq 0x3440
cmpl $0x2, %r15d
jne 0x3bcb
movq 0x80(%rsp), %r14
movq 0x88(%rsp), %r12
leaq 0x19605(%rip), %rsi     # 0x1d010
leaq 0xa8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x3e60
leaq 0x60(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
movq %r14, %rdx
callq 0x3e41
leaq 0x195de(%rip), %rdx     # 0x1d017
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x3e41
movq 0x30(%rsp), %rdi
callq 0x11fb4
leaq 0x30(%rsp), %rdi
callq 0x3160
leaq 0x60(%rsp), %rdi
callq 0x3160
leaq 0xa8(%rsp), %rdi
callq 0x3160
leaq 0x195a0(%rip), %rsi     # 0x1d01a
leaq 0xa8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x3e60
leaq 0x60(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
movq %r12, %rdx
callq 0x3e41
leaq 0x1956f(%rip), %rdx     # 0x1d017
leaq 0x30(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x3e41
movq 0x30(%rsp), %rdi
callq 0x11fb4
cmpq $0x1000, %rbx           # imm = 0x1000
sete %bpl
leaq 0x30(%rsp), %rdi
callq 0x3160
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x3160
leaq 0xa8(%rsp), %rdi
callq 0x3160
callq 0x3dee
andl $0x0, (%r15)
movsd %xmm0, 0x18(%rsp)
movq 0x10(%rsp), %rax
cmpq $-0x1, %rax
pushq $0x8
popq %rdx
cmovnel %eax, %edx
xorl %r8d, %r8d
cmpl $0x0, 0x8(%rsp)
setne %r8b
cmpl $0x0, 0xc(%rsp)
setne %al
orb %bpl, %al
movzbl %al, %eax
movq %r14, %rdi
movq %r12, %rsi
movl %r13d, %ecx
movq %rbx, %r9
pushq %r15
pushq %rax
callq 0x3f34
popq %rcx
popq %rdx
testb %al, %al
je 0x3bef
leaq 0x1a148(%rip), %rdi     # 0x1dc95
callq 0x3440
leaq 0x30(%rsp), %rsi
movq %r12, %rdi
callq 0x696b
testb %al, %al
je 0x3c19
movl 0x34(%rsp), %esi
movq 0x48(%rsp), %rdx
movq 0x40(%rsp), %rcx
leaq 0x194a5(%rip), %rdi     # 0x1d021
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x3050
movl 0x60(%rsp), %edx
movq %r14, %rdi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x66b0
testb %al, %al
je 0x3c51
leaq 0x1a154(%rip), %rdi     # 0x1dcfa
callq 0x3440
callq 0x3dee
subsd 0x18(%rsp), %xmm0
leaq 0x194aa(%rip), %rdi     # 0x1d067
movb $0x1, %al
callq 0x3050
jmp 0x3c85
jmp 0x3bcb
leaq 0x1a221(%rip), %rdi     # 0x1ddf3
jmp 0x3c77
leaq 0x1a1b4(%rip), %rdi     # 0x1dd92
jmp 0x3c77
leaq 0x1a1e5(%rip), %rdi     # 0x1ddcf
jmp 0x3c77
leaq 0x1a085(%rip), %rdi     # 0x1dc7b
callq 0x3440
pushq $0x2
jmp 0x3c83
xorl %r15d, %r15d
jmp 0x3c85
leaq 0x1a15b(%rip), %rdi     # 0x1dd69
jmp 0x3c77
leaq 0x1a115(%rip), %rdi     # 0x1dd2c
jmp 0x3c77
leaq 0x1a08b(%rip), %rdi     # 0x1dcab
callq 0x3440
pushq $0x3
jmp 0x3c83
leaq 0x1a1b1(%rip), %rdi     # 0x1dde1
jmp 0x3c77
leaq 0x1a184(%rip), %rdi     # 0x1ddbd
jmp 0x3c77
jmp 0x3c3f
jmp 0x3c3f
leaq 0x1a137(%rip), %rdi     # 0x1dd7d
jmp 0x3c77
leaq 0x1a158(%rip), %rdi     # 0x1dda7
jmp 0x3c77
leaq 0x1a07a(%rip), %rdi     # 0x1dcd2
callq 0x3440
pushq $0x4
jmp 0x3c83
jmp 0x3c65
jmp 0x3c70
leaq 0x1a0e8(%rip), %rdi     # 0x1dd54
jmp 0x3c77
jmp 0x3c70
leaq 0x1a0c8(%rip), %rdi     # 0x1dd3f
callq 0x3440
callq 0x3cfc
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c65
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3160
jmp 0x3cae
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3160
jmp 0x3ce2
movq %rax, %rbx
jmp 0x3ce2
jmp 0x3cf1
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x3160
jmp 0x3cd3
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3160
jmp 0x3ce2
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x3160
jmp 0x3cf4
movq %rax, %rbx
movq %rbx, %rdi
callq 0x34f0