#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ fwrite(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
scalar_t__ strlen(char *);
int write_to_file(char *filename, char *writeval) {
  char *line = NULL;
  size_t len;
  FILE *fp;
  fp = fopen(filename, "w");
  if (!fp)
    return -1;
  fprintf(stdout, "<br><br>--------<pre>Writing %s to file %s:\n\n", writeval,
          filename);
  if (fwrite(writeval, strlen(writeval), 1, fp) < strlen(writeval))
    fprintf(stdout, "Write failed\n");
  else
    fprintf(stdout, "Write succeeded\n");
  fprintf(stdout, "</pre>");
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<write_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1193 <write_to_file+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    124f <write_to_file+0xff>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jge    1211 <write_to_file+0xc1>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1228 <write_to_file+0xd8>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11fa <write_to_file+0xaa>
mov    %rax,%r15
mov    0x2e63(%rip),%r12
mov    (%r12),%edi
lea    0xe7a(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
mov    (%r12),%edi
lea    0xe64(%rip),%rax
lea    0xe6b(%rip),%rsi
cmovl  %rax,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r12),%edi
lea    0xe66(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1200 <write_to_file+0xb0>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.1 = [46 x i8] c"<br><br>--------<pre>Writing %s to file %s:\0A\0A\00"
@.str.2 = [14 x i8] c"Write failed\0A\00"
@.str.3 = [17 x i8] c"Write succeeded\0A\00"
@.str.4 = [7 x i8] c"</pre>\00"
define i32 @write_to_file(i8* %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 %18, label %5
5: ; preds = %2
%6 = load i32, i32* @stdout
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%9 = tail call i64 @fwrite(i8* %1, i64 %8, i32 1, i32* %3)
%10 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%11 = icmp slt i64 %9, %10
%12 = load i32, i32* @stdout
%13 = select i1 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* %13)
%15 = load i32, i32* @stdout
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%17 = tail call i32 @fclose(i32* %3)
br label %18
18: ; preds = %2, %5
%19 = phi i32 [ 0, %5 ], [ -1, %2 ]
ret i32 %19
}