#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
size_t fwrite(char const *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
size_t strlen(char const *);
int WriteString(const char *val, const char *fileName, FILE *fp) {
  size_t len;
  len = strlen(val);
  if (fwrite(val, 1, len, fp) != len) {
    fclose(fp);
    fprintf(stderr, "Error writing to file, '%s'.\n", fileName);
    return (0);
  }
  return (1);
}


x86 assembly (clang, -O0):
<WriteString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
cmp    -0x28(%rbp),%rax
je     11af <WriteString+0x6f>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <WriteString+0x76>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteString>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%rbp
mov    $0x1,%r12d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   1060 <fwrite@plt>
cmp    %rbp,%rax
je     119a <WriteString+0x5a>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Error writing to file, '%s'.\0A\00"
define i32 @WriteString(i8* %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%9 = load i8*, i8** %5
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %8
%11 = load i8*, i8** %5
%12 = load i64, i64* %8
%13 = load i32*, i32** %7
%14 = call i64 @fwrite(i8* %11, i32 1, i64 %12, i32* %13)
%15 = load i64, i64* %8
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %23
17: ; preds = %3
%18 = load i32*, i32** %7
%19 = call i32 @fclose(i32* %18)
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %6
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 0, i32* %4
br label %24
23: ; preds = %3
store i32 1, i32* %4
br label %24
24: ; preds = %23, %17
%25 = load i32, i32* %4
ret i32 %25
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Error writing to file, '%s'.\0A\00"
define i32 @WriteString(i8* nocapture %0, i8* %1, i32* nocapture %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @fwrite(i8* %0, i32 1, i64 %4, i32* %2)
%6 = icmp eq i64 %5, %4
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = tail call i32 @fclose(i32* %2)
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %11
11: ; preds = %3, %7
%12 = phi i32 [ 0, %7 ], [ 1, %3 ]
ret i32 %12
}