#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 */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
void *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int write(int, void *, int);
int main(int argc, char **argv) {
  int size;
  int status;
  int disk;
  int ii;
  if (argc < 2) {
    printf("makedisk <size in blocks>\n");
    exit(-1);
  }
  size = atoi(argv[1]);
  if (size < 1) {
    printf("makedisk <size in blocks>\n");
    exit(-1);
  }
  disk = open("stackl.disk", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  if (disk < 0) {
    printf("Unable to open disk\n");
    exit(-2);
  }
  void *buff = calloc(1024, 1);
  if (buff == NULL) {
    printf("Buffer allocation failed\n");
    exit(-3);
  }
  for (ii = 0; ii < size; ii++) {
    status = write(disk, buff, 1024);
    if (status != 1024) {
      printf("File write failed\n");
      exit(-5);
    }
  }
  close(disk);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a6 <main+0x36>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jge    11d6 <main+0x66>
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
mov    0x2e01(%rip),%rax
or     (%rax),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
lea    0xe11(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1232 <main+0xc2>
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
mov    $0x400,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1266 <main+0xf6>
lea    0xde5(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffffd,%edi
call   1090 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12c3 <main+0x153>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x400,-0x18(%rbp)
je     12b0 <main+0x140>
lea    0xdb5(%rip),%rdi
call   1040 <printf@plt>
mov    $0xfffffffb,%edi
call   1090 <exit@plt>
jmp    12b5 <main+0x145>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    126d <main+0xfd>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jle    1231 <main+0xc1>
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
test   %eax,%eax
jle    1231 <main+0xc1>
mov    %eax,%ebx
mov    0x2e21(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
mov    0x2e16(%rip),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe3c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1247 <main+0xd7>
mov    %eax,%r14d
mov    $0x400,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     125d <main+0xed>
mov    %rax,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
cmp    $0x400,%eax
jne    121b <main+0xab>
add    $0xffffffff,%ebx
jne    11f0 <main+0x80>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdea(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffb,%edi
call   1090 <exit@plt>
lea    0xe13(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffd,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"makedisk <size in blocks>\0A\00"
@.str.1 = [12 x i8] c"stackl.disk\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [21 x i8] c"Unable to open disk\0A\00"
@.str.3 = [26 x i8] c"Buffer allocation failed\0A\00"
@.str.4 = [19 x i8] c"File write failed\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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %16
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %32, %33
%35 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %26
%39 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 -2)
unreachable
41: ; preds = %26
%42 = call i8* @calloc(i32 1024, i32 1)
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 -3)
unreachable
48: ; preds = %41
store i32 0, i32* %9
br label %49
49: ; preds = %63, %48
%50 = load i32, i32* %9
%51 = load i32, i32* %6
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %66
53: ; preds = %49
%54 = load i32, i32* %8
%55 = load i8*, i8** %10
%56 = call i32 @write(i32 %54, i8* %55, i32 1024)
store i32 %56, i32* %7
%57 = load i32, i32* %7
%58 = icmp ne i32 %57, 1024
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%61 = call i32 @exit(i32 -5)
unreachable
62: ; preds = %53
br label %63
63: ; preds = %62
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %49
66: ; preds = %49
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [12 x i8] c"stackl.disk\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@str = [18 x i8] c"File write failed\00"
@str.5 = [25 x i8] c"Buffer allocation failed\00"
@str.6 = [20 x i8] c"Unable to open disk\00"
@str.8 = [26 x i8] c"makedisk <size in blocks>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%14 = tail call i32 @exit(i32 -1)
unreachable
15: ; preds = %7
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRUSR
%22 = load i32, i32* @S_IWUSR
%23 = or i32 %22, %21
%24 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.6, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -2)
unreachable
29: ; preds = %15
%30 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1024, i32 1)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
%34 = tail call i32 @exit(i32 -3)
unreachable
35: ; preds = %37
%36 = icmp eq i32 %41, %10
br i1 %36, label %45, label %37
37: ; preds = %29, %35
%38 = phi i32 [ %41, %35 ], [ 0, %29 ]
%39 = tail call i32 @write(i32 %24, i8* %30, i32 1024)
%40 = icmp eq i32 %39, 1024
%41 = add nuw nsw i32 %38, 1
br i1 %40, label %35, label %42
42: ; preds = %37
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%44 = tail call i32 @exit(i32 -5)
unreachable
45: ; preds = %35
%46 = tail call i32 @close(i32 %24)
ret i32 0
}