#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 bits(int *, int);
int *diff_longs(int *, int, int, int *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int /*<<< orphan*/ pack_chunk(int *, int, int, int /*<<< orphan*/ *);
void pack_longimage_copen(int *img, int x, int y, FILE *packfile) {
  int chunksiz, packsiz, nbits, next_nbits, tot_nbits;
  int buffer[16384L];
  int *diffs = buffer;
  int *end = diffs - 1;
  int done = 0;
  fprintf(packfile, "\nCCP4 packed image, X: %04d, Y: %04d\n", x, y);
  while (done < (x * y)) {
    end = diff_longs(img, x, y, buffer, done);
    done += (end - buffer) + 1;
    diffs = buffer;
    while (diffs <= end) {
      packsiz = 0;
      chunksiz = 1;
      nbits = bits(diffs, 1);
      while (packsiz == 0) {
        if (end <= (diffs + chunksiz * 2))
          packsiz = chunksiz;
        else {
          next_nbits = bits(diffs + chunksiz, chunksiz);
          tot_nbits = 2 * (((nbits) > (next_nbits)) ? (nbits) : (next_nbits));
          if (tot_nbits >= (nbits + next_nbits + 6))
            packsiz = chunksiz;
          else {
            nbits = tot_nbits;
            if (chunksiz == 64)
              packsiz = chunksiz * 2;
            else
              chunksiz *= 2;
          }
        }
      }
      pack_chunk(diffs, packsiz, nbits / packsiz, packfile);
      diffs += packsiz;
    }
  }
  pack_chunk(NULL, 0, 0, packfile);
}


x86 assembly (clang, -O0):
<pack_longimage_copen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
lea    -0x10030(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x10038(%rbp),%rax
add    $0xfffffffffffffffc,%rax
mov    %rax,-0x10040(%rbp)
movl   $0x0,-0x10044(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10044(%rbp),%eax
mov    -0xc(%rbp),%ecx
imul   -0x10(%rbp),%ecx
cmp    %ecx,%eax
jge    135d <pack_longimage_copen+0x21d>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    -0x10030(%rbp),%rcx
mov    -0x10044(%rbp),%r8d
call   1060 <diff_longs@plt>
mov    %rax,-0x10040(%rbp)
mov    -0x10040(%rbp),%rcx
lea    -0x10030(%rbp),%rax
sub    %rax,%rcx
sar    $0x2,%rcx
add    $0x1,%rcx
movslq -0x10044(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10044(%rbp)
lea    -0x10030(%rbp),%rax
mov    %rax,-0x10038(%rbp)
mov    -0x10038(%rbp),%rax
cmp    -0x10040(%rbp),%rax
ja     1358 <pack_longimage_copen+0x218>
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x10038(%rbp),%rdi
mov    $0x1,%esi
call   1050 <bits@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    131c <pack_longimage_copen+0x1dc>
mov    -0x10040(%rbp),%rax
mov    -0x10038(%rbp),%rcx
mov    -0x1c(%rbp),%edx
shl    $0x1,%edx
movslq %edx,%rdx
shl    $0x2,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
ja     127b <pack_longimage_copen+0x13b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1317 <pack_longimage_copen+0x1d7>
mov    -0x10038(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%esi
call   1050 <bits@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    12b2 <pack_longimage_copen+0x172>
mov    -0x24(%rbp),%eax
mov    %eax,-0x10048(%rbp)
jmp    12bb <pack_longimage_copen+0x17b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x10048(%rbp)
mov    -0x10048(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    -0x28(%rbp),%ecx
add    $0x6,%ecx
cmp    %ecx,%eax
jl     12e6 <pack_longimage_copen+0x1a6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1312 <pack_longimage_copen+0x1d2>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x40,-0x1c(%rbp)
jne    1304 <pack_longimage_copen+0x1c4>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    130d <pack_longimage_copen+0x1cd>
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <pack_longimage_copen+0x1d2>
jmp    1317 <pack_longimage_copen+0x1d7>
jmp    123f <pack_longimage_copen+0xff>
mov    -0x10038(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
call   1030 <pack_chunk@plt>
mov    -0x20(%rbp),%ecx
mov    -0x10038(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10038(%rbp)
jmp    1209 <pack_longimage_copen+0xc9>
jmp    1199 <pack_longimage_copen+0x59>
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <pack_chunk@plt>
add    $0x10050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack_longimage_copen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10038,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,0x18(%rsp)
lea    0xe9f(%rip),%rsi
mov    %rcx,0x10(%rsp)
mov    %rcx,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1040 <fprintf@plt>
mov    %ebx,0xc(%rsp)
mov    %ebp,0x8(%rsp)
imul   %ebp,%ebx
mov    %ebx,0x4(%rsp)
test   %ebx,%ebx
jle    129e <pack_longimage_copen+0x15e>
xor    %r8d,%r8d
jmp    11ac <pack_longimage_copen+0x6c>
xchg   %ax,%ax
mov    0x28(%rsp),%r8
mov    0x20(%rsp),%rax
add    %eax,%r8d
add    $0x1,%r8d
cmp    0x4(%rsp),%r8d
jge    129e <pack_longimage_copen+0x15e>
mov    0x18(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0x30(%rsp),%rbx
mov    %rbx,%rcx
mov    %r8,0x28(%rsp)
call   1060 <diff_longs@plt>
mov    %rax,%r15
sub    %rbx,%rax
shr    $0x2,%rax
mov    %rax,0x20(%rsp)
cmp    %rbx,%r15
jb     1190 <pack_longimage_copen+0x50>
lea    0x30(%rsp),%r13
jmp    1220 <pack_longimage_copen+0xe0>
cs nopw 0x0(%rax,%rax,1)
mov    $0x80,%ebp
mov    %eax,%r12d
mov    %r12d,%eax
cltd
idiv   %ebp
mov    %r13,%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    0x10(%rsp),%rcx
call   1030 <pack_chunk@plt>
movslq %ebp,%rax
lea    0x0(%r13,%rax,4),%r13
cmp    %r15,%r13
ja     1190 <pack_longimage_copen+0x50>
mov    $0x1,%r14d
mov    %r13,%rdi
mov    $0x1,%esi
call   1050 <bits@plt>
mov    %eax,%r12d
jmp    124d <pack_longimage_copen+0x10d>
nopl   0x0(%rax,%rax,1)
mov    %r14d,%ebx
mov    %r14d,%ebp
mov    %ebx,%r14d
test   %ebp,%ebp
jne    11f8 <pack_longimage_copen+0xb8>
lea    (%r14,%r14,1),%ebx
movslq %ebx,%rax
lea    0x0(,%rax,4),%rax
add    %r13,%rax
cmp    %rax,%r15
jbe    1240 <pack_longimage_copen+0x100>
movslq %r14d,%rax
lea    0x0(,%rax,4),%rdi
add    %r13,%rdi
mov    %r14d,%esi
call   1050 <bits@plt>
cmp    %eax,%r12d
lea    0x6(%r12,%rax,1),%ecx
cmovg  %r12d,%eax
add    %eax,%eax
cmp    %ecx,%eax
jge    1240 <pack_longimage_copen+0x100>
cmp    $0x40,%r14d
je     11f0 <pack_longimage_copen+0xb0>
mov    %eax,%r12d
xor    %r14d,%r14d
jmp    1243 <pack_longimage_copen+0x103>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
mov    0x10(%rsp),%rcx
call   1030 <pack_chunk@plt>
add    $0x10038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"\0ACCP4 packed image, X: %04d, Y: %04d\0A\00"
define void @pack_longimage_copen(i32* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [16384 x i32]
%15 = alloca i32*
%16 = alloca i32*
%17 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%18 = getelementptr inbounds [16384 x i32], [16384 x i32]* %14, i64 0, i64 0
store i32* %18, i32** %15
%19 = load i32*, i32** %15
%20 = getelementptr inbounds i32, i32* %19, i64 -1
store i32* %20, i32** %16
store i32 0, i32* %17
%21 = load i32*, i32** %8
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %23)
br label %25
25: ; preds = %120, %4
%26 = load i32, i32* %17
%27 = load i32, i32* %6
%28 = load i32, i32* %7
%29 = mul nsw i32 %27, %28
%30 = icmp slt i32 %26, %29
br i1 %30, label %31, label %121
31: ; preds = %25
%32 = load i32*, i32** %5
%33 = load i32, i32* %6
%34 = load i32, i32* %7
%35 = getelementptr inbounds [16384 x i32], [16384 x i32]* %14, i64 0, i64 0
%36 = load i32, i32* %17
%37 = call i32* @diff_longs(i32* %32, i32 %33, i32 %34, i32* %35, i32 %36)
store i32* %37, i32** %16
%38 = load i32*, i32** %16
%39 = getelementptr inbounds [16384 x i32], [16384 x i32]* %14, i64 0, i64 0
%40 = ptrtoint i32* %38 to i64
%41 = ptrtoint i32* %39 to i64
%42 = sub i64 %40, %41
%43 = sdiv exact i64 %42, 4
%44 = add nsw i64 %43, 1
%45 = load i32, i32* %17
%46 = sext i32 %45 to i64
%47 = add nsw i64 %46, %44
%48 = trunc i64 %47 to i32
store i32 %48, i32* %17
%49 = getelementptr inbounds [16384 x i32], [16384 x i32]* %14, i64 0, i64 0
store i32* %49, i32** %15
br label %50
50: ; preds = %108, %31
%51 = load i32*, i32** %15
%52 = load i32*, i32** %16
%53 = icmp ule i32* %51, %52
br i1 %53, label %54, label %120
54: ; preds = %50
store i32 0, i32* %10
store i32 1, i32* %9
%55 = load i32*, i32** %15
%56 = call i32 @bits(i32* %55, i32 1)
store i32 %56, i32* %11
br label %57
57: ; preds = %107, %54
%58 = load i32, i32* %10
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %108
60: ; preds = %57
%61 = load i32*, i32** %16
%62 = load i32*, i32** %15
%63 = load i32, i32* %9
%64 = mul nsw i32 %63, 2
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i32, i32* %62, i64 %65
%67 = icmp ule i32* %61, %66
br i1 %67, label %68, label %70
68: ; preds = %60
%69 = load i32, i32* %9
store i32 %69, i32* %10
br label %107
70: ; preds = %60
%71 = load i32*, i32** %15
%72 = load i32, i32* %9
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
%75 = load i32, i32* %9
%76 = call i32 @bits(i32* %74, i32 %75)
store i32 %76, i32* %12
%77 = load i32, i32* %11
%78 = load i32, i32* %12
%79 = icmp sgt i32 %77, %78
br i1 %79, label %80, label %82
80: ; preds = %70
%81 = load i32, i32* %11
br label %84
82: ; preds = %70
%83 = load i32, i32* %12
br label %84
84: ; preds = %82, %80
%85 = phi i32 [ %81, %80 ], [ %83, %82 ]
%86 = mul nsw i32 2, %85
store i32 %86, i32* %13
%87 = load i32, i32* %13
%88 = load i32, i32* %11
%89 = load i32, i32* %12
%90 = add nsw i32 %88, %89
%91 = add nsw i32 %90, 6
%92 = icmp sge i32 %87, %91
br i1 %92, label %93, label %95
93: ; preds = %84
%94 = load i32, i32* %9
store i32 %94, i32* %10
br label %106
95: ; preds = %84
%96 = load i32, i32* %13
store i32 %96, i32* %11
%97 = load i32, i32* %9
%98 = icmp eq i32 %97, 64
br i1 %98, label %99, label %102
99: ; preds = %95
%100 = load i32, i32* %9
%101 = mul nsw i32 %100, 2
store i32 %101, i32* %10
br label %105
102: ; preds = %95
%103 = load i32, i32* %9
%104 = mul nsw i32 %103, 2
store i32 %104, i32* %9
br label %105
105: ; preds = %102, %99
br label %106
106: ; preds = %105, %93
br label %107
107: ; preds = %106, %68
br label %57
108: ; preds = %57
%109 = load i32*, i32** %15
%110 = load i32, i32* %10
%111 = load i32, i32* %11
%112 = load i32, i32* %10
%113 = sdiv i32 %111, %112
%114 = load i32*, i32** %8
%115 = call i32 @pack_chunk(i32* %109, i32 %110, i32 %113, i32* %114)
%116 = load i32, i32* %10
%117 = load i32*, i32** %15
%118 = sext i32 %116 to i64
%119 = getelementptr inbounds i32, i32* %117, i64 %118
store i32* %119, i32** %15
br label %50
120: ; preds = %50
br label %25
121: ; preds = %25
%122 = load i32*, i32** %8
%123 = call i32 @pack_chunk(i32* null, i32 0, i32 0, i32* %122)
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"\0ACCP4 packed image, X: %04d, Y: %04d\0A\00"
define void @pack_longimage_copen(i32* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca [16384 x i32]
%6 = bitcast [16384 x i32]* %5 to i8*
%7 = getelementptr inbounds [16384 x i32], [16384 x i32]* %5, i64 0, i64 0
%8 = tail call i32 @fprintf(i32* %3, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%9 = mul nsw i32 %2, %1
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %60
11: ; preds = %4
%12 = ptrtoint [16384 x i32]* %5 to i64
br label %15
13: ; preds = %52, %15
%14 = icmp slt i32 %23, %9
br i1 %14, label %15, label %60
15: ; preds = %11, %13
%16 = phi i32 [ 0, %11 ], [ %23, %13 ]
%17 = call i32* @diff_longs(i32* %0, i32 %1, i32 %2, i32* %7, i32 %16)
%18 = ptrtoint i32* %17 to i64
%19 = sub i64 %18, %12
%20 = lshr exact i64 %19, 2
%21 = trunc i64 %20 to i32
%22 = add nsw i32 %16, 1
%23 = add i32 %22, %21
%24 = icmp ugt i32* %7, %17
br i1 %24, label %13, label %25
25: ; preds = %15, %52
%26 = phi i32* [ %58, %52 ], [ %7, %15 ]
%27 = call i32 @bits(i32* %26, i32 1)
br label %28
28: ; preds = %25, %47
%29 = phi i32 [ 1, %25 ], [ %50, %47 ]
%30 = phi i32 [ %27, %25 ], [ %48, %47 ]
%31 = shl nsw i32 %29, 1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %26, i64 %32
%34 = icmp ugt i32* %17, %33
br i1 %34, label %35, label %47
35: ; preds = %28
%36 = sext i32 %29 to i64
%37 = getelementptr inbounds i32, i32* %26, i64 %36
%38 = call i32 @bits(i32* %37, i32 %29)
%39 = icmp sgt i32 %30, %38
%40 = select i1 %39, i32 %30, i32 %38
%41 = shl nsw i32 %40, 1
%42 = add i32 %30, 6
%43 = add i32 %42, %38
%44 = icmp slt i32 %41, %43
br i1 %44, label %45, label %47
45: ; preds = %35
%46 = icmp eq i32 %29, 64
br i1 %46, label %52, label %47
47: ; preds = %45, %35, %28
%48 = phi i32 [ %30, %28 ], [ %30, %35 ], [ %41, %45 ]
%49 = phi i32 [ %29, %28 ], [ %29, %35 ], [ 0, %45 ]
%50 = phi i32 [ %29, %28 ], [ %29, %35 ], [ %31, %45 ]
%51 = icmp eq i32 %49, 0
br i1 %51, label %28, label %52
52: ; preds = %45, %47
%53 = phi i32 [ %49, %47 ], [ 128, %45 ]
%54 = phi i32 [ %48, %47 ], [ %41, %45 ]
%55 = sdiv i32 %54, %53
%56 = call i32 @pack_chunk(i32* %26, i32 %53, i32 %55, i32* %3)
%57 = sext i32 %53 to i64
%58 = getelementptr inbounds i32, i32* %26, i64 %57
%59 = icmp ugt i32* %58, %17
br i1 %59, label %13, label %25
60: ; preds = %13, %4
%61 = call i32 @pack_chunk(i32* null, i32 0, i32 0, i32* %3)
ret void
}