#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;
long atol(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, long, int /*<<< orphan*/ *);
scalar_t__ malloc(long);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, long);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fp;
  char *padding;
  long nOrgSize;
  long nNewSize;
  long nPadSize;
  if (argc != 3) {
    printf("Usage: padfile <filename> <new length>\n");
    return -1;
  }
  nNewSize = atol(argv[2]);
  fp = fopen(argv[1], "ab+");
  if (fp != NULL) {
    fseek(fp, 0, SEEK_END);
    nOrgSize = ftell(fp);
    if (nNewSize >= nOrgSize) {
      nPadSize = nNewSize - nOrgSize;
      padding = (char *)malloc(nPadSize);
      memset(padding, 0, nPadSize);
      fwrite(padding, sizeof(char), nPadSize, fp);
      free(padding);
    } else {
      printf("New length smaller than file size.\n");
      return -1;
    }
    fclose(fp);
  } else {
    printf("Cannot open: %s.\n", argv[1]);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11da <main+0x3a>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <main+0x13a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atol@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe2e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12b1 <main+0x111>
mov    -0x18(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jl     1289 <main+0xe9>
mov    -0x30(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12a3 <main+0x103>
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <main+0x13a>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12d3 <main+0x133>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <main+0x13a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    1214 <main+0x64>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10b0 <atol@plt>
mov    %rax,%r15
mov    0x8(%rbx),%rdi
lea    0xe29(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1222 <main+0x72>
mov    %rax,%r14
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %r14,%rdi
call   1080 <ftell@plt>
sub    %rax,%r15
jge    1247 <main+0x97>
lea    0xe09(%rip),%rdi
call   1040 <puts@plt>
jmp    1234 <main+0x84>
lea    0xe1e(%rip),%rdi
call   1040 <puts@plt>
jmp    1234 <main+0x84>
mov    0x8(%rbx),%rsi
lea    0xdd7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1070 <memset@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   10d0 <fwrite@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
jmp    1239 <main+0x89>

LLVM IR (-O0):
@.str = [40 x i8] c"Usage: padfile <filename> <new length>\0A\00"
@.str.1 = [4 x i8] c"ab+\00"
@SEEK_END = global i32 0
@.str.2 = [36 x i8] c"New length smaller than file size.\0A\00"
@.str.3 = [18 x i8] c"Cannot open: %s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i64 @atol(i8* %18)
store i64 %19, i64* %9
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %56
26: ; preds = %15
%27 = load i32*, i32** %6
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32*, i32** %6
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %8
%32 = load i64, i64* %9
%33 = load i64, i64* %8
%34 = icmp sge i64 %32, %33
br i1 %34, label %35, label %51
35: ; preds = %26
%36 = load i64, i64* %9
%37 = load i64, i64* %8
%38 = sub nsw i64 %36, %37
store i64 %38, i64* %10
%39 = load i64, i64* %10
%40 = call i64 @malloc(i64 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = load i64, i64* %10
%44 = call i32 @memset(i8* %42, i32 0, i64 %43)
%45 = load i8*, i8** %7
%46 = load i64, i64* %10
%47 = load i32*, i32** %6
%48 = call i32 @fwrite(i8* %45, i32 1, i64 %46, i32* %47)
%49 = load i8*, i8** %7
%50 = call i32 @free(i8* %49)
br label %53
51: ; preds = %26
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
53: ; preds = %35
%54 = load i32*, i32** %6
%55 = call i32 @fclose(i32* %54)
br label %61
56: ; preds = %15
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %59)
store i32 -1, i32* %3
br label %62
61: ; preds = %53
store i32 0, i32* %3
br label %62
62: ; preds = %61, %56, %51, %13
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"ab+\00"
@SEEK_END = global i32 0
@.str.3 = [18 x i8] c"Cannot open: %s.\0A\00"
@str = [35 x i8] c"New length smaller than file size.\00"
@str.4 = [39 x i8] c"Usage: padfile <filename> <new length>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.4, i64 0, i64 0))
br label %32
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i64 @atol(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %29, label %14
14: ; preds = %6
%15 = load i32, i32* @SEEK_END
%16 = tail call i32 @fseek(i32* %12, i32 0, i32 %15)
%17 = tail call i64 @ftell(i32* %12)
%18 = icmp slt i64 %9, %17
br i1 %18, label %27, label %19
19: ; preds = %14
%20 = sub nsw i64 %9, %17
%21 = tail call i64 @malloc(i64 %20)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i32 @memset(i8* %22, i32 0, i64 %20)
%24 = tail call i32 @fwrite(i8* %22, i32 1, i64 %20, i32* %12)
%25 = tail call i32 @free(i8* %22)
%26 = tail call i32 @fclose(i32* %12)
br label %32
27: ; preds = %14
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %32
29: ; preds = %6
%30 = load i8*, i8** %10
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %19, %29, %27, %4
%33 = phi i32 [ -1, %4 ], [ 0, %19 ], [ -1, %27 ], [ -1, %29 ]
ret i32 %33
}