#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 */

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, char *, int);
int fill_testfile(int testfile_descriptor, int testfile_size) {
  char *zero;
  int i;
  int parts;
  parts = (testfile_size / 51200);
  if (parts < 1)
    parts = 1;
  zero = (char *)calloc((testfile_size / parts) * 1024, sizeof(char));
  for (i = 0; i < (testfile_size / parts) * 1024; i++)
    zero[i] = 'A';
  for (i = 0; i < parts; i++) {
    if (write(testfile_descriptor, (char *)zero,
              ((testfile_size / parts) * 1024) * sizeof(char)) < 0) {
      perror("write");
      free(zero);
      return -1;
    }
  }
  free(zero);
  return testfile_descriptor;
}


x86 assembly (clang, -O0):
<fill_testfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xc800,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jge    116d <fill_testfile+0x2d>
movl   $0x1,-0x20(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,%edi
shl    $0xa,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
cltd
idivl  -0x20(%rbp)
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
shl    $0xa,%ecx
cmp    %ecx,%eax
jge    11c5 <fill_testfile+0x85>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x41,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <fill_testfile+0x4e>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1234 <fill_testfile+0xf4>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%eax
cltd
idivl  -0x20(%rbp)
shl    $0xa,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    1221 <fill_testfile+0xe1>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <fill_testfile+0x103>
jmp    1226 <fill_testfile+0xe6>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11cc <fill_testfile+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fill_testfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movslq %esi,%rax
imul   $0x51eb851f,%rax,%rbx
mov    %rbx,%rcx
shr    $0x3f,%rcx
sar    $0x2e,%rbx
add    %ecx,%ebx
test   %ebx,%ebx
mov    $0x1,%r15d
cmovle %r15d,%ebx
mov    %edi,%r14d
cltd
idiv   %ebx
mov    %eax,%r12d
mov    %eax,%ebp
shl    $0xa,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
test   %r12d,%r12d
jle    11c0 <fill_testfile+0x70>
cmp    $0x2,%ebp
cmovge %ebp,%r15d
mov    %r13,%rdi
mov    $0x41,%esi
mov    %r15,%rdx
call   1050 <memset@plt>
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
test   %rax,%rax
js     11f1 <fill_testfile+0xa1>
add    $0xffffffff,%ebx
jne    11c0 <fill_testfile+0x70>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11d7 <fill_testfile+0x87>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i32 @fill_testfile(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = sdiv i32 %9, 51200
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 1, i32* %8
br label %14
14: ; preds = %13, %2
%15 = load i32, i32* %5
%16 = load i32, i32* %8
%17 = sdiv i32 %15, %16
%18 = mul nsw i32 %17, 1024
%19 = call i64 @calloc(i32 %18, i32 1)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %6
store i32 0, i32* %7
br label %21
21: ; preds = %33, %14
%22 = load i32, i32* %7
%23 = load i32, i32* %5
%24 = load i32, i32* %8
%25 = sdiv i32 %23, %24
%26 = mul nsw i32 %25, 1024
%27 = icmp slt i32 %22, %26
br i1 %27, label %28, label %36
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 65, i8* %32
br label %33
33: ; preds = %28
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %21
36: ; preds = %21
store i32 0, i32* %7
br label %37
37: ; preds = %58, %36
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %61
41: ; preds = %37
%42 = load i32, i32* %4
%43 = load i8*, i8** %6
%44 = load i32, i32* %5
%45 = load i32, i32* %8
%46 = sdiv i32 %44, %45
%47 = mul nsw i32 %46, 1024
%48 = sext i32 %47 to i64
%49 = mul i64 %48, 1
%50 = trunc i64 %49 to i32
%51 = call i64 @write(i32 %42, i8* %43, i32 %50)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %41
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
store i32 -1, i32* %3
br label %65
57: ; preds = %41
br label %58
58: ; preds = %57
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 1
store i32 %60, i32* %7
br label %37
61: ; preds = %37
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
%64 = load i32, i32* %4
store i32 %64, i32* %3
br label %65
65: ; preds = %61, %53
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i32 @fill_testfile(i32 %0, i32 %1) {
%3 = sdiv i32 %1, 51200
%4 = icmp sgt i32 %3, 1
%5 = select i1 %4, i32 %3, i32 1
%6 = sdiv i32 %1, %5
%7 = shl nsw i32 %6, 10
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = icmp sgt i32 %6, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @llvm.smax.i32(i32 %7, i32 1)
%13 = zext i32 %12 to i64
br label %14
14: ; preds = %11, %2
br label %17
15: ; preds = %17
%16 = icmp eq i32 %21, %5
br i1 %16, label %24, label %17
17: ; preds = %14, %15
%18 = phi i32 [ %21, %15 ], [ 0, %14 ]
%19 = tail call i64 @write(i32 %0, i8* %9, i32 %7)
%20 = icmp slt i64 %19, 0
%21 = add nuw nsw i32 %18, 1
br i1 %20, label %22, label %15
22: ; preds = %17
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %15, %22
%25 = phi i32 [ -1, %22 ], [ %0, %15 ]
%26 = tail call i32 @free(i8* %9)
ret i32 %25
}