#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fgetc(int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
long unpack(int size, FILE *fin) {
  int i;
  int value = 0;
  static int ptr = 1;
  static int sum;
  if (size == -2) {
    sum = fgetc(fin);
    sum <<= 1;
    return ((long)0);
  }
  if (size == -1)
    return ((long)((sum & 256) >> 8));
  for (i = 0; i < size; ++i, ++ptr, sum <<= 1) {
    if (sum & 256)
      value |= 1 << i;
    if (ptr == 8) {
      sum = getc(fin);
      ptr = 0;
    }
  }
  return ((long)value);
}


x86 assembly (clang, -O0):
<unpack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0xfffffffe,-0xc(%rbp)
jne    116b <unpack+0x4b>
mov    -0x18(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,0x2ee9(%rip)
mov    0x2ee3(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2eda(%rip)
movq   $0x0,-0x8(%rbp)
jmp    1224 <unpack+0x104>
cmpl   $0xffffffff,-0xc(%rbp)
jne    118e <unpack+0x6e>
mov    0x2ebd(%rip),%eax
and    $0x100,%eax
sar    $0x8,%eax
cltq
mov    %rax,-0x8(%rbp)
jmp    1224 <unpack+0x104>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121c <unpack+0xfc>
mov    0x2e91(%rip),%eax
and    $0x100,%eax
cmp    $0x0,%eax
je     11c5 <unpack+0xa5>
mov    -0x1c(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x8,0x2e64(%rip)
jne    11eb <unpack+0xcb>
mov    -0x18(%rbp),%rdi
call   1040 <getc@plt>
mov    %eax,0x2e57(%rip)
movl   $0x0,0x2e45(%rip)
jmp    11f0 <unpack+0xd0>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e31(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e28(%rip)
mov    0x2e2a(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2e21(%rip)
jmp    1195 <unpack+0x75>
movslq -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unpack>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
cmp    $0xfffffffe,%edi
je     1149 <unpack+0x29>
mov    %edi,%r15d
cmp    $0xffffffff,%edi
jne    115e <unpack+0x3e>
mov    0x2ef9(%rip),%r12d
shr    $0x8,%r12d
and    $0x1,%r12d
jmp    11c3 <unpack+0xa3>
mov    %r14,%rdi
call   1030 <fgetc@plt>
add    %eax,%eax
mov    %eax,0x2edf(%rip)
xor    %r12d,%r12d
jmp    11c3 <unpack+0xa3>
xor    %r12d,%r12d
test   %r15d,%r15d
jle    11c3 <unpack+0xa3>
mov    0x2ecc(%rip),%eax
mov    0x2ebe(%rip),%edx
xor    %ebp,%ebp
jmp    119c <unpack+0x7c>
cs nopw 0x0(%rax,%rax,1)
or     %ebx,%r12d
add    $0x1,%ebp
add    $0x1,%edx
mov    %edx,0x2ea1(%rip)
add    %eax,%eax
mov    %eax,0x2ea1(%rip)
cmp    %ebp,%r15d
je     11c0 <unpack+0xa0>
mov    %eax,%esi
mov    $0x1,%ebx
mov    %ebp,%ecx
shl    %cl,%ebx
shl    $0x17,%esi
sar    $0x1f,%esi
and    %esi,%ebx
cmp    $0x8,%edx
jne    1180 <unpack+0x60>
mov    %r14,%rdi
call   1040 <getc@plt>
xor    %edx,%edx
jmp    1180 <unpack+0x60>
movslq %r12d,%r12
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@unpack.ptr = internal global i32 1
@unpack.sum = internal global i32 0
define i64 @unpack(i32 %0, i32* %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
store i32 0, i32* %7
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i32*, i32** %5
%12 = call i32 @fgetc(i32* %11)
store i32 %12, i32* @unpack.sum
%13 = load i32, i32* @unpack.sum
%14 = shl i32 %13, 1
store i32 %14, i32* @unpack.sum
store i64 0, i64* %3
br label %54
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* @unpack.sum
%20 = and i32 %19, 256
%21 = ashr i32 %20, 8
%22 = sext i32 %21 to i64
store i64 %22, i64* %3
br label %54
23: ; preds = %15
store i32 0, i32* %6
br label %24
24: ; preds = %44, %23
%25 = load i32, i32* %6
%26 = load i32, i32* %4
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = load i32, i32* @unpack.sum
%30 = and i32 %29, 256
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %28
%33 = load i32, i32* %6
%34 = shl i32 1, %33
%35 = load i32, i32* %7
%36 = or i32 %35, %34
store i32 %36, i32* %7
br label %37
37: ; preds = %32, %28
%38 = load i32, i32* @unpack.ptr
%39 = icmp eq i32 %38, 8
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32*, i32** %5
%42 = call i32 @getc(i32* %41)
store i32 %42, i32* @unpack.sum
store i32 0, i32* @unpack.ptr
br label %43
43: ; preds = %40, %37
br label %44
44: ; preds = %43
%45 = load i32, i32* %6
%46 = add nsw i32 %45, 1
store i32 %46, i32* %6
%47 = load i32, i32* @unpack.ptr
%48 = add nsw i32 %47, 1
store i32 %48, i32* @unpack.ptr
%49 = load i32, i32* @unpack.sum
%50 = shl i32 %49, 1
store i32 %50, i32* @unpack.sum
br label %24
51: ; preds = %24
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
store i64 %53, i64* %3
br label %54
54: ; preds = %51, %18, %10
%55 = load i64, i64* %3
ret i64 %55
}

LLVM IR (-O2):
@unpack.ptr = internal unnamed_addr global i32 1
@unpack.sum = internal unnamed_addr global i32 0
define i64 @unpack(i32 %0, i32* nocapture %1) {
switch i32 %0, label %3 [
i32 -2, label %8
i32 -1, label %11
]
3: ; preds = %2
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %38
5: ; preds = %3
%6 = load i32, i32* @unpack.sum
%7 = load i32, i32* @unpack.ptr
br label %16
8: ; preds = %2
%9 = tail call i32 @fgetc(i32* %1)
%10 = shl i32 %9, 1
store i32 %10, i32* @unpack.sum
br label %38
11: ; preds = %2
%12 = load i32, i32* @unpack.sum
%13 = lshr i32 %12, 8
%14 = and i32 %13, 1
%15 = zext i32 %14 to i64
br label %38
16: ; preds = %5, %29
%17 = phi i32 [ %33, %29 ], [ %7, %5 ]
%18 = phi i32 [ %34, %29 ], [ %6, %5 ]
%19 = phi i32 [ %25, %29 ], [ 0, %5 ]
%20 = phi i32 [ %32, %29 ], [ 0, %5 ]
%21 = and i32 %18, 256
%22 = icmp eq i32 %21, 0
%23 = shl nuw i32 1, %20
%24 = select i1 %22, i32 0, i32 %23
%25 = or i32 %24, %19
%26 = icmp eq i32 %17, 8
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = tail call i32 @getc(i32* %1)
br label %29
29: ; preds = %16, %27
%30 = phi i32 [ %18, %16 ], [ %28, %27 ]
%31 = phi i32 [ %17, %16 ], [ 0, %27 ]
%32 = add nuw nsw i32 %20, 1
%33 = add nsw i32 %31, 1
store i32 %33, i32* @unpack.ptr
%34 = shl i32 %30, 1
store i32 %34, i32* @unpack.sum
%35 = icmp eq i32 %32, %0
br i1 %35, label %36, label %16
36: ; preds = %29
%37 = sext i32 %25 to i64
br label %38
38: ; preds = %3, %36, %11, %8
%39 = phi i64 [ 0, %8 ], [ %15, %11 ], [ 0, %3 ], [ %37, %36 ]
ret i64 %39
}