#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*/ ch;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, size, i, unit, a, b;
  char buf[1024];
  char ch = 'A';
  if (argc != 3 && argc != 4) {
    fprintf(stderr, "Usage: %s <filename> <size> [unit]\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  memset(buf, 'A', sizeof(buf));
  fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC);
  if (fd == -1) {
    perror("open() failed");
    exit(EXIT_FAILURE);
  }
  if (argc == 4) {
    switch ((int)argv[3][0]) {
    case 'k':
    case 'K':
      unit = 1 << 10;
      break;
    case 'm':
    case 'M':
      unit = 1 << 20;
      break;
    case 'g':
    case 'G':
      unit = 1 << 30;
      break;
    case 'b':
    case 'B':
    default:
      unit = 1;
      break;
    }
  }
  size = atoi(argv[2]) * unit;
  printf("size = %d\n", size);
  a = size / 1024;
  b = size % 1024;
  for (i = 0; i < a; ++i)
    write(fd, buf, sizeof(buf));
  for (i = 0; i < b; ++i)
    write(fd, &ch, sizeof(ch));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x41,-0x431(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ee <main+0x5e>
cmpl   $0x4,-0x8(%rbp)
je     11ee <main+0x5e>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xed5(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x430(%rbp),%rdi
mov    $0x41,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
or     (%rax),%esi
mov    0x2dcb(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1253 <main+0xc3>
lea    0xe94(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x4,-0x8(%rbp)
jne    12c9 <main+0x139>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
movsbl (%rax),%eax
add    $0xffffffbe,%eax
mov    %eax,%ecx
mov    %rcx,-0x440(%rbp)
sub    $0x2b,%eax
ja     12bd <main+0x12d>
mov    -0x440(%rbp),%rax
lea    0xd75(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x400,-0x20(%rbp)
jmp    12c4 <main+0x134>
movl   $0x100000,-0x20(%rbp)
jmp    12c4 <main+0x134>
movl   $0x40000000,-0x20(%rbp)
jmp    12c4 <main+0x134>
jmp    12bd <main+0x12d>
movl   $0x1,-0x20(%rbp)
jmp    12c9 <main+0x139>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
imul   -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdfb(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %edx,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    133d <main+0x1ad>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    130f <main+0x17f>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    1372 <main+0x1e2>
mov    -0x14(%rbp),%edi
lea    -0x431(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1344 <main+0x1b4>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
movb   $0x41,0xf(%rsp)
lea    -0x5(%r15),%eax
cmp    $0xfffffffd,%eax
jbe    12ff <main+0x16f>
lea    0x10(%rsp),%rdi
mov    $0x41,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    0x2de6(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1319 <main+0x189>
mov    %eax,%r12d
cmp    $0x4,%r15d
jne    1258 <main+0xc8>
mov    0x18(%r14),%rax
movsbl (%rax),%eax
add    $0xffffffb9,%eax
cmp    $0x26,%eax
ja     1253 <main+0xc3>
movabs $0x100000001,%rcx
bt     %rax,%rcx
jb     124c <main+0xbc>
mov    $0x400,%ebx
movabs $0x1000000010,%rcx
bt     %rax,%rcx
jb     1258 <main+0xc8>
movabs $0x4000000040,%rcx
bt     %rax,%rcx
jae    1253 <main+0xc3>
mov    $0x100000,%ebx
jmp    1258 <main+0xc8>
mov    $0x40000000,%ebx
jmp    1258 <main+0xc8>
mov    $0x1,%ebx
mov    0x10(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
imul   %ebx,%r14d
lea    0xdc3(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
lea    0x3ff(%r14),%ebx
test   %r14d,%r14d
cmovns %r14d,%ebx
mov    %ebx,%eax
and    $0xfffffc00,%eax
mov    %r14d,%ebp
sub    %eax,%ebp
cmp    $0x400,%r14d
jl     12c5 <main+0x135>
sar    $0xa,%ebx
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebx
jne    12b0 <main+0x120>
test   %ebp,%ebp
jle    12e5 <main+0x155>
lea    0xf(%rsp),%r14
xchg   %ax,%ax
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebp
jne    12d0 <main+0x140>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xcee(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1325 <main+0x195>
lea    0xd04(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"Usage: %s <filename> <size> [unit]\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@.str.2 = [11 x i8] c"size = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1024 x i8]
%13 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 65, i8* %13
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %27
16: ; preds = %2
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 4
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %16, %2
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%29 = call i32 @memset(i8* %28, i8 signext 65, i32 1024)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %32, i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %27
%42 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %27
%46 = load i32, i32* %4
%47 = icmp eq i32 %46, 4
br i1 %47, label %48, label %61
48: ; preds = %45
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 3
%51 = load i8*, i8** %50
%52 = getelementptr inbounds i8, i8* %51, i64 0
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
switch i32 %54, label %59 [
i32 107, label %55
i32 75, label %55
i32 109, label %56
i32 77, label %56
i32 103, label %57
i32 71, label %57
i32 98, label %58
i32 66, label %58
]
55: ; preds = %48, %48
store i32 1024, i32* %9
br label %60
56: ; preds = %48, %48
store i32 1048576, i32* %9
br label %60
57: ; preds = %48, %48
store i32 1073741824, i32* %9
br label %60
58: ; preds = %48, %48
br label %59
59: ; preds = %48, %58
store i32 1, i32* %9
br label %60
60: ; preds = %59, %57, %56, %55
br label %61
61: ; preds = %60, %45
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 2
%64 = load i8*, i8** %63
%65 = call i32 @atoi(i8* %64)
%66 = load i32, i32* %9
%67 = mul nsw i32 %65, %66
store i32 %67, i32* %7
%68 = load i32, i32* %7
%69 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %68)
%70 = load i32, i32* %7
%71 = sdiv i32 %70, 1024
store i32 %71, i32* %10
%72 = load i32, i32* %7
%73 = srem i32 %72, 1024
store i32 %73, i32* %11
store i32 0, i32* %8
br label %74
74: ; preds = %82, %61
%75 = load i32, i32* %8
%76 = load i32, i32* %10
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %85
78: ; preds = %74
%79 = load i32, i32* %6
%80 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%81 = call i32 @write(i32 %79, i8* %80, i32 1024)
br label %82
82: ; preds = %78
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 1
store i32 %84, i32* %8
br label %74
85: ; preds = %74
store i32 0, i32* %8
br label %86
86: ; preds = %93, %85
%87 = load i32, i32* %8
%88 = load i32, i32* %11
%89 = icmp slt i32 %87, %88
br i1 %89, label %90, label %96
90: ; preds = %86
%91 = load i32, i32* %6
%92 = call i32 @write(i32 %91, i8* %13, i32 1)
br label %93
93: ; preds = %90
%94 = load i32, i32* %8
%95 = add nsw i32 %94, 1
store i32 %95, i32* %8
br label %86
96: ; preds = %86
%97 = load i32, i32* %6
%98 = call i32 @close(i32 %97)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"Usage: %s <filename> <size> [unit]\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@.str.2 = [11 x i8] c"size = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca i8
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 65, i8* %4
%6 = add i32 %0, -5
%7 = icmp ult i32 %6, -2
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = call i32 @memset(i8* %5, i8 signext 65, i32 1024)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = load i32, i32* @O_TRUNC
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %14
%30 = icmp eq i32 %0, 4
br i1 %30, label %31, label %39
31: ; preds = %29
%32 = getelementptr inbounds i8*, i8** %1, i64 3
%33 = load i8*, i8** %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
switch i32 %35, label %38 [
i32 107, label %39
i32 75, label %39
i32 109, label %36
i32 77, label %36
i32 103, label %37
i32 71, label %37
]
36: ; preds = %31, %31
br label %39
37: ; preds = %31, %31
br label %39
38: ; preds = %31
br label %39
39: ; preds = %31, %31, %36, %37, %38, %29
%40 = phi i32 [ 1, %38 ], [ 1073741824, %37 ], [ 1048576, %36 ], [ undef, %29 ], [ 1024, %31 ], [ 1024, %31 ]
%41 = getelementptr inbounds i8*, i8** %1, i64 2
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
%44 = mul nsw i32 %43, %40
%45 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = sdiv i32 %44, 1024
%47 = srem i32 %44, 1024
%48 = icmp sgt i32 %44, 1023
br i1 %48, label %51, label %49
49: ; preds = %51, %39
%50 = icmp sgt i32 %47, 0
br i1 %50, label %56, label %61
51: ; preds = %39, %51
%52 = phi i32 [ %54, %51 ], [ 0, %39 ]
%53 = call i32 @write(i32 %23, i8* %5, i32 1024)
%54 = add nuw nsw i32 %52, 1
%55 = icmp eq i32 %54, %46
br i1 %55, label %49, label %51
56: ; preds = %49, %56
%57 = phi i32 [ %59, %56 ], [ 0, %49 ]
%58 = call i32 @write(i32 %23, i8* %4, i32 1)
%59 = add nuw nsw i32 %57, 1
%60 = icmp eq i32 %59, %47
br i1 %60, label %61, label %56
61: ; preds = %56, %49
%62 = call i32 @close(i32 %23)
ret i32 0
}