#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 */
struct stat {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ stat(char const *, struct stat *);
void qfits_zeropad(const char *filename) {
  struct stat sta;
  int size;
  int remaining;
  FILE *out;
  char *buf;
  if (filename == NULL)
    return;
  if (stat(filename, &sta) != 0) {
    return;
  }
  size = (int)sta.st_size;
  remaining = size % (2880);
  if (remaining == 0)
    return;
  remaining = (2880) - remaining;
  if ((out = fopen(filename, "a")) == NULL)
    return;
  buf = calloc(remaining, sizeof(char));
  fwrite(buf, 1, remaining, out);
  fclose(out);
  free(buf);
  return;
}


x86 assembly (clang, -O0):
<qfits_zeropad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    117c <qfits_zeropad+0x1c>
jmp    1222 <qfits_zeropad+0xc2>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1198 <qfits_zeropad+0x38>
jmp    1222 <qfits_zeropad+0xc2>
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    $0xb40,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11bc <qfits_zeropad+0x5c>
jmp    1222 <qfits_zeropad+0xc2>
mov    $0xb40,%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ea <qfits_zeropad+0x8a>
jmp    1222 <qfits_zeropad+0xc2>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qfits_zeropad>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11f4 <qfits_zeropad+0x94>
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    11f4 <qfits_zeropad+0x94>
movslq (%rsp),%rbp
imul   $0xffffffffb60b60b7,%rbp,%rax
shr    $0x20,%rax
add    %ebp,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0xb,%eax
add    %ecx,%eax
imul   $0xb40,%eax,%eax
sub    %eax,%ebp
je     11f4 <qfits_zeropad+0x94>
lea    0xe54(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f4 <qfits_zeropad+0x94>
mov    %rax,%r15
mov    $0xb40,%ebx
sub    %ebp,%ebx
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r15,%rcx
call   1080 <fwrite@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [2 x i8] c"a\00"
define void @qfits_zeropad(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
br label %43
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i64 @stat(i8* %12, %struct.stat* %3)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
br label %43
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%18 = load i64, i64* %17
%19 = trunc i64 %18 to i32
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = srem i32 %20, 2880
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %16
br label %43
25: ; preds = %16
%26 = load i32, i32* %5
%27 = sub nsw i32 2880, %26
store i32 %27, i32* %5
%28 = load i8*, i8** %2
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %29, i32** %6
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %32
31: ; preds = %25
br label %43
32: ; preds = %25
%33 = load i32, i32* %5
%34 = call i8* @calloc(i32 %33, i32 1)
store i8* %34, i8** %7
%35 = load i8*, i8** %7
%36 = load i32, i32* %5
%37 = load i32*, i32** %6
%38 = call i32 @fwrite(i8* %35, i32 1, i32 %36, i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %7
%42 = call i32 @free(i8* %41)
br label %43
43: ; preds = %32, %31, %24, %15, %10
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [2 x i8] c"a\00"
define void @qfits_zeropad(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %23, label %5
5: ; preds = %1
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %5
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i64, i64* %9
%11 = trunc i64 %10 to i32
%12 = srem i32 %11, 2880
%13 = icmp eq i32 %12, 0
br i1 %13, label %23, label %14
14: ; preds = %8
%15 = sub nsw i32 2880, %12
%16 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %23, label %18
18: ; preds = %14
%19 = call i8* @calloc(i32 %15, i32 1)
%20 = call i32 @fwrite(i8* %19, i32 1, i32 %15, i32* %16)
%21 = call i32 @fclose(i32* %16)
%22 = call i32 @free(i8* %19)
br label %23
23: ; preds = %14, %8, %5, %1, %18
ret void
}