#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 *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
char *strdup(char *);
int strlen(char *);
char *format(char *string) {
  char *formatted = ((char *)malloc(sizeof(char) * strlen(string) + 1));
  formatted = strdup(string);
  FILE *writeto = NULL;
  writeto = fopen("../../file.txt", "w");
  if (writeto != NULL) {
    fprintf(writeto, "%s", string);
    fclose(writeto);
  } else {
    perror("fopen");
  }
  free(formatted);
  return string;
}


x86 assembly (clang, -O0):
<format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe3a(%rip),%rdi
lea    0xe42(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1203 <format+0x83>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    120f <format+0x8f>
lea    0xe0a(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %r15,%rdi
call   10a0 <strdup@plt>
mov    %rax,%r14
lea    0xe59(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11df <format+0x5f>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2b(%rip),%rdi
call   1090 <perror@plt>
jmp    11ce <format+0x4e>

LLVM IR (-O0):
@.str = [15 x i8] c"../../file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c"fopen\00"
define i8* @format(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = sext i32 %6 to i64
%8 = mul i64 1, %7
%9 = add i64 %8, 1
%10 = trunc i64 %9 to i32
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %3
%13 = load i8*, i8** %2
%14 = call i8* @strdup(i8* %13)
store i8* %14, i8** %3
store i32* null, i32** %4
%15 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %4
%16 = load i32*, i32** %4
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %24
18: ; preds = %1
%19 = load i32*, i32** %4
%20 = load i8*, i8** %2
%21 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i32*, i32** %4
%23 = call i32 @fclose(i32* %22)
br label %26
24: ; preds = %1
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i8*, i8** %3
%28 = call i32 @free(i8* %27)
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@.str = [15 x i8] c"../../file.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [6 x i8] c"fopen\00"
define i8* @format(i8* returned %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add i32 %2, 1
%4 = tail call i64 @malloc(i32 %3)
%5 = tail call i8* @strdup(i8* %0)
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = tail call i32 @fputs(i8* %0, i32* %6)
%10 = tail call i32 @fclose(i32* %6)
br label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i32 @free(i8* %5)
ret i8* %0
}