#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_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int errno;
int /*<<< orphan*/ exit(int);
char *malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fdWrite, n;
  char *buf;
  int blocksize, blockcount;
  if (argc != 3) {
    printf("Usage: %s blocksize blockcount\n", argv[0]);
    exit(1);
  }
  blocksize = atoi(argv[1]);
  blockcount = atoi(argv[2]);
  if ((buf = malloc(blocksize * (sizeof(char)))) == NULL) {
    printf("%s\n", "error allocating blocksize, out of heap memory");
    exit(1);
  }
  if ((fdWrite = open("bar", O_CREAT | O_WRONLY,
                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
    printf("open error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  while (blockcount > 0) {
    if ((n = write(fdWrite, buf, blocksize)) == -1) {
      printf("write error with errno = %d\n", errno);
      perror("bar");
      exit(1);
    }
    blockcount--;
  }
  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   $0x3,-0x8(%rbp)
je     11af <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    120b <main+0x9b>
lea    0xe2d(%rip),%rdi
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
mov    0x2db3(%rip),%rax
or     (%rax),%edx
mov    0x2d92(%rip),%rax
or     (%rax),%edx
mov    0x2d81(%rip),%rax
or     (%rax),%edx
lea    0xe0b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1286 <main+0x116>
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xddc(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    128b <main+0x11b>
cmpl   $0x0,-0x28(%rbp)
jle    12eb <main+0x17b>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    12dd <main+0x16d>
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd85(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    128b <main+0x11b>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1274 <main+0xf4>
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    %r14d,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     128f <main+0x10f>
mov    %rax,%r15
mov    0x2e33(%rip),%rax
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2de1(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ddf(%rip),%rax
or     (%rax),%edx
mov    0x2dce(%rip),%rax
or     (%rax),%edx
lea    0xe54(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12a5 <main+0x125>
test   %ebp,%ebp
jle    123a <main+0xba>
mov    %eax,%ebx
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1247 <main+0xc7>
add    $0xffffffff,%ebp
cmp    $0x2,%ebp
jge    1220 <main+0xa0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdea(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    (%rbx),%rsi
lea    0xd82(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd8a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
lea    0xd9e(%rip),%rdi
jmp    1257 <main+0xd7>

LLVM IR (-O0):
@.str = [32 x i8] c"Usage: %s blocksize blockcount\0A\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [47 x i8] c"error allocating blocksize, out of heap memory\00"
@.str.3 = [4 x i8] c"bar\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.4 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.5 = [29 x i8] c"write error with errno = %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 i8*
%9 = alloca i32
%10 = alloca i32
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 %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %9
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %10
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = mul i64 %29, 1
%31 = trunc i64 %30 to i32
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %8
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %19
%38 = load i32, i32* @O_CREAT
%39 = load i32, i32* @O_WRONLY
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IWUSR
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IRGRP
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IROTH
%47 = or i32 %45, %46
%48 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %47)
store i32 %48, i32* %6
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %55
50: ; preds = %37
%51 = load i32, i32* @errno
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %37
br label %56
56: ; preds = %70, %55
%57 = load i32, i32* %10
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %73
59: ; preds = %56
%60 = load i32, i32* %6
%61 = load i8*, i8** %8
%62 = load i32, i32* %9
%63 = call i32 @write(i32 %60, i8* %61, i32 %62)
store i32 %63, i32* %7
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %70
65: ; preds = %59
%66 = load i32, i32* @errno
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %66)
%68 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %59
%71 = load i32, i32* %10
%72 = add nsw i32 %71, -1
store i32 %72, i32* %10
br label %56
73: ; preds = %56
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"Usage: %s blocksize blockcount\0A\00"
@.str.2 = [47 x i8] c"error allocating blocksize, out of heap memory\00"
@.str.3 = [4 x i8] c"bar\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.4 = [28 x i8] c"open error with errno = %d\0A\00"
@errno = global i32 0
@.str.5 = [29 x i8] c"write error with errno = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = tail call i8* @malloc(i32 %11)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_WRONLY
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IRGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IROTH
%30 = or i32 %28, %29
%31 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %23, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %35, label %33
33: ; preds = %20
%34 = icmp sgt i32 %14, 0
br i1 %34, label %43, label %52
35: ; preds = %20
%36 = load i32, i32* @errno
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %43
%41 = add nsw i32 %44, -1
%42 = icmp sgt i32 %44, 1
br i1 %42, label %43, label %52
43: ; preds = %33, %40
%44 = phi i32 [ %41, %40 ], [ %14, %33 ]
%45 = tail call i32 @write(i32 %31, i8* %15, i32 %11)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %40
47: ; preds = %43
%48 = load i32, i32* @errno
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %48)
%50 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%51 = tail call i32 @exit(i32 1)
unreachable
52: ; preds = %40, %33
ret i32 0
}