#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int ret = 0;
  uint8_t *buf = NULL;
  FILE *fp = fopen("test.bin", "w");
  do {
    if (!fp) {
      printf("Failed to open file\n");
      break;
    }
    if (!(buf = malloc(1024 * 1024))) {
      break;
    }
    ret = fwrite(buf, 1, 1024 * 1024, fp);
    printf("write %d bytes (%d)\n", ret, 1024 * 1024);
  } while (0);
  if (fp)
    fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    0xe8b(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a3 <main+0x53>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f0 <main+0xa0>
mov    $0x100000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11c0 <main+0x70>
jmp    11f0 <main+0xa0>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100000,%edx
call   1070 <fwrite@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x100000,%edx
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
je     1204 <main+0xb4>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe98(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bf <main+0x5f>
mov    %rax,%rbx
mov    $0x100000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b5 <main+0x55>
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x100000,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
mov    $0x100000,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11cb <main+0x6b>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.bin\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [21 x i8] c"Failed to open file\0A\00"
@.str.3 = [21 x i8] c"write %d bytes (%d)\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
store i32* null, i32** %3
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
br label %6
6: ; preds = %0
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %21
11: ; preds = %6
%12 = call i32* @malloc(i32 1048576)
store i32* %12, i32** %3
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11
br label %21
15: ; preds = %11
%16 = load i32*, i32** %3
%17 = load i32*, i32** %4
%18 = call i32 @fwrite(i32* %16, i32 1, i32 1048576, i32* %17)
store i32 %18, i32* %2
%19 = load i32, i32* %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 1048576)
br label %21
21: ; preds = %15, %14, %9
%22 = load i32*, i32** %4
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
br label %27
27: ; preds = %24, %21
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.bin\00"
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [21 x i8] c"write %d bytes (%d)\0A\00"
@str = [20 x i8] c"Failed to open file\00"
define i32 @main() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %9, label %3
3: ; preds = %0
%4 = tail call i32* @malloc(i32 1048576)
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i32 @fwrite(i32* %4, i32 1, i32 1048576, i32* %1)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 1048576)
br label %11
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %13
11: ; preds = %3, %6
%12 = tail call i32 @fclose(i32* %1)
br label %13
13: ; preds = %9, %11
ret i32 0
}