#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;
scalar_t__ calloc(size_t, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int fwrite(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, size_t);
int /*<<< orphan*/ printf(char *, ...);
void do_add_padding(char *uboot_file) {
  FILE *fp_src_uboot = NULL;
  char *fn_src_uboot = uboot_file;
  unsigned char *src = NULL;
  int fs = 0;
  fp_src_uboot = fopen(fn_src_uboot, "ab+");
  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("before padding uboot.bin file size= %dB\n", fs);
  if (fs > (328 * 1024)) {
    printf("error: too large uboot !!!");
    return;
  }
  src = (unsigned char *)calloc((size_t)((328 * 1024) - fs), 1);
  memset(src, 0, (size_t)((328 * 1024) - fs));
  if (!src) {
    printf("memory alloc failed !!!");
    return;
  }
  fs = fwrite(src, 1, (size_t)((328 * 1024) - fs), fp_src_uboot);
  printf("%d B written \n", fs);
  free(src);
  fclose(fp_src_uboot);
}


x86 assembly (clang, -O0):
<do_add_padding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    11f2 <do_add_padding+0x62>
mov    -0x18(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d5 <do_add_padding+0x145>
mov    -0x10(%rbp),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x52000,-0x24(%rbp)
jle    1245 <do_add_padding+0xb5>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d5 <do_add_padding+0x145>
mov    $0x52000,%eax
sub    -0x24(%rbp),%eax
movslq %eax,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x52000,%eax
sub    -0x24(%rbp),%eax
movslq %eax,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1292 <do_add_padding+0x102>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d5 <do_add_padding+0x145>
mov    -0x20(%rbp),%rdi
mov    $0x52000,%eax
sub    -0x24(%rbp),%eax
movslq %eax,%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_add_padding>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe5e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11f0 <do_add_padding+0x60>
mov    %rax,%r12
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebx
lea    0xe55(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x52001,%ebx
jl     120c <do_add_padding+0x7c>
lea    0xe66(%rip),%rdi
jmp    127c <do_add_padding+0xec>
lea    0xe0d(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <printf@plt>
mov    $0x52000,%r15d
sub    %ebx,%r15d
mov    %r15,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1060 <memset@plt>
test   %r14,%r14
je     1275 <do_add_padding+0xe5>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   10b0 <fwrite@plt>
lea    0xe33(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>
lea    0xdf0(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <printf@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"ab+\00"
@.str.1 = [36 x i8] c"can't open uboot file for read(%s)\0A\00"
@SEEK_END = global i32 0
@.str.2 = [41 x i8] c"before padding uboot.bin file size= %dB\0A\00"
@.str.3 = [27 x i8] c"error: too large uboot !!!\00"
@.str.4 = [24 x i8] c"memory alloc failed !!!\00"
@.str.5 = [15 x i8] c"%d B written \0A\00"
define void @do_add_padding(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
store i32* null, i32** %3
%7 = load i8*, i8** %2
store i8* %7, i8** %4
store i8* null, i8** %5
store i32 0, i32* %6
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %3
%10 = load i32*, i32** %3
%11 = icmp eq i32* null, %10
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %55
15: ; preds = %1
%16 = load i32*, i32** %3
%17 = load i32, i32* @SEEK_END
%18 = call i32 @fseek(i32* %16, i64 0, i32 %17)
%19 = load i32*, i32** %3
%20 = call i32 @ftell(i32* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 335872
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %55
27: ; preds = %15
%28 = load i32, i32* %6
%29 = sub nsw i32 335872, %28
%30 = sext i32 %29 to i64
%31 = call i64 @calloc(i64 %30, i32 1)
%32 = inttoptr i64 %31 to i8*
store i8* %32, i8** %5
%33 = load i8*, i8** %5
%34 = load i32, i32* %6
%35 = sub nsw i32 335872, %34
%36 = sext i32 %35 to i64
%37 = call i32 @memset(i8* %33, i32 0, i64 %36)
%38 = load i8*, i8** %5
%39 = icmp ne i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %27
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %55
42: ; preds = %27
%43 = load i8*, i8** %5
%44 = load i32, i32* %6
%45 = sub nsw i32 335872, %44
%46 = sext i32 %45 to i64
%47 = load i32*, i32** %3
%48 = call i32 @fwrite(i8* %43, i32 1, i64 %46, i32* %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = load i8*, i8** %5
%52 = call i32 @free(i8* %51)
%53 = load i32*, i32** %3
%54 = call i32 @fclose(i32* %53)
br label %55
55: ; preds = %42, %40, %25, %12
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"ab+\00"
@.str.1 = [36 x i8] c"can't open uboot file for read(%s)\0A\00"
@SEEK_END = global i32 0
@.str.2 = [41 x i8] c"before padding uboot.bin file size= %dB\0A\00"
@.str.3 = [27 x i8] c"error: too large uboot !!!\00"
@.str.4 = [24 x i8] c"memory alloc failed !!!\00"
@.str.5 = [15 x i8] c"%d B written \0A\00"
define void @do_add_padding(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = 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 %28
6: ; preds = %1
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %2, i64 0, i32 %7)
%9 = tail call i32 @ftell(i32* %2)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %9, 335872
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %28
14: ; preds = %6
%15 = sub nsw i32 335872, %9
%16 = zext i32 %15 to i64
%17 = tail call i64 @calloc(i64 %16, i32 1)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i32 @memset(i8* %18, i32 0, i64 %16)
%20 = icmp eq i64 %17, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %28
23: ; preds = %14
%24 = tail call i32 @fwrite(i8* %18, i32 1, i64 %16, i32* %2)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %24)
%26 = tail call i32 @free(i8* %18)
%27 = tail call i32 @fclose(i32* %2)
br label %28
28: ; preds = %23, %21, %12, %4
ret void
}