#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 */
scalar_t__ build_crc_table();
unsigned long calculate_file_crc(unsigned char *, int, short);
int /*<<< orphan*/ dispose_pointer(unsigned char *);
int /*<<< orphan*/ free_crc_table();
scalar_t__ new_pointer(int);
unsigned long calculate_crc_for_opened_file(short refnum) {
  unsigned long crc;
  unsigned char *buffer;
  if (build_crc_table()) {
    buffer = (unsigned char *)new_pointer(1024 * sizeof(unsigned char));
    if (buffer) {
      crc = calculate_file_crc(buffer, 1024, refnum);
      dispose_pointer(buffer);
    }
    free_crc_table();
  }
  return crc;
}


x86 assembly (clang, -O0):
<calculate_crc_for_opened_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x2(%rbp)
mov    $0x0,%al
call   1070 <build_crc_table@plt>
cmp    $0x0,%rax
je     11af <calculate_crc_for_opened_file+0x5f>
mov    $0x400,%edi
call   1040 <new_pointer@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a8 <calculate_crc_for_opened_file+0x58>
mov    -0x18(%rbp),%rdi
mov    $0x400,%esi
movswl -0x2(%rbp),%edx
call   1060 <calculate_file_crc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <dispose_pointer@plt>
mov    $0x0,%al
call   1030 <free_crc_table@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_crc_for_opened_file>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %eax,%eax
call   1070 <build_crc_table@plt>
test   %rax,%rax
je     1190 <calculate_crc_for_opened_file+0x40>
mov    $0x400,%edi
call   1040 <new_pointer@plt>
test   %rax,%rax
je     1192 <calculate_crc_for_opened_file+0x42>
mov    %rax,%rbx
movswl %bp,%edx
mov    %rax,%rdi
mov    $0x400,%esi
call   1060 <calculate_file_crc@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1050 <dispose_pointer@plt>
jmp    1192 <calculate_crc_for_opened_file+0x42>
jmp    1199 <calculate_crc_for_opened_file+0x49>
xor    %eax,%eax
call   1030 <free_crc_table@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i64 @calculate_crc_for_opened_file(i16 signext %0) {
%2 = alloca i16
%3 = alloca i64
%4 = alloca i8*
store i16 %0, i16* %2
%5 = call i64 (...) @build_crc_table()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = call i64 @new_pointer(i32 1024)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %18
12: ; preds = %7
%13 = load i8*, i8** %4
%14 = load i16, i16* %2
%15 = call i64 @calculate_file_crc(i8* %13, i32 1024, i16 signext %14)
store i64 %15, i64* %3
%16 = load i8*, i8** %4
%17 = call i32 @dispose_pointer(i8* %16)
br label %18
18: ; preds = %12, %7
%19 = call i32 (...) @free_crc_table()
br label %20
20: ; preds = %18, %1
%21 = load i64, i64* %3
ret i64 %21
}

LLVM IR (-O2):
define i64 @calculate_crc_for_opened_file(i16 signext %0) {
%2 = tail call i64 (...) @build_crc_table()
%3 = icmp eq i64 %2, 0
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = tail call i64 @new_pointer(i32 1024)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i64 @calculate_file_crc(i8* %6, i32 1024, i16 signext %0)
%10 = tail call i32 @dispose_pointer(i8* %6)
br label %11
11: ; preds = %8, %4
%12 = phi i64 [ %9, %8 ], [ undef, %4 ]
%13 = tail call i32 (...) @free_crc_table()
br label %14
14: ; preds = %11, %1
%15 = phi i64 [ %12, %11 ], [ undef, %1 ]
ret i64 %15
}