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

/* Variables and functions */
int atoi(char *);
int errno;
int /*<<< orphan*/ exit(int);
int ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fdWrite;
  char *buf;
  int blocksize, blockcount, n;
  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 = fopen("bar", "w")) == NULL) {
    printf("fopen error with errno = %d\n", errno);
    perror("bar");
    exit(1);
  }
  while (blockcount > 0) {
    if ((n = fwrite(buf, 1, blocksize, fdWrite)) == 0) {
      if (ferror(fdWrite)) {
        printf("bar write error with errno = %d\n", ferror(fdWrite));
        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     11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%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    121b <main+0x9b>
lea    0xe1d(%rip),%rdi
lea    0xe1a(%rip),%rsi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe31(%rip),%rdi
lea    0xe2e(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1269 <main+0xe9>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    126e <main+0xee>
cmpl   $0x0,-0x28(%rbp)
jle    12e1 <main+0x161>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jne    12d3 <main+0x153>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
je     12ce <main+0x14e>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
mov    %eax,%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12d3 <main+0x153>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    126e <main+0xee>
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    1252 <main+0xc2>
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     126d <main+0xdd>
mov    %rax,%r15
lea    0xe7a(%rip),%rdi
lea    0xe77(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1283 <main+0xf3>
test   %ebp,%ebp
jle    1245 <main+0xb5>
mov    %rax,%rbx
add    $0x1,%ebp
jmp    1208 <main+0x78>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jle    1245 <main+0xb5>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
test   %eax,%eax
jne    1200 <main+0x70>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
je     1200 <main+0x70>
lea    0xe40(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xda4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdac(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdae(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

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"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [29 x i8] c"fopen error with errno = %d\0A\00"
@errno = global i32 0
@.str.6 = [33 x i8] c"bar 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 i8*
%8 = alloca i32
%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* %8
%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* %9
%28 = load i32, i32* %8
%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** %7
%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 = call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %38, i32** %6
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %45
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %41)
%43 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %37
br label %46
46: ; preds = %65, %45
%47 = load i32, i32* %9
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %68
49: ; preds = %46
%50 = load i8*, i8** %7
%51 = load i32, i32* %8
%52 = load i32*, i32** %6
%53 = call i32 @fwrite(i8* %50, i32 1, i32 %51, i32* %52)
store i32 %53, i32* %10
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %49
%56 = load i32*, i32** %6
%57 = call i32 @ferror(i32* %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %64
59: ; preds = %55
%60 = load i32*, i32** %6
%61 = call i32 @ferror(i32* %60)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %61)
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %55
br label %65
65: ; preds = %64, %49
%66 = load i32, i32* %9
%67 = add nsw i32 %66, -1
store i32 %67, i32* %9
br label %46
68: ; preds = %46
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"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [29 x i8] c"fopen error with errno = %d\0A\00"
@errno = global i32 0
@.str.6 = [33 x i8] c"bar 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 = tail call i32* @fopen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = icmp sgt i32 %14, 0
br i1 %24, label %30, label %43
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 %26)
%28 = tail call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %23, %40
%31 = phi i32 [ %41, %40 ], [ %14, %23 ]
%32 = tail call i32 @fwrite(i8* %15, i32 1, i32 %11, i32* %21)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = tail call i32 @ferror(i32* %21)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i32 %35)
%39 = tail call i32 @exit(i32 1)
unreachable
40: ; preds = %34, %30
%41 = add nsw i32 %31, -1
%42 = icmp sgt i32 %31, 1
br i1 %42, label %30, label %43
43: ; preds = %40, %23
ret i32 0
}