#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ calc_bl2_chksum(unsigned long);
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
void do_chksum(char *src_file, char *dst_file) {
  FILE *fp_src_uboot = NULL;
  FILE *fp_dst_uboot = NULL;
  char *fn_src_uboot = src_file;
  char *fn_dst_uboot = dst_file;
  unsigned char *src = NULL;
  int fs = 0;
  fp_src_uboot = fopen(fn_src_uboot, "rb");
  if (NULL == fp_src_uboot) {
    printf("can't open uboot file for read(%s)\n", fn_src_uboot);
    return;
  }
  fp_dst_uboot = fopen(fn_dst_uboot, "wb");
  if (NULL == fp_src_uboot) {
    printf("can't open uboot file for read(%s)\n", fn_src_uboot);
    return;
  }
  fseek(fp_src_uboot, 0L, SEEK_END);
  fs = ftell(fp_src_uboot);
  printf("bl2aa file size= %dB\n", fs);
  rewind(fp_src_uboot);
  src = (unsigned char *)calloc((size_t)fs, 1);
  fread(src, 1, (size_t)fs, fp_src_uboot);
  calc_bl2_chksum((unsigned long)src);
  fwrite(src, 1, (size_t)fs, fp_dst_uboot);
  free(src);
  fclose(fp_src_uboot);
  fclose(fp_dst_uboot);
}


x86 assembly (clang, -O0):
<do_chksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe06(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    1226 <do_chksum+0x76>
mov    -0x28(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12fb <do_chksum+0x14b>
mov    -0x30(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    125d <do_chksum+0xad>
mov    -0x28(%rbp),%rsi
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12fb <do_chksum+0x14b>
mov    -0x18(%rbp),%rdi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
movslq -0x3c(%rbp),%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x3c(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x38(%rbp),%rdi
call   10c0 <calc_bl2_chksum@plt>
mov    -0x38(%rbp),%rdi
movslq -0x3c(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10d0 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_chksum>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe3b(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1281 <do_chksum+0xd1>
mov    %rax,%r15
lea    0xe4a(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
mov    %rax,%r14
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r15,%rdi
call   1090 <ftell@plt>
mov    %eax,%ebx
lea    0xe1e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <rewind@plt>
movslq %ebx,%r12
mov    %r12,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   10c0 <calc_bl2_chksum@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r14,%rcx
call   10d0 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>
lea    0xd7b(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1060 <printf@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [36 x i8] c"can't open uboot file for read(%s)\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@.str.3 = [22 x i8] c"bl2aa file size= %dB\0A\00"
define void @do_chksum(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32* null, i32** %5
store i32* null, i32** %6
%11 = load i8*, i8** %3
store i8* %11, i8** %7
%12 = load i8*, i8** %4
store i8* %12, i8** %8
store i8* null, i8** %9
store i32 0, i32* %10
%13 = load i8*, i8** %7
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %5
%15 = load i32*, i32** %5
%16 = icmp eq i32* null, %15
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %61
20: ; preds = %2
%21 = load i8*, i8** %8
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %6
%23 = load i32*, i32** %5
%24 = icmp eq i32* null, %23
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %61
28: ; preds = %20
%29 = load i32*, i32** %5
%30 = load i32, i32* @SEEK_END
%31 = call i32 @fseek(i32* %29, i64 0, i32 %30)
%32 = load i32*, i32** %5
%33 = call i32 @ftell(i32* %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = load i32*, i32** %5
%37 = call i32 @rewind(i32* %36)
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = call i64 @calloc(i64 %39, i32 1)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %9
%42 = load i8*, i8** %9
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = load i32*, i32** %5
%46 = call i32 @fread(i8* %42, i32 1, i64 %44, i32* %45)
%47 = load i8*, i8** %9
%48 = ptrtoint i8* %47 to i64
%49 = call i32 @calc_bl2_chksum(i64 %48)
%50 = load i8*, i8** %9
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = load i32*, i32** %6
%54 = call i32 @fwrite(i8* %50, i32 1, i64 %52, i32* %53)
%55 = load i8*, i8** %9
%56 = call i32 @free(i8* %55)
%57 = load i32*, i32** %5
%58 = call i32 @fclose(i32* %57)
%59 = load i32*, i32** %6
%60 = call i32 @fclose(i32* %59)
br label %61
61: ; preds = %28, %25, %17
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [36 x i8] c"can't open uboot file for read(%s)\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@SEEK_END = global i32 0
@.str.3 = [22 x i8] c"bl2aa file size= %dB\0A\00"
define void @do_chksum(i8* %0, i8* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %23
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %3, i64 0, i32 %9)
%11 = tail call i32 @ftell(i32* %3)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @rewind(i32* %3)
%14 = sext i32 %11 to i64
%15 = tail call i64 @calloc(i64 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @fread(i8* %16, i32 1, i64 %14, i32* %3)
%18 = tail call i32 @calc_bl2_chksum(i64 %15)
%19 = tail call i32 @fwrite(i8* %16, i32 1, i64 %14, i32* %8)
%20 = tail call i32 @free(i8* %16)
%21 = tail call i32 @fclose(i32* %3)
%22 = tail call i32 @fclose(i32* %8)
br label %23
23: ; preds = %7, %5
ret void
}