#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 uint32_t;

/* Variables and functions */
int calc_crc(unsigned char *, int);
int /*<<< orphan*/ gentable(int);
int /*<<< orphan*/ put_uint32le(unsigned char *, int);
void telechips_encode_crc(unsigned char *buf, int length) {
  uint32_t crc1, crc2;
  gentable(0x8001801BL);
  put_uint32le(buf + 0x10, 0);
  put_uint32le(buf + 0x18, 0);
  put_uint32le(buf + 0x1c, length);
  crc1 = calc_crc(buf, length);
  if (length >= 128 * 1024) {
    put_uint32le(buf + 0x18, crc1);
    crc2 = calc_crc(buf, 128 * 1024);
    put_uint32le(buf + 0x10, crc2);
  } else {
    put_uint32le(buf + 0x10, crc1);
  }
}


x86 assembly (clang, -O0):
<telechips_encode_crc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x8001801b,%edi
call   1040 <gentable@plt>
mov    -0x8(%rbp),%rdi
add    $0x10,%rdi
xor    %esi,%esi
call   1030 <put_uint32le@plt>
mov    -0x8(%rbp),%rdi
add    $0x18,%rdi
xor    %esi,%esi
call   1030 <put_uint32le@plt>
mov    -0x8(%rbp),%rdi
add    $0x1c,%rdi
mov    -0xc(%rbp),%esi
call   1030 <put_uint32le@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <calc_crc@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x20000,-0xc(%rbp)
jl     11c9 <telechips_encode_crc+0x99>
mov    -0x8(%rbp),%rdi
add    $0x18,%rdi
mov    -0x10(%rbp),%esi
call   1030 <put_uint32le@plt>
mov    -0x8(%rbp),%rdi
mov    $0x20000,%esi
call   1050 <calc_crc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x10,%rdi
mov    -0x14(%rbp),%esi
call   1030 <put_uint32le@plt>
jmp    11d9 <telechips_encode_crc+0xa9>
mov    -0x8(%rbp),%rdi
add    $0x10,%rdi
mov    -0x10(%rbp),%esi
call   1030 <put_uint32le@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<telechips_encode_crc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x8001801b,%edi
call   1040 <gentable@plt>
lea    0x10(%rbx),%r14
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <put_uint32le@plt>
lea    0x18(%rbx),%r15
mov    %r15,%rdi
xor    %esi,%esi
call   1030 <put_uint32le@plt>
lea    0x1c(%rbx),%rdi
mov    %ebp,%esi
call   1030 <put_uint32le@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <calc_crc@plt>
cmp    $0x20000,%ebp
jl     1196 <telechips_encode_crc+0x66>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <put_uint32le@plt>
mov    %rbx,%rdi
mov    $0x20000,%esi
call   1050 <calc_crc@plt>
mov    %r14,%rdi
mov    %eax,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <put_uint32le@plt>

LLVM IR (-O0):
define void @telechips_encode_crc(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = call i32 @gentable(i32 -2147385317)
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 16
%10 = call i32 @put_uint32le(i8* %9, i32 0)
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 24
%13 = call i32 @put_uint32le(i8* %12, i32 0)
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i64 28
%16 = load i32, i32* %4
%17 = call i32 @put_uint32le(i8* %15, i32 %16)
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = call i32 @calc_crc(i8* %18, i32 %19)
store i32 %20, i32* %5
%21 = load i32, i32* %4
%22 = icmp sge i32 %21, 131072
br i1 %22, label %23, label %34
23: ; preds = %2
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 24
%26 = load i32, i32* %5
%27 = call i32 @put_uint32le(i8* %25, i32 %26)
%28 = load i8*, i8** %3
%29 = call i32 @calc_crc(i8* %28, i32 131072)
store i32 %29, i32* %6
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i64 16
%32 = load i32, i32* %6
%33 = call i32 @put_uint32le(i8* %31, i32 %32)
br label %39
34: ; preds = %2
%35 = load i8*, i8** %3
%36 = getelementptr inbounds i8, i8* %35, i64 16
%37 = load i32, i32* %5
%38 = call i32 @put_uint32le(i8* %36, i32 %37)
br label %39
39: ; preds = %34, %23
ret void
}

LLVM IR (-O2):
define void @telechips_encode_crc(i8* %0, i32 %1) {
%3 = tail call i32 @gentable(i32 -2147385317)
%4 = getelementptr inbounds i8, i8* %0, i64 16
%5 = tail call i32 @put_uint32le(i8* %4, i32 0)
%6 = getelementptr inbounds i8, i8* %0, i64 24
%7 = tail call i32 @put_uint32le(i8* %6, i32 0)
%8 = getelementptr inbounds i8, i8* %0, i64 28
%9 = tail call i32 @put_uint32le(i8* %8, i32 %1)
%10 = tail call i32 @calc_crc(i8* %0, i32 %1)
%11 = icmp sgt i32 %1, 131071
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = tail call i32 @put_uint32le(i8* %6, i32 %10)
%14 = tail call i32 @calc_crc(i8* %0, i32 131072)
br label %15
15: ; preds = %2, %12
%16 = phi i32 [ %14, %12 ], [ %10, %2 ]
%17 = tail call i32 @put_uint32le(i8* %4, i32 %16)
ret void
}