// File: /workspace/github_c_makefile_star2_O2/coapp-packages[P]bzip2/bzip2recover.c
// Function: main
Int32 main ( Int32 argc, Char** argv )
{
   FILE*       inFile;
   FILE*       outFile;
   BitStream*  bsIn, *bsWr;
   Int32       b, wrBlock, currBlock, rbCtr;
   MaybeUInt64 bitsRead;

   UInt32      buffHi, buffLo, blockCRC;
   Char*       p;

   strcpy ( progName, argv[0] );
   inFileName[0] = outFileName[0] = 0;

   fprintf ( stderr, 
             "bzip2recover 1.0.6: extracts blocks from damaged .bz2 files.\n" );

   if (argc != 2) {
      fprintf ( stderr, "%s: usage is `%s damaged_file_name'.\n",
                        progName, progName );
      switch (sizeof(MaybeUInt64)) {
         case 8:
            fprintf(stderr, 
                    "\trestrictions on size of recovered file: None\n");
            break;
         case 4:
            fprintf(stderr, 
                    "\trestrictions on size of recovered file: 512 MB\n");
            fprintf(stderr, 
                    "\tto circumvent, recompile with MaybeUInt64 as an\n"
                    "\tunsigned 64-bit int.\n");
            break;
         default:
            fprintf(stderr, 
                    "\tsizeof(MaybeUInt64) is not 4 or 8 -- "
                    "configuration error.\n");
            break;
      }
      exit(1);
   }

   if (strlen(argv[1]) >= BZ_MAX_FILENAME-20) {
      fprintf ( stderr, 
                "%s: supplied filename is suspiciously (>= %d chars) long.  Bye!\n",
                progName, (int)strlen(argv[1]) );
      exit(1);
   }

   strcpy ( inFileName, argv[1] );

   inFile = fopen ( inFileName, "rb" );
   if (inFile == NULL) {
      fprintf ( stderr, "%s: can't read `%s'\n", progName, inFileName );
      exit(1);
   }

   bsIn = bsOpenReadStream ( inFile );
   fprintf ( stderr, "%s: searching for block boundaries ...\n", progName );

   bitsRead = 0;
   buffHi = buffLo = 0;
   currBlock = 0;
   bStart[currBlock] = 0;

   rbCtr = 0;

   while (True) {
      b = bsGetBit ( bsIn );
      bitsRead++;
      if (b == 2) {
         if (bitsRead >= bStart[currBlock] &&
            (bitsRead - bStart[currBlock]) >= 40) {
            bEnd[currBlock] = bitsRead-1;
            if (currBlock > 0)
               fprintf ( stderr, "   block %d runs from " MaybeUInt64_FMT 
                                 " to " MaybeUInt64_FMT " (incomplete)\n",
                         currBlock,  bStart[currBlock], bEnd[currBlock] );
         } else
            currBlock--;
         break;
      }
      buffHi = (buffHi << 1) | (buffLo >> 31);
      buffLo = (buffLo << 1) | (b & 1);
      if ( ( (buffHi & 0x0000ffff) == BLOCK_HEADER_HI 
             && buffLo == BLOCK_HEADER_LO)
           || 
           ( (buffHi & 0x0000ffff) == BLOCK_ENDMARK_HI 
             && buffLo == BLOCK_ENDMARK_LO)
         ) {
         if (bitsRead > 49) {
            bEnd[currBlock] = bitsRead-49;
         } else {
            bEnd[currBlock] = 0;
         }
         if (currBlock > 0 &&
	     (bEnd[currBlock] - bStart[currBlock]) >= 130) {
            fprintf ( stderr, "   block %d runs from " MaybeUInt64_FMT 
                              " to " MaybeUInt64_FMT "\n",
                      rbCtr+1,  bStart[currBlock], bEnd[currBlock] );
            rbStart[rbCtr] = bStart[currBlock];
            rbEnd[rbCtr] = bEnd[currBlock];
            rbCtr++;
         }
         if (currBlock >= BZ_MAX_HANDLED_BLOCKS)
            tooManyBlocks(BZ_MAX_HANDLED_BLOCKS);
         currBlock++;

         bStart[currBlock] = bitsRead;
      }
   }

   bsClose ( bsIn );

   /*-- identified blocks run from 1 to rbCtr inclusive. --*/

   if (rbCtr < 1) {
      fprintf ( stderr,
                "%s: sorry, I couldn't find any block boundaries.\n",
                progName );
      exit(1);
   };

   fprintf ( stderr, "%s: splitting into blocks\n", progName );

   inFile = fopen ( inFileName, "rb" );
   if (inFile == NULL) {
      fprintf ( stderr, "%s: can't open `%s'\n", progName, inFileName );
      exit(1);
   }
   bsIn = bsOpenReadStream ( inFile );

   /*-- placate gcc's dataflow analyser --*/
   blockCRC = 0; bsWr = 0;

   bitsRead = 0;
   outFile = NULL;
   wrBlock = 0;
   while (True) {
      b = bsGetBit(bsIn);
      if (b == 2) break;
      buffHi = (buffHi << 1) | (buffLo >> 31);
      buffLo = (buffLo << 1) | (b & 1);
      if (bitsRead == 47+rbStart[wrBlock]) 
         blockCRC = (buffHi << 16) | (buffLo >> 16);

      if (outFile != NULL && bitsRead >= rbStart[wrBlock]
                          && bitsRead <= rbEnd[wrBlock]) {
         bsPutBit ( bsWr, b );
      }

      bitsRead++;

      if (bitsRead == rbEnd[wrBlock]+1) {
         if (outFile != NULL) {
            bsPutUChar ( bsWr, 0x17 ); bsPutUChar ( bsWr, 0x72 );
            bsPutUChar ( bsWr, 0x45 ); bsPutUChar ( bsWr, 0x38 );
            bsPutUChar ( bsWr, 0x50 ); bsPutUChar ( bsWr, 0x90 );
            bsPutUInt32 ( bsWr, blockCRC );
            bsClose ( bsWr );
         }
         if (wrBlock >= rbCtr) break;
         wrBlock++;
      } else
      if (bitsRead == rbStart[wrBlock]) {
         /* Create the output file name, correctly handling leading paths. 
            (31.10.2001 by Sergey E. Kusikov) */
         Char* split;
         Int32 ofs, k;
         for (k = 0; k < BZ_MAX_FILENAME; k++) 
            outFileName[k] = 0;
         strcpy (outFileName, inFileName);
         split = strrchr (outFileName, BZ_SPLIT_SYM);
         if (split == NULL) {
            split = outFileName;
         } else {
            ++split;
	 }
	 /* Now split points to the start of the basename. */
         ofs  = split - outFileName;
         sprintf (split, "rec%5d", wrBlock+1);
         for (p = split; *p != 0; p++) if (*p == ' ') *p = '0';
         strcat (outFileName, inFileName + ofs);

         if ( !endsInBz2(outFileName)) strcat ( outFileName, ".bz2" );

         fprintf ( stderr, "   writing block %d to `%s' ...\n",
                           wrBlock+1, outFileName );

         outFile = fopen ( outFileName, "wb" );
         if (outFile == NULL) {
            fprintf ( stderr, "%s: can't write `%s'\n",
                      progName, outFileName );
            exit(1);
         }
         bsWr = bsOpenWriteStream ( outFile );
         bsPutUChar ( bsWr, BZ_HDR_B );    
         bsPutUChar ( bsWr, BZ_HDR_Z );    
         bsPutUChar ( bsWr, BZ_HDR_h );    
         bsPutUChar ( bsWr, BZ_HDR_0 + 9 );
         bsPutUChar ( bsWr, 0x31 ); bsPutUChar ( bsWr, 0x41 );
         bsPutUChar ( bsWr, 0x59 ); bsPutUChar ( bsWr, 0x26 );
         bsPutUChar ( bsWr, 0x53 ); bsPutUChar ( bsWr, 0x59 );
      }
   }

   fprintf ( stderr, "%s: finished\n", progName );
   return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq (%rsi), %rsi
leaq 0x2e77(%rip), %rdi      # 0x40d0
callq 0x1050
xorl %eax, %eax
movb %al, 0x363a(%rip)       # 0x48a0
movb %al, 0x3e04(%rip)       # 0x5070
movq 0x2d85(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xd87(%rip), %rdi       # 0x2004
pushq $0x3d
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
cmpl $0x2, %ebp
jne 0x185a
movq 0x8(%rbx), %rbx
movq %rbx, %rdi
callq 0x1070
cmpq $0x7bc, %rax            # imm = 0x7BC
jae 0x1896
leaq 0x3dc0(%rip), %r14      # 0x5070
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1050
leaq 0xe16(%rip), %rsi       # 0x20d8
movq %r14, %rdi
callq 0x10d0
testq %rax, %rax
je 0x18b9
movq %rax, %rdi
callq 0x1937
movq %rax, %rbx
movq 0x2d13(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xe01(%rip), %rsi       # 0x20f0
leaq 0x2dda(%rip), %rdx      # 0x40d0
xorl %eax, %eax
callq 0x10a0
andq $0x0, 0x453b(%rip)      # 0x5840
xorl %ebp, %ebp
movq $0x0, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %r15d, %r15d
incq %r14
movq %r14, %rcx
movq %rcx, %r14
movq %rbx, %rdi
callq 0x195b
movl %ebp, %r12d
cmpl $0x2, %eax
je 0x1426
shldl $0x1, %r15d, %r13d
leal (%rax,%r15,2), %r15d
movzwl %r13w, %eax
movl %eax, %ecx
xorl $0x3141, %ecx           # imm = 0x3141
movl %r15d, %edx
xorl $0x59265359, %edx       # imm = 0x59265359
orl %ecx, %edx
je 0x136e
xorl $0x1772, %eax           # imm = 0x1772
movl %r15d, %edx
xorl $0x45385090, %edx       # imm = 0x45385090
leaq 0x1(%r14), %rcx
orl %eax, %edx
jne 0x131f
leaq -0x31(%r14), %r8
cmpq $0x32, %r14
movl $0x0, %eax
cmovbq %rax, %r8
leaq 0x65f3a(%rip), %rax     # 0x672c0
movq %r8, (%rax,%r12,8)
testl %ebp, %ebp
je 0x13b3
leaq 0x44ab(%rip), %rax      # 0x5840
movq (%rax,%r12,8), %rcx
movq %r8, %rax
subq %rcx, %rax
cmpq $0x82, %rax
jae 0x13c6
cmpl $0xc350, %ebp           # imm = 0xC350
jae 0x1805
incl %ebp
leaq 0x4484(%rip), %rax      # 0x5840
movq %r14, 0x8(%rax,%r12,8)
jmp 0x1319
movq 0x2c2b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x8(%rsp), %rax
leal 0x1(%rax), %edx
movl %edx, 0x4(%rsp)
leaq 0xd64(%rip), %rsi       # 0x2147
xorl %eax, %eax
callq 0x10a0
leaq 0x444f(%rip), %rax      # 0x5840
movq (%rax,%r12,8), %rax
movslq 0x8(%rsp), %rcx
leaq 0xc793f(%rip), %rdx     # 0xc8d40
movq %rax, (%rdx,%rcx,8)
leaq 0x65eb4(%rip), %rax     # 0x672c0
movq (%rax,%r12,8), %rax
leaq 0x1293a9(%rip), %rdx    # 0x12a7c0
movq %rax, (%rdx,%rcx,8)
movl 0x4(%rsp), %eax
movq %rax, 0x8(%rsp)
jmp 0x13a7
leaq 0x4413(%rip), %rax      # 0x5840
movq (%rax,%r12,8), %rcx
movq %r14, %rax
subq %rcx, %rax
jb 0x146e
cmpq $0x28, %rax
jb 0x146e
decq %r14
leaq 0x65e77(%rip), %rax     # 0x672c0
movq %r14, (%rax,%r12,8)
testl %ebp, %ebp
je 0x146e
movq 0x2ba0(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcb6(%rip), %rsi       # 0x2118
movl %ebp, %edx
movq %r14, %r8
xorl %eax, %eax
callq 0x10a0
movq %rbx, %rdi
callq 0x19ab
movq 0x2b7b(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
cmpl $0x0, 0x8(%rsp)
jle 0x18cc
leaq 0xd09(%rip), %rsi       # 0x219b
leaq 0x2c37(%rip), %rdx      # 0x40d0
xorl %eax, %eax
callq 0x10a0
leaq 0x3bc9(%rip), %rdi      # 0x5070
leaq 0xc2a(%rip), %rsi       # 0x20d8
callq 0x10d0
testq %rax, %rax
je 0x18e3
movq %rax, %rdi
callq 0x1937
movq %rax, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x4(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
movq %r12, 0x18(%rsp)
movq %rax, 0x10(%rsp)
movslq %eax, %rbx
movq %r14, %r12
movq 0x20(%rsp), %rdi
callq 0x195b
cmpl $0x2, %eax
je 0x17d5
movl %eax, %r14d
shldl $0x1, %r15d, %r13d
leal (%r15,%r15), %eax
leaq 0xc7831(%rip), %rcx     # 0xc8d40
movq (%rcx,%rbx,8), %rcx
shrdl $0x10, %r13d, %eax
leaq 0x2f(%rcx), %rdx
cmpq %rdx, %r12
cmovel %eax, %ebp
cmpq %rcx, %r12
setb %al
orb 0x4(%rsp), %al
testb $0x1, %al
jne 0x154a
leaq 0x129289(%rip), %rax    # 0x12a7c0
cmpq (%rax,%rbx,8), %r12
ja 0x154a
movq 0x18(%rsp), %rdi
movl %r14d, %esi
callq 0x1a12
leal (%r14,%r15,2), %r15d
leaq 0x1(%r12), %r14
leaq 0x129266(%rip), %rax    # 0x12a7c0
cmpq (%rax,%rbx,8), %r12
je 0x1579
movq %r14, %r12
leaq 0xc77d6(%rip), %rax     # 0xc8d40
cmpq (%rax,%rbx,8), %r14
jne 0x14e9
jmp 0x1608
testb $0x1, 0x4(%rsp)
jne 0x15ed
movq 0x18(%rsp), %r12
movq %r12, %rdi
pushq $0x17
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x72
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x45
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x38
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x50
popq %rsi
callq 0x1a6c
movq %r12, %rdi
movl $0x90, %esi
callq 0x1a6c
pushq $0x1f
popq %rbx
testl %ebx, %ebx
js 0x15e5
xorl %esi, %esi
btl %ebx, %ebp
setb %sil
movq %r12, %rdi
callq 0x1a12
decl %ebx
jmp 0x15cc
movq %r12, %rdi
callq 0x19ab
movq 0x10(%rsp), %rax
cmpl 0x8(%rsp), %eax
jge 0x17d5
movq 0x10(%rsp), %rax
incl %eax
jmp 0x14de
xorl %eax, %eax
leaq 0x3a5f(%rip), %r12      # 0x5070
leaq 0x3288(%rip), %rbx      # 0x48a0
cmpq $0x7d0, %rax            # imm = 0x7D0
je 0x1629
movb $0x0, (%rax,%rbx)
incq %rax
jmp 0x1618
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1050
movq %rbx, %rdi
pushq $0x2f
popq %rsi
callq 0x1080
movq %rbx, %rcx
leaq 0x1(%rax), %rbx
testq %rax, %rax
cmoveq %rcx, %rbx
movq 0x10(%rsp), %rax
leal 0x1(%rax), %edx
movq %rbx, %rdi
leaq 0xb6c(%rip), %rsi       # 0x21cb
movl %edx, 0x4(%rsp)
xorl %eax, %eax
callq 0x1100
movq %rbx, %rax
movzbl (%rax), %ecx
cmpl $0x20, %ecx
je 0x167b
testl %ecx, %ecx
jne 0x167e
jmp 0x1683
movb $0x30, (%rax)
incq %rax
jmp 0x166d
leaq 0x3216(%rip), %rax      # 0x48a0
subl %eax, %ebx
movslq %ebx, %rsi
movq %rax, %rbx
addq %r12, %rsi
movq %rax, %rdi
callq 0x10f0
movq %rbx, %rdi
callq 0x1070
cmpl $0x5, %eax
jl 0x16e6
movl $0xfffffffc, %ecx       # imm = 0xFFFFFFFC
addl %eax, %ecx
cmpb $0x2e, (%rcx,%rbx)
jne 0x16e6
movl $0xfffffffc, %ecx       # imm = 0xFFFFFFFC
addl %eax, %ecx
incl %ecx
cmpb $0x62, (%rcx,%rbx)
jne 0x16e6
movl $0xfffffffc, %ecx       # imm = 0xFFFFFFFC
addl %eax, %ecx
addl $0x2, %ecx
cmpb $0x7a, (%rcx,%rbx)
jne 0x16e6
movl $0xfffffffc, %ecx       # imm = 0xFFFFFFFC
addl %ecx, %eax
addl $0x3, %eax
cmpb $0x32, (%rax,%rbx)
je 0x16fa
movq %rbx, %rdi
callq 0x1070
movl $0x327a622e, (%rax,%rbx) # imm = 0x327A622E
movb $0x0, 0x4(%rax,%rbx)
movq 0x28f7(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xacc(%rip), %rsi       # 0x21d7
movl 0x4(%rsp), %edx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x10a0
movq %rbx, %rdi
leaq 0xad5(%rip), %rsi       # 0x21f8
callq 0x10d0
testq %rax, %rax
je 0x1904
movq %rax, %rbx
pushq $0x18
popq %rdi
callq 0x10b0
testq %rax, %rax
je 0x1932
movq %rax, %r12
movq %rbx, (%rax)
andq $0x0, 0x8(%rax)
movb $0x77, 0x10(%rax)
movq %rax, %rdi
pushq $0x42
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x5a
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x68
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x39
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x31
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x41
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x59
popq %rbx
movl %ebx, %esi
callq 0x1a6c
movq %r12, %rdi
pushq $0x26
popq %rsi
callq 0x1a6c
movq %r12, %rdi
pushq $0x53
popq %rsi
callq 0x1a6c
movq %r12, %rdi
movl %ebx, %esi
callq 0x1a6c
movl $0x0, 0x4(%rsp)
movq 0x10(%rsp), %rax
jmp 0x14d9
movq 0x281c(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xa2b(%rip), %rsi       # 0x2211
leaq 0x28e3(%rip), %rdx      # 0x40d0
xorl %eax, %eax
callq 0x10a0
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x27ec(%rip), %r14      # 0x3ff8
movq (%r14), %rdi
leaq 0xa9b(%rip), %rsi       # 0x22b1
leaq 0x28b3(%rip), %rbx      # 0x40d0
leaq 0x384c(%rip), %rcx      # 0x5070
movq %rbx, %rdx
movl $0xc350, %r8d           # imm = 0xC350
xorl %eax, %eax
callq 0x10a0
movq (%r14), %rdi
leaq 0xaa4(%rip), %rsi       # 0x22e2
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10a0
movq (%r14), %rdi
leaq 0xabe(%rip), %rsi       # 0x2310
movq %rbx, %rdx
jmp 0x18da
movq 0x2797(%rip), %rbx      # 0x3ff8
movq (%rbx), %rdi
leaq 0x7d7(%rip), %rsi       # 0x2042
leaq 0x285e(%rip), %rcx      # 0x40d0
movq %rcx, %rdx
xorl %eax, %eax
callq 0x10a0
movq (%rbx), %rcx
leaq 0x7e2(%rip), %rdi       # 0x2068
pushq $0x2e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1120
jmp 0x192a
movq 0x275b(%rip), %rcx      # 0x3ff8
movq (%rcx), %rdi
leaq 0x7f0(%rip), %rsi       # 0x2097
leaq 0x2822(%rip), %rdx      # 0x40d0
movl %eax, %ecx
xorl %eax, %eax
callq 0x10a0
jmp 0x192a
movq 0x2738(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x811(%rip), %rsi       # 0x20db
jmp 0x18f4
leaq 0x896(%rip), %rsi       # 0x2169
leaq 0x27f6(%rip), %rdx      # 0x40d0
xorl %eax, %eax
callq 0x10a0
jmp 0x192a
movq 0x270e(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x8c2(%rip), %rsi       # 0x21b6
leaq 0x27d5(%rip), %rdx      # 0x40d0
leaq 0x376e(%rip), %rcx      # 0x5070
jmp 0x1923
movq 0x26ed(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x8e6(%rip), %rsi       # 0x21fb
leaq 0x27b4(%rip), %rdx      # 0x40d0
leaq 0x2f7d(%rip), %rcx      # 0x48a0
xorl %eax, %eax
callq 0x10a0
pushq $0x1
popq %rdi
callq 0x1110
callq 0x1a96