#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 short ldbs_peek2(unsigned char *);
int /*<<< orphan*/ memset(unsigned char *, unsigned char, unsigned short);
size_t adisk_decompress(unsigned char *compressed, size_t c_len,
                        unsigned char *result) {
  unsigned char *readp, *writep;
  unsigned short blklen;
  size_t u_len = 0;
  readp = compressed;
  writep = NULL;
  if (result)
    writep = result;
  u_len = 0;
  while (c_len >= 3) {
    blklen = ldbs_peek2(readp);
    readp += 2;
    if (writep) {
      memset(writep, readp[0], blklen);
      writep += blklen;
    }
    u_len += blklen;
    readp++;
    c_len -= 3;
  }
  return u_len;
}


x86 assembly (clang, -O0):
<adisk_decompress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     115f <adisk_decompress+0x3f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x3,-0x10(%rbp)
jb     11ea <adisk_decompress+0xca>
mov    -0x20(%rbp),%rdi
call   1040 <ldbs_peek2@plt>
mov    %ax,-0x2a(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11c1 <adisk_decompress+0xa1>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%cl
mov    -0x2a(%rbp),%ax
movzbl %cl,%esi
movzwl %ax,%edx
call   1030 <memset@plt>
movzwl -0x2a(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
movzwl -0x2a(%rbp),%eax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x3,%rax
mov    %rax,-0x10(%rbp)
jmp    1167 <adisk_decompress+0x47>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adisk_decompress>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
cmp    $0x3,%rsi
jb     117d <adisk_decompress+0x5d>
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r14d,%r14d
jmp    1167 <adisk_decompress+0x47>
nopl   (%rax)
movzbl 0x2(%rbx),%esi
movzwl %ax,%r13d
mov    %r12,%rdi
mov    %r13d,%edx
call   1030 <memset@plt>
add    %r13,%r12
add    %r13,%r14
add    $0x3,%rbx
add    $0xfffffffffffffffd,%r15
cmp    $0x2,%r15
jbe    1180 <adisk_decompress+0x60>
mov    %rbx,%rdi
call   1040 <ldbs_peek2@plt>
test   %r12,%r12
jne    1140 <adisk_decompress+0x20>
movzwl %ax,%r13d
xor    %r12d,%r12d
jmp    1156 <adisk_decompress+0x36>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @adisk_decompress(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i16
%10 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
store i64 0, i64* %10
%11 = load i8*, i8** %4
store i8* %11, i8** %7
store i8* null, i8** %8
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i8*, i8** %6
store i8* %15, i8** %8
br label %16
16: ; preds = %14, %3
store i64 0, i64* %10
br label %17
17: ; preds = %39, %16
%18 = load i64, i64* %5
%19 = icmp uge i64 %18, 3
br i1 %19, label %20, label %48
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = call zeroext i16 @ldbs_peek2(i8* %21)
store i16 %22, i16* %9
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 2
store i8* %24, i8** %7
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %39
27: ; preds = %20
%28 = load i8*, i8** %8
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = load i16, i16* %9
%33 = call i32 @memset(i8* %28, i8 zeroext %31, i16 zeroext %32)
%34 = load i16, i16* %9
%35 = zext i16 %34 to i32
%36 = load i8*, i8** %8
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %8
br label %39
39: ; preds = %27, %20
%40 = load i16, i16* %9
%41 = zext i16 %40 to i64
%42 = load i64, i64* %10
%43 = add i64 %42, %41
store i64 %43, i64* %10
%44 = load i8*, i8** %7
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %7
%46 = load i64, i64* %5
%47 = sub i64 %46, 3
store i64 %47, i64* %5
br label %17
48: ; preds = %17
%49 = load i64, i64* %10
ret i64 %49
}

LLVM IR (-O2):
define i64 @adisk_decompress(i8* %0, i64 %1, i8* %2) {
%4 = icmp ugt i64 %1, 2
br i1 %4, label %5, label %27
5: ; preds = %3, %20
%6 = phi i64 [ %23, %20 ], [ 0, %3 ]
%7 = phi i64 [ %25, %20 ], [ %1, %3 ]
%8 = phi i8* [ %22, %20 ], [ %2, %3 ]
%9 = phi i8* [ %24, %20 ], [ %0, %3 ]
%10 = tail call zeroext i16 @ldbs_peek2(i8* %9)
%11 = icmp eq i8* %8, null
br i1 %11, label %12, label %14
12: ; preds = %5
%13 = zext i16 %10 to i64
br label %20
14: ; preds = %5
%15 = getelementptr inbounds i8, i8* %9, i64 2
%16 = load i8, i8* %15
%17 = tail call i32 @memset(i8* %8, i8 zeroext %16, i16 zeroext %10)
%18 = zext i16 %10 to i64
%19 = getelementptr inbounds i8, i8* %8, i64 %18
br label %20
20: ; preds = %12, %14
%21 = phi i64 [ %13, %12 ], [ %18, %14 ]
%22 = phi i8* [ null, %12 ], [ %19, %14 ]
%23 = add i64 %6, %21
%24 = getelementptr inbounds i8, i8* %9, i64 3
%25 = add i64 %7, -3
%26 = icmp ugt i64 %25, 2
br i1 %26, label %5, label %27
27: ; preds = %20, %3
%28 = phi i64 [ 0, %3 ], [ %23, %20 ]
ret i64 %28
}