#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 */
int /*<<< orphan*/ dact_bit_buffer_purge();
unsigned char dact_bit_buffer_read(int);
int dact_bit_buffer_size();
int /*<<< orphan*/ dact_bit_buffer_write(unsigned char, int);
int /*<<< orphan*/ memset(char *, unsigned char, int);
int dact_text_decompress(unsigned char *curr_block, char *out_block,
                         int blk_size, int bufsize) {
  unsigned char high_byte, low_byte;
  unsigned int range;
  int i = 0, x = 0, y;
  low_byte = curr_block[x++];
  high_byte = curr_block[x++];
  range = (unsigned int)(high_byte - low_byte);
  if (range == 0) {
    memset(out_block, low_byte, bufsize);
    return (bufsize);
  }
  for (y = 1; y <= 8; y++)
    if ((range >> y) == 0)
      break;
  dact_bit_buffer_purge();
  while (1) {
    if (dact_bit_buffer_size() < y)
      dact_bit_buffer_write((unsigned char)curr_block[x++], 8);
    out_block[i++] = (dact_bit_buffer_read(y) + low_byte);
    if ((x == blk_size) && (dact_bit_buffer_size() < y))
      break;
    if (i >= (bufsize))
      break;
  }
  return (i);
}


x86 assembly (clang, -O0):
<dact_text_decompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x30(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x22(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x30(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
movzbl -0x21(%rbp),%eax
movzbl -0x22(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11d8 <dact_text_decompress+0x88>
mov    -0x18(%rbp),%rdi
mov    -0x22(%rbp),%al
mov    -0x20(%rbp),%edx
movzbl %al,%esi
call   1030 <memset@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ae <dact_text_decompress+0x15e>
movl   $0x1,-0x34(%rbp)
cmpl   $0x8,-0x34(%rbp)
jg     1212 <dact_text_decompress+0xc2>
mov    -0x28(%rbp),%eax
mov    -0x34(%rbp),%ecx
shr    %cl,%eax
cmp    $0x0,%eax
jne    11ff <dact_text_decompress+0xaf>
jmp    1212 <dact_text_decompress+0xc2>
jmp    1204 <dact_text_decompress+0xb4>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11df <dact_text_decompress+0x8f>
mov    $0x0,%al
call   1060 <dact_bit_buffer_purge@plt>
mov    $0x0,%al
call   1040 <dact_bit_buffer_size@plt>
cmp    -0x34(%rbp),%eax
jge    1249 <dact_text_decompress+0xf9>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x30(%rbp)
movslq %ecx,%rcx
mov    $0x8,%esi
movzbl (%rax,%rcx,1),%edi
call   1050 <dact_bit_buffer_write@plt>
mov    -0x34(%rbp),%edi
call   1070 <dact_bit_buffer_read@plt>
movzbl %al,%eax
movzbl -0x22(%rbp),%ecx
add    %ecx,%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    1292 <dact_text_decompress+0x142>
mov    $0x0,%al
call   1040 <dact_bit_buffer_size@plt>
cmp    -0x34(%rbp),%eax
jge    1292 <dact_text_decompress+0x142>
jmp    12a8 <dact_text_decompress+0x158>
mov    -0x2c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jl     12a3 <dact_text_decompress+0x153>
jmp    12a8 <dact_text_decompress+0x158>
jmp    1219 <dact_text_decompress+0xc9>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dact_text_decompress>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %edx,0xc(%rsp)
mov    %rsi,%r15
movzbl (%rdi),%r13d
mov    %rdi,0x10(%rsp)
movzbl 0x1(%rdi),%eax
sub    %r13d,%eax
je     1225 <dact_text_decompress+0xd5>
mov    %ebp,0x8(%rsp)
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%ebp
mov    %eax,%edx
mov    %ebp,%ecx
shr    %cl,%edx
cmp    $0x8,%ebp
ja     11a2 <dact_text_decompress+0x52>
test   %edx,%edx
jne    1190 <dact_text_decompress+0x40>
xor    %eax,%eax
call   1060 <dact_bit_buffer_purge@plt>
mov    $0x2,%r14d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <dact_bit_buffer_size@plt>
cmp    %ebp,%eax
jge    11e5 <dact_text_decompress+0x95>
movslq %r14d,%rax
add    $0x1,%r14d
mov    0x10(%rsp),%rcx
movzbl (%rcx,%rax,1),%edi
mov    $0x8,%esi
call   1050 <dact_bit_buffer_write@plt>
mov    %ebp,%edi
call   1070 <dact_bit_buffer_read@plt>
add    %r13b,%al
mov    %r15,%rbx
mov    %al,(%r15,%r12,1)
lea    0x1(%r12),%r15
cmp    0xc(%rsp),%r14d
jne    120d <dact_text_decompress+0xbd>
xor    %eax,%eax
call   1040 <dact_bit_buffer_size@plt>
cmp    %ebp,%eax
jl     121c <dact_text_decompress+0xcc>
cmp    0x8(%rsp),%r15d
jge    121c <dact_text_decompress+0xcc>
mov    %r15,%r12
mov    %rbx,%r15
jmp    11c0 <dact_text_decompress+0x70>
add    $0x1,%r12d
mov    %r12d,%ebp
jmp    1233 <dact_text_decompress+0xe3>
movzbl %r13b,%esi
mov    %r15,%rdi
mov    %ebp,%edx
call   1030 <memset@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dact_text_decompress(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %13
store i32 0, i32* %14
%16 = load i8*, i8** %6
%17 = load i32, i32* %14
%18 = add nsw i32 %17, 1
store i32 %18, i32* %14
%19 = sext i32 %17 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
%21 = load i8, i8* %20
store i8 %21, i8* %11
%22 = load i8*, i8** %6
%23 = load i32, i32* %14
%24 = add nsw i32 %23, 1
store i32 %24, i32* %14
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = load i8, i8* %26
store i8 %27, i8* %10
%28 = load i8, i8* %10
%29 = zext i8 %28 to i32
%30 = load i8, i8* %11
%31 = zext i8 %30 to i32
%32 = sub nsw i32 %29, %31
store i32 %32, i32* %12
%33 = load i32, i32* %12
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %4
%36 = load i8*, i8** %7
%37 = load i8, i8* %11
%38 = load i32, i32* %9
%39 = call i32 @memset(i8* %36, i8 zeroext %37, i32 %38)
%40 = load i32, i32* %9
store i32 %40, i32* %5
br label %98
41: ; preds = %4
store i32 1, i32* %15
br label %42
42: ; preds = %52, %41
%43 = load i32, i32* %15
%44 = icmp sle i32 %43, 8
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = load i32, i32* %12
%47 = load i32, i32* %15
%48 = lshr i32 %46, %47
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
br label %55
51: ; preds = %45
br label %52
52: ; preds = %51
%53 = load i32, i32* %15
%54 = add nsw i32 %53, 1
store i32 %54, i32* %15
br label %42
55: ; preds = %50, %42
%56 = call i32 (...) @dact_bit_buffer_purge()
br label %57
57: ; preds = %55, %95
%58 = call i32 (...) @dact_bit_buffer_size()
%59 = load i32, i32* %15
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %69
61: ; preds = %57
%62 = load i8*, i8** %6
%63 = load i32, i32* %14
%64 = add nsw i32 %63, 1
store i32 %64, i32* %14
%65 = sext i32 %63 to i64
%66 = getelementptr inbounds i8, i8* %62, i64 %65
%67 = load i8, i8* %66
%68 = call i32 @dact_bit_buffer_write(i8 zeroext %67, i32 8)
br label %69
69: ; preds = %61, %57
%70 = load i32, i32* %15
%71 = call zeroext i8 @dact_bit_buffer_read(i32 %70)
%72 = zext i8 %71 to i32
%73 = load i8, i8* %11
%74 = zext i8 %73 to i32
%75 = add nsw i32 %72, %74
%76 = trunc i32 %75 to i8
%77 = load i8*, i8** %7
%78 = load i32, i32* %13
%79 = add nsw i32 %78, 1
store i32 %79, i32* %13
%80 = sext i32 %78 to i64
%81 = getelementptr inbounds i8, i8* %77, i64 %80
store i8 %76, i8* %81
%82 = load i32, i32* %14
%83 = load i32, i32* %8
%84 = icmp eq i32 %82, %83
br i1 %84, label %85, label %90
85: ; preds = %69
%86 = call i32 (...) @dact_bit_buffer_size()
%87 = load i32, i32* %15
%88 = icmp slt i32 %86, %87
br i1 %88, label %89, label %90
89: ; preds = %85
br label %96
90: ; preds = %85, %69
%91 = load i32, i32* %13
%92 = load i32, i32* %9
%93 = icmp sge i32 %91, %92
br i1 %93, label %94, label %95
94: ; preds = %90
br label %96
95: ; preds = %90
br label %57
96: ; preds = %94, %89
%97 = load i32, i32* %13
store i32 %97, i32* %5
br label %98
98: ; preds = %96, %35
%99 = load i32, i32* %5
ret i32 %99
}

LLVM IR (-O2):
define i32 @dact_text_decompress(i8* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = load i8, i8* %0
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = load i8, i8* %6
%8 = zext i8 %7 to i32
%9 = zext i8 %5 to i32
%10 = sub nsw i32 %8, %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @memset(i8* %1, i8 zeroext %5, i32 %3)
br label %52
14: ; preds = %4, %14
%15 = phi i32 [ %20, %14 ], [ 1, %4 ]
%16 = icmp ugt i32 %15, 8
%17 = lshr i32 %10, %15
%18 = icmp eq i32 %17, 0
%19 = select i1 %16, i1 true, i1 %18
%20 = add nuw nsw i32 %15, 1
br i1 %19, label %21, label %14
21: ; preds = %14
%22 = tail call i32 (...) @dact_bit_buffer_purge()
br label %23
23: ; preds = %49, %21
%24 = phi i64 [ 0, %21 ], [ %38, %49 ]
%25 = phi i32 [ 2, %21 ], [ %35, %49 ]
%26 = tail call i32 (...) @dact_bit_buffer_size()
%27 = icmp slt i32 %26, %15
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = add nsw i32 %25, 1
%30 = sext i32 %25 to i64
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = load i8, i8* %31
%33 = tail call i32 @dact_bit_buffer_write(i8 zeroext %32, i32 8)
br label %34
34: ; preds = %28, %23
%35 = phi i32 [ %29, %28 ], [ %25, %23 ]
%36 = tail call zeroext i8 @dact_bit_buffer_read(i32 %15)
%37 = add i8 %36, %5
%38 = add nuw i64 %24, 1
%39 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 %37, i8* %39
%40 = icmp eq i32 %35, %2
%41 = trunc i64 %38 to i32
br i1 %40, label %42, label %47
42: ; preds = %34
%43 = tail call i32 (...) @dact_bit_buffer_size()
%44 = icmp sge i32 %43, %15
%45 = icmp slt i32 %41, %3
%46 = select i1 %44, i1 %45, i1 false
br i1 %46, label %49, label %50
47: ; preds = %34
%48 = icmp slt i32 %41, %3
br i1 %48, label %49, label %50
49: ; preds = %47, %42
br label %23
50: ; preds = %47, %42
%51 = trunc i64 %38 to i32
br label %52
52: ; preds = %50, %12
%53 = phi i32 [ %3, %12 ], [ %51, %50 ]
ret i32 %53
}