#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
unsigned int JCALG1_Decompress_Fast(char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, unsigned short);
unsigned JCALG1_Decompress_Formatted_Buffer(unsigned nInputLength, void *pInput,
                                            unsigned nMaxOutput,
                                            void *pOutput) {
  unsigned outputBufferLength = 0;
  for (char *pInputPointer = (char *)pInput;
       pInputPointer < (char *)pInput + nInputLength;) {
    unsigned short bytesThisBlock = *(unsigned short *)pInputPointer;
    if (!bytesThisBlock)
      break;
    pInputPointer += sizeof(unsigned short);
    if (bytesThisBlock & 0x8000) {
      bytesThisBlock &= ~0x8000;
      memcpy((char *)pOutput + outputBufferLength, pInputPointer,
             bytesThisBlock);
      outputBufferLength += bytesThisBlock;
      pInputPointer += bytesThisBlock;
    } else {
      unsigned bytesDecompressed = JCALG1_Decompress_Fast(
          pInputPointer, (char *)pOutput + outputBufferLength);
      outputBufferLength += bytesDecompressed;
      pInputPointer += bytesThisBlock;
    }
    if (outputBufferLength > nMaxOutput) {
      return 0xFFFFFFFF;
    }
  }
  return outputBufferLength;
}


x86 assembly (clang, -O0):
<JCALG1_Decompress_Formatted_Buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%edx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    122b <JCALG1_Decompress_Formatted_Buffer+0x10b>
mov    -0x30(%rbp),%rax
mov    (%rax),%ax
mov    %ax,-0x32(%rbp)
cmpw   $0x0,-0x32(%rbp)
jne    1177 <JCALG1_Decompress_Formatted_Buffer+0x57>
jmp    122b <JCALG1_Decompress_Formatted_Buffer+0x10b>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
movzwl -0x32(%rbp),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     11dd <JCALG1_Decompress_Formatted_Buffer+0xbd>
movzwl -0x32(%rbp),%eax
and    $0xffff7fff,%eax
mov    %ax,-0x32(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x32(%rbp),%ax
movzwl %ax,%edx
call   1040 <memcpy@plt>
movzwl -0x32(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
movzwl -0x32(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    120e <JCALG1_Decompress_Formatted_Buffer+0xee>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%eax
add    %rax,%rsi
call   1030 <JCALG1_Decompress_Fast@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
movzwl -0x32(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jbe    1226 <JCALG1_Decompress_Formatted_Buffer+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    1231 <JCALG1_Decompress_Formatted_Buffer+0x111>
jmp    1145 <JCALG1_Decompress_Formatted_Buffer+0x25>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<JCALG1_Decompress_Formatted_Buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %edi,%r13d
add    %rsi,%r13
xor    %r12d,%r12d
jmp    115c <JCALG1_Decompress_Formatted_Buffer+0x3c>
nop
mov    %r12d,%esi
add    %r14,%rsi
mov    %rbp,%rdi
call   1030 <JCALG1_Decompress_Fast@plt>
movzwl %bx,%ecx
add    %rcx,%rbp
add    %eax,%r12d
cmp    %r15d,%r12d
ja     118c <JCALG1_Decompress_Formatted_Buffer+0x6c>
cmp    %r13,%rbp
jae    1192 <JCALG1_Decompress_Formatted_Buffer+0x72>
movzwl 0x0(%rbp),%ebx
test   %ebx,%ebx
je     1192 <JCALG1_Decompress_Formatted_Buffer+0x72>
add    $0x2,%rbp
test   %bx,%bx
jns    1140 <JCALG1_Decompress_Formatted_Buffer+0x20>
and    $0x7fff,%ebx
mov    %r12d,%edi
add    %r14,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %ebx,%eax
jmp    114e <JCALG1_Decompress_Formatted_Buffer+0x2e>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @JCALG1_Decompress_Formatted_Buffer(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i16
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %10
%14 = load i8*, i8** %7
store i8* %14, i8** %11
br label %15
15: ; preds = %77, %4
%16 = load i8*, i8** %11
%17 = load i8*, i8** %7
%18 = load i32, i32* %6
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = icmp ult i8* %16, %20
br i1 %21, label %22, label %78
22: ; preds = %15
%23 = load i8*, i8** %11
%24 = bitcast i8* %23 to i16*
%25 = load i16, i16* %24
store i16 %25, i16* %12
%26 = load i16, i16* %12
%27 = icmp ne i16 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %22
br label %78
29: ; preds = %22
%30 = load i8*, i8** %11
%31 = getelementptr inbounds i8, i8* %30, i64 2
store i8* %31, i8** %11
%32 = load i16, i16* %12
%33 = zext i16 %32 to i32
%34 = and i32 %33, 32768
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %57
36: ; preds = %29
%37 = load i16, i16* %12
%38 = zext i16 %37 to i32
%39 = and i32 %38, -32769
%40 = trunc i32 %39 to i16
store i16 %40, i16* %12
%41 = load i8*, i8** %9
%42 = load i32, i32* %10
%43 = zext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8*, i8** %11
%46 = load i16, i16* %12
%47 = call i32 @memcpy(i8* %44, i8* %45, i16 zeroext %46)
%48 = load i16, i16* %12
%49 = zext i16 %48 to i32
%50 = load i32, i32* %10
%51 = add i32 %50, %49
store i32 %51, i32* %10
%52 = load i16, i16* %12
%53 = zext i16 %52 to i32
%54 = load i8*, i8** %11
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8* %56, i8** %11
br label %72
57: ; preds = %29
%58 = load i8*, i8** %11
%59 = load i8*, i8** %9
%60 = load i32, i32* %10
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = call i32 @JCALG1_Decompress_Fast(i8* %58, i8* %62)
store i32 %63, i32* %13
%64 = load i32, i32* %13
%65 = load i32, i32* %10
%66 = add i32 %65, %64
store i32 %66, i32* %10
%67 = load i16, i16* %12
%68 = zext i16 %67 to i32
%69 = load i8*, i8** %11
%70 = sext i32 %68 to i64
%71 = getelementptr inbounds i8, i8* %69, i64 %70
store i8* %71, i8** %11
br label %72
72: ; preds = %57, %36
%73 = load i32, i32* %10
%74 = load i32, i32* %8
%75 = icmp ugt i32 %73, %74
br i1 %75, label %76, label %77
76: ; preds = %72
store i32 -1, i32* %5
br label %80
77: ; preds = %72
br label %15
78: ; preds = %28, %15
%79 = load i32, i32* %10
store i32 %79, i32* %5
br label %80
80: ; preds = %78, %76
%81 = load i32, i32* %5
ret i32 %81
}

LLVM IR (-O2):
define i32 @JCALG1_Decompress_Formatted_Buffer(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = zext i32 %0 to i64
%6 = getelementptr inbounds i8, i8* %1, i64 %5
br label %7
7: ; preds = %28, %4
%8 = phi i32 [ 0, %4 ], [ %33, %28 ]
%9 = phi i8* [ %1, %4 ], [ %32, %28 ]
%10 = icmp ult i8* %9, %6
br i1 %10, label %11, label %35
11: ; preds = %7
%12 = bitcast i8* %9 to i16*
%13 = load i16, i16* %12
%14 = icmp eq i16 %13, 0
br i1 %14, label %35, label %15
15: ; preds = %11
%16 = getelementptr inbounds i8, i8* %9, i64 2
%17 = icmp sgt i16 %13, -1
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = and i16 %13, 32767
%20 = zext i32 %8 to i64
%21 = getelementptr inbounds i8, i8* %3, i64 %20
%22 = tail call i32 @memcpy(i8* %21, i8* %16, i16 zeroext %19)
%23 = zext i16 %19 to i32
br label %28
24: ; preds = %15
%25 = zext i32 %8 to i64
%26 = getelementptr inbounds i8, i8* %3, i64 %25
%27 = tail call i32 @JCALG1_Decompress_Fast(i8* %16, i8* %26)
br label %28
28: ; preds = %18, %24
%29 = phi i32 [ %23, %18 ], [ %27, %24 ]
%30 = phi i16 [ %19, %18 ], [ %13, %24 ]
%31 = zext i16 %30 to i64
%32 = getelementptr inbounds i8, i8* %16, i64 %31
%33 = add i32 %29, %8
%34 = icmp ugt i32 %33, %2
br i1 %34, label %35, label %7
35: ; preds = %11, %7, %28
%36 = phi i32 [ -1, %28 ], [ %8, %7 ], [ %8, %11 ]
ret i32 %36
}