#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 */
scalar_t__ EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/ *);
int strlen(char *);
bool str_to_file(char *filepath, char *data) {
  FILE *file_out = fopen(filepath, "w");
  bool to_return = false;
  if (file_out != NULL) {
    if (fputs(data, file_out) != EOF) {
      to_return = true;
    }
    if (data[strlen(data) - 1] != '\n') {
      if (fputs("\n", file_out) != EOF) {
        to_return = true;
      }
    }
    fclose(file_out);
  }
  return to_return;
}


x86 assembly (clang, -O0):
<str_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f9 <str_to_file+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <fputs@plt>
mov    0x2e5e(%rip),%rcx
cmp    (%rcx),%rax
je     119a <str_to_file+0x5a>
movl   $0x1,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     11f0 <str_to_file+0xb0>
mov    -0x18(%rbp),%rsi
lea    0xe33(%rip),%rdi
call   1050 <fputs@plt>
mov    0x2e0d(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <str_to_file+0xab>
movl   $0x1,-0x1c(%rbp)
jmp    11f0 <str_to_file+0xb0>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xeaf(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11af <str_to_file+0x6f>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    0x2e78(%rip),%r15
xor    %ebp,%ebp
cmp    (%r15),%rax
setne  %bpl
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r14,1)
je     11a5 <str_to_file+0x65>
lea    0xe70(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fputs@plt>
cmp    (%r15),%rax
mov    $0x1,%eax
cmovne %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11b1 <str_to_file+0x71>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@EOF = global i64 0
@.str.1 = [2 x i8] c"\0A\00"
define i32 @str_to_file(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %24, label %5
5: ; preds = %2
%6 = tail call i64 @fputs(i8* %1, i32* %3)
%7 = load i64, i64* @EOF
%8 = icmp ne i64 %6, %7
%9 = zext i1 %8 to i32
%10 = tail call i32 @strlen(i8* %1)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %21, label %16
16: ; preds = %5
%17 = tail call i64 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%18 = load i64, i64* @EOF
%19 = icmp eq i64 %17, %18
%20 = select i1 %19, i32 %9, i32 1
br label %21
21: ; preds = %16, %5
%22 = phi i32 [ %9, %5 ], [ %20, %16 ]
%23 = tail call i32 @fclose(i32* %3)
br label %24
24: ; preds = %21, %2
%25 = phi i32 [ %22, %21 ], [ 0, %2 ]
ret i32 %25
}