#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*/ debug(char *, char const *, char const *);
int /*<<< orphan*/ die(char *, ...);
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fwrite(char const *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char const *);
void write_string_to_file(const char *filepath, const char *buf) {
  debug("write_string_to_file %s %s", filepath, buf);
  FILE *f = fopen(filepath, "w");
  if (f == NULL)
    die("fopen %s failed", filepath);
  if (fwrite(buf, strlen(buf), 1, f) != 1)
    die("fwrite failed");
  if (fflush(f) != 0)
    die("fflush failed");
  if (fclose(f) != 0)
    die("fclose failed");
}


x86 assembly (clang, -O0):
<write_string_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe71(%rip),%rdi
call   1050 <debug@plt>
mov    -0x8(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c5 <write_string_to_file+0x55>
mov    -0x8(%rbp),%rsi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     1201 <write_string_to_file+0x91>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
cmp    $0x0,%rax
je     1222 <write_string_to_file+0xb2>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1243 <write_string_to_file+0xd3>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1080 <die@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string_to_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe7e(%rip),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1050 <debug@plt>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
jne    11b5 <write_string_to_file+0x45>
lea    0xe72(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <die@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     11e2 <write_string_to_file+0x72>
lea    0xe52(%rip),%rdi
xor    %eax,%eax
call   1080 <die@plt>
mov    %r14,%rdi
call   1060 <fflush@plt>
test   %rax,%rax
je     11fd <write_string_to_file+0x8d>
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1080 <die@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     121d <write_string_to_file+0xad>
lea    0xe38(%rip),%rdi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1080 <die@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"write_string_to_file %s %s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [16 x i8] c"fopen %s failed\00"
@.str.3 = [14 x i8] c"fwrite failed\00"
@.str.4 = [14 x i8] c"fflush failed\00"
@.str.5 = [14 x i8] c"fclose failed\00"
define void @write_string_to_file(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = load i8*, i8** %4
%8 = call i32 @debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %7)
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %2
%17 = load i8*, i8** %4
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = load i32*, i32** %5
%21 = call i32 @fwrite(i8* %17, i32 %19, i32 1, i32* %20)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
%26 = load i32*, i32** %5
%27 = call i64 @fflush(i32* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = load i32*, i32** %5
%33 = call i64 @fclose(i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"write_string_to_file %s %s\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [16 x i8] c"fopen %s failed\00"
@.str.3 = [14 x i8] c"fwrite failed\00"
@.str.4 = [14 x i8] c"fflush failed\00"
@.str.5 = [14 x i8] c"fclose failed\00"
define void @write_string_to_file(i8* %0, i8* %1) {
%3 = tail call i32 @debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %2
%9 = tail call i32 @strlen(i8* %1)
%10 = tail call i32 @fwrite(i8* %1, i32 %9, i32 1, i32* %4)
%11 = icmp eq i32 %10, 1
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = tail call i64 @fflush(i32* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = tail call i64 @fclose(i32* %4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
ret void
}