#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 *file, int size) {
  FILE *fp_src_file = NULL;
  char *fn_src_file = file;
  unsigned char *src = NULL;
  int fs = 0;
  fp_src_file = fopen(fn_src_file, "ab+");
  if (NULL == fp_src_file) {
    printf("can't open uboot file for read(%s)\n", fn_src_file);
    return;
  }
  fseek(fp_src_file, 0L, SEEK_END);
  fs = ftell(fp_src_file);
  printf("before padding %s size= %dByte\n", fn_src_file, fs);
  if (fs > size) {
    printf("error: too large %s !!!\n", fn_src_file);
    return;
  }
  src = (unsigned char *)calloc((size_t)(size - fs), 1);
  memset(src, 0, (size_t)(size - fs));
  if (!src) {
    printf("memory alloc failed !!!\n");
    return;
  }
  fs = fwrite(src, 1, (size_t)(size - fs), fp_src_file);
  printf("%d Byte written \n", fs);
  free(src);
  fclose(fp_src_file);
}


x86 assembly (clang, -O0):
<do_add_padding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    11f5 <do_add_padding+0x65>
mov    -0x20(%rbp),%rsi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d9 <do_add_padding+0x149>
mov    -0x18(%rbp),%rdi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    124f <do_add_padding+0xbf>
mov    -0x20(%rbp),%rsi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d9 <do_add_padding+0x149>
mov    -0xc(%rbp),%eax
sub    -0x2c(%rbp),%eax
movslq %eax,%rdi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%eax
sub    -0x2c(%rbp),%eax
movslq %eax,%rdx
xor    %esi,%esi
call   1060 <memset@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1298 <do_add_padding+0x108>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d9 <do_add_padding+0x149>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%eax
sub    -0x2c(%rbp),%eax
movslq %eax,%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_add_padding>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xe4c(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     11ff <do_add_padding+0x5f>
mov    %rax,%r14
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1090 <ftell@plt>
mov    %eax,%ebp
lea    0xe43(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %r15d,%ebp
jle    121a <do_add_padding+0x7a>
lea    0xe4b(%rip),%rdi
jmp    1206 <do_add_padding+0x66>
lea    0xdfe(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <printf@plt>
sub    %ebp,%r15d
movslq %r15d,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1070 <memset@plt>
test   %rbx,%rbx
je     127f <do_add_padding+0xdf>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
lea    0xe05(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
lea    0xded(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <puts@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 = [32 x i8] c"before padding %s size= %dByte\0A\00"
@.str.3 = [25 x i8] c"error: too large %s !!!\0A\00"
@.str.4 = [25 x i8] c"memory alloc failed !!!\0A\00"
@.str.5 = [18 x i8] c"%d Byte written \0A\00"
define void @do_add_padding(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32* null, i32** %5
%9 = load i8*, i8** %3
store i8* %9, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
%10 = load i8*, i8** %6
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp eq i32* null, %12
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %6
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %63
17: ; preds = %2
%18 = load i32*, i32** %5
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i64 0, i32 %19)
%21 = load i32*, i32** %5
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %8
%23 = load i8*, i8** %6
%24 = load i32, i32* %8
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %23, i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* %4
%28 = icmp sgt i32 %26, %27
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = load i8*, i8** %6
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %63
32: ; preds = %17
%33 = load i32, i32* %4
%34 = load i32, i32* %8
%35 = sub nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = call i64 @calloc(i64 %36, i32 1)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = load i32, i32* %4
%41 = load i32, i32* %8
%42 = sub nsw i32 %40, %41
%43 = sext i32 %42 to i64
%44 = call i32 @memset(i8* %39, i32 0, i64 %43)
%45 = load i8*, i8** %7
%46 = icmp ne i8* %45, null
br i1 %46, label %49, label %47
47: ; preds = %32
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %63
49: ; preds = %32
%50 = load i8*, i8** %7
%51 = load i32, i32* %4
%52 = load i32, i32* %8
%53 = sub nsw i32 %51, %52
%54 = sext i32 %53 to i64
%55 = load i32*, i32** %5
%56 = call i32 @fwrite(i8* %50, i32 1, i64 %54, i32* %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = load i8*, i8** %7
%60 = call i32 @free(i8* %59)
%61 = load i32*, i32** %5
%62 = call i32 @fclose(i32* %61)
br label %63
63: ; preds = %49, %47, %29, %14
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 = [32 x i8] c"before padding %s size= %dByte\0A\00"
@.str.3 = [25 x i8] c"error: too large %s !!!\0A\00"
@.str.5 = [18 x i8] c"%d Byte written \0A\00"
@str = [24 x i8] c"memory alloc failed !!!\00"
define void @do_add_padding(i8* %0, i32 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 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 %29
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i64 0, i32 %8)
%10 = tail call i32 @ftell(i32* %3)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %10)
%12 = icmp sgt i32 %10, %1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %29
15: ; preds = %7
%16 = sub nsw i32 %1, %10
%17 = sext i32 %16 to i64
%18 = tail call i64 @calloc(i64 %17, i32 1)
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @memset(i8* %19, i32 0, i64 %17)
%21 = icmp eq i64 %18, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %29
24: ; preds = %15
%25 = tail call i32 @fwrite(i8* %19, i32 1, i64 %17, i32* %3)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i32 %25)
%27 = tail call i32 @free(i8* %19)
%28 = tail call i32 @fclose(i32* %3)
br label %29
29: ; preds = %24, %22, %13, %5
ret void
}