#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*/ erralloc();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int /*<<< orphan*/,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strtok(char *, char *);
int writeFile(char *filename, char *buffer) {
  char ext[] = ".cfr";
  strtok(filename, ".");
  strcat(filename, ext);
  FILE *fp = fopen(filename, "w");
  if (!fp)
    erralloc();
  fwrite(buffer, sizeof(char), strlen(buffer), fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe7a(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xe75(%rip),%al
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1070 <strtok@plt>
mov    -0x8(%rbp),%rdi
lea    -0x15(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d5 <writeFile+0x65>
mov    $0x0,%al
call   1030 <erralloc@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,0x4(%rsp)
movl   $0x7266632e,(%rsp)
lea    0xe78(%rip),%rsi
call   1070 <strtok@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
lea    0xe63(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11bb <writeFile+0x4b>
xor    %eax,%eax
call   1030 <erralloc@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.writeFile.ext = [5 x i8] c".cfr\00"
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @writeFile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [5 x i8]
%6 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = bitcast [5 x i8]* %5 to i8*
%8 = load i8*, i8** %3
%9 = call i32 @strtok(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %3
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%12 = call i32 @strcat(i8* %10, i8* %11)
%13 = load i8*, i8** %3
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp ne i32* %15, null
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 (...) @erralloc()
br label %19
19: ; preds = %17, %2
%20 = load i8*, i8** %4
%21 = load i8*, i8** %4
%22 = call i32 @strlen(i8* %21)
%23 = load i32*, i32** %6
%24 = call i32 @fwrite(i8* %20, i32 1, i32 %22, i32* %23)
%25 = load i32*, i32** %6
%26 = call i32 @fclose(i32* %25)
ret i32 0
}

LLVM IR (-O2):
@__const.writeFile.ext = [5 x i8] c".cfr\00"
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @writeFile(i8* %0, i8* %1) {
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = call i32 @strcat(i8* %0, i8* %4)
%7 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 (...) @erralloc()
br label %11
11: ; preds = %9, %2
%12 = call i32 @strlen(i8* %1)
%13 = call i32 @fwrite(i8* %1, i32 1, i32 %12, i32* %7)
%14 = call i32 @fclose(i32* %7)
ret i32 0
}