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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
void test_fopen_file_writing() {
  FILE *f = fopen(".throwaway-test-output", "w");
  if (f == NULL) {
    puts("Can't fopen(..., \"w\"): BROKEN");
  }
  char buf[] = "Hello, world.";
  size_t size = fwrite(buf, sizeof(buf), 1, f);
  if (ferror(f)) {
    puts("Error writing: BROKEN");
  }
  fclose(f);
  puts("Writing files with 'wpath': OK");
}


x86 assembly (clang, -O0):
<test_fopen_file_writing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
lea    0xeb1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1186 <test_fopen_file_writing+0x36>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
mov    0xeaa(%rip),%rax
mov    %rax,-0x16(%rbp)
mov    0xea8(%rip),%eax
mov    %eax,-0xe(%rbp)
mov    0xea2(%rip),%ax
mov    %ax,-0xa(%rbp)
lea    -0x16(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0xe,%esi
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <ferror@plt>
cmp    $0x0,%rax
je     11df <test_fopen_file_writing+0x8f>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fopen_file_writing>:
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rdi
lea    0xeb4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    117c <test_fopen_file_writing+0x2c>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
movabs $0x2e646c726f7720,%rax
mov    %rax,0x6(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,(%rsp)
mov    %rsp,%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <ferror@plt>
test   %rax,%rax
je     11c7 <test_fopen_file_writing+0x77>
lea    0xe83(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe85(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [23 x i8] c".throwaway-test-output\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [30 x i8] c"Can't fopen(..., \22w\22): BROKEN\00"
@__const.test_fopen_file_writing.buf = [14 x i8] c"Hello, world.\00"
@.str.3 = [22 x i8] c"Error writing: BROKEN\00"
@.str.4 = [31 x i8] c"Writing files with 'wpath': OK\00"
define void @test_fopen_file_writing() {
%1 = alloca i32*
%2 = alloca [14 x i8]
%3 = alloca i64
%4 = call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %1
%5 = load i32*, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = bitcast [14 x i8]* %2 to i8*
%11 = getelementptr inbounds [14 x i8], [14 x i8]* %2, i64 0, i64 0
%12 = load i32*, i32** %1
%13 = call i64 @fwrite(i8* %11, i32 14, i32 1, i32* %12)
store i64 %13, i64* %3
%14 = load i32*, i32** %1
%15 = call i64 @ferror(i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %9
%20 = load i32*, i32** %1
%21 = call i32 @fclose(i32* %20)
%22 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c".throwaway-test-output\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [30 x i8] c"Can't fopen(..., \22w\22): BROKEN\00"
@__const.test_fopen_file_writing.buf = [14 x i8] c"Hello, world.\00"
@.str.3 = [22 x i8] c"Error writing: BROKEN\00"
@.str.4 = [31 x i8] c"Writing files with 'wpath': OK\00"
define void @test_fopen_file_writing() {
%1 = alloca [14 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%8 = call i64 @fwrite(i8* %7, i32 14, i32 1, i32* %2)
%9 = tail call i64 @ferror(i32* %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %13
13: ; preds = %11, %6
%14 = tail call i32 @fclose(i32* %2)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
ret void
}