#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 /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int pack(int size, long value, FILE *foutf) {
  int i;
  static int ptr = 1, sum = 0, num_of_packed_bytes = 0;
  if (size == -1 && ptr != 1) {
    fputc(sum << (8 - ptr), foutf);
    ++num_of_packed_bytes;
    return (0);
  }
  if (size == -2)
    return (num_of_packed_bytes);
  for (i = 0; i < size; ++i, ++ptr, value = value >> 1, sum = sum << 1) {
    if (value & 1)
      sum |= 1;
    if (ptr == 8) {
      fputc(sum, foutf);
      ++num_of_packed_bytes;
      sum = 0;
      ptr = 0;
    }
  }
  return (-1);
}


x86 assembly (clang, -O0):
<pack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1171 <pack+0x61>
cmpl   $0x1,0x2ef4(%rip)
je     1171 <pack+0x61>
mov    0x2ef0(%rip),%edi
mov    $0x8,%ecx
sub    0x2edd(%rip),%ecx
shl    %cl,%edi
mov    -0x18(%rbp),%rsi
call   1030 <fputc@plt>
mov    0x2ed8(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2ecf(%rip)
movl   $0x0,-0x4(%rbp)
jmp    1240 <pack+0x130>
cmpl   $0xfffffffe,-0x8(%rbp)
jne    1189 <pack+0x79>
mov    0x2eb3(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    1240 <pack+0x130>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1239 <pack+0x129>
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     11bd <pack+0xad>
mov    0x2e7c(%rip),%eax
or     $0x1,%eax
mov    %eax,0x2e73(%rip)
cmpl   $0x8,0x2e64(%rip)
jne    11fc <pack+0xec>
mov    0x2e60(%rip),%edi
mov    -0x18(%rbp),%rsi
call   1030 <fputc@plt>
mov    0x2e55(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e4c(%rip)
movl   $0x0,0x2e3e(%rip)
movl   $0x0,0x2e2c(%rip)
jmp    1201 <pack+0xf1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e18(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e0f(%rip)
mov    -0x10(%rbp),%rax
sar    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    0x2e05(%rip),%eax
shl    $0x1,%eax
mov    %eax,0x2dfc(%rip)
jmp    1190 <pack+0x80>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2f03(%rip),%eax
cmp    $0xffffffff,%edi
jne    114f <pack+0x3f>
cmp    $0x1,%eax
je     114f <pack+0x3f>
mov    0x2efb(%rip),%edi
mov    $0x8,%cl
sub    %al,%cl
shl    %cl,%edi
mov    %r14,%rsi
call   1030 <fputc@plt>
addl   $0x1,0x2eea(%rip)
xor    %r15d,%r15d
jmp    1167 <pack+0x57>
cmp    $0xfffffffe,%ebp
jne    115d <pack+0x4d>
mov    0x2ed9(%rip),%r15d
jmp    1167 <pack+0x57>
mov    $0xffffffff,%r15d
test   %ebp,%ebp
jg     1199 <pack+0x89>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%eax
mov    %eax,0x2e9f(%rip)
sar    %rbx
add    %edi,%edi
mov    %edi,0x2e9c(%rip)
add    $0xffffffff,%ebp
je     1167 <pack+0x57>
test   $0x1,%bl
je     11a5 <pack+0x95>
orb    $0x1,0x2e8b(%rip)
mov    0x2e85(%rip),%edi
cmp    $0x8,%eax
jne    1180 <pack+0x70>
mov    %r14,%rsi
call   1030 <fputc@plt>
addl   $0x1,0x2e75(%rip)
xor    %edi,%edi
xor    %eax,%eax
jmp    1180 <pack+0x70>

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

LLVM IR (-O2):
@pack.ptr = internal unnamed_addr global i32 1
@pack.sum = internal unnamed_addr global i32 0
@pack.num_of_packed_bytes = internal unnamed_addr global i32 0
define i32 @pack(i32 %0, i64 %1, i32* nocapture %2) {
%4 = icmp eq i32 %0, -1
%5 = load i32, i32* @pack.ptr
%6 = icmp ne i32 %5, 1
%7 = select i1 %4, i1 %6, i1 false
br i1 %7, label %8, label %15
8: ; preds = %3
%9 = load i32, i32* @pack.sum
%10 = sub nsw i32 8, %5
%11 = shl i32 %9, %10
%12 = tail call i32 @fputc(i32 %11, i32* %2)
%13 = load i32, i32* @pack.num_of_packed_bytes
%14 = add nsw i32 %13, 1
store i32 %14, i32* @pack.num_of_packed_bytes
br label %45
15: ; preds = %3
%16 = icmp eq i32 %0, -2
br i1 %16, label %19, label %17
17: ; preds = %15
%18 = icmp sgt i32 %0, 0
br i1 %18, label %21, label %45
19: ; preds = %15
%20 = load i32, i32* @pack.num_of_packed_bytes
br label %45
21: ; preds = %17, %37
%22 = phi i32 [ %41, %37 ], [ %5, %17 ]
%23 = phi i32 [ %40, %37 ], [ 0, %17 ]
%24 = phi i64 [ %42, %37 ], [ %1, %17 ]
%25 = and i64 %24, 1
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %21
%28 = load i32, i32* @pack.sum
%29 = or i32 %28, 1
store i32 %29, i32* @pack.sum
br label %30
30: ; preds = %27, %21
%31 = icmp eq i32 %22, 8
%32 = load i32, i32* @pack.sum
br i1 %31, label %33, label %37
33: ; preds = %30
%34 = tail call i32 @fputc(i32 %32, i32* %2)
%35 = load i32, i32* @pack.num_of_packed_bytes
%36 = add nsw i32 %35, 1
store i32 %36, i32* @pack.num_of_packed_bytes
br label %37
37: ; preds = %30, %33
%38 = phi i32 [ 0, %33 ], [ %32, %30 ]
%39 = phi i32 [ 0, %33 ], [ %22, %30 ]
%40 = add nuw nsw i32 %23, 1
%41 = add nsw i32 %39, 1
store i32 %41, i32* @pack.ptr
%42 = ashr i64 %24, 1
%43 = shl i32 %38, 1
store i32 %43, i32* @pack.sum
%44 = icmp eq i32 %40, %0
br i1 %44, label %45, label %21
45: ; preds = %37, %17, %19, %8
%46 = phi i32 [ 0, %8 ], [ %20, %19 ], [ -1, %17 ], [ -1, %37 ]
ret i32 %46
}