#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
int save_code(char *buffer, char *fname) {
  FILE *fp;
  if ((fp = fopen(fname, "w")) == NULL) {
    puts("Problem with provided output file name:");
    puts(strerror(errno));
    strcpy(fname, "out.c");
    if ((fp = fopen(fname, "w")) == NULL) {
      puts(strerror(errno));
      return 0;
    }
  }
  fputs(buffer, fp);
  fclose(fp);
  printf("File %s has been created.\n", fname);
  return 1;
}


x86 assembly (clang, -O0):
<save_code>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1215 <save_code+0xa5>
lea    0xe5d(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1210 <save_code+0xa0>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1242 <save_code+0xd2>
jmp    1215 <save_code+0xa5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfa(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_code>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe7b(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11df <save_code+0x6f>
lea    0xe66(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e38(%rip),%r12
mov    (%r12),%edi
call   1090 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
lea    0xe6a(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
lea    0xe31(%rip),%rsi
mov    %r15,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1212 <save_code+0xa2>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe37(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r12),%edi
call   1090 <strerror@plt>
mov    %rax,%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1206 <save_code+0x96>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [40 x i8] c"Problem with provided output file name:\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"out.c\00"
@.str.3 = [27 x i8] c"File %s has been created.\0A\00"
define i32 @save_code(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %25
10: ; preds = %2
%11 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @puts(i8* %13)
%15 = load i8*, i8** %5
%16 = call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %6
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %24
20: ; preds = %10
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @puts(i8* %22)
store i32 0, i32* %3
br label %33
24: ; preds = %10
br label %25
25: ; preds = %24, %2
%26 = load i8*, i8** %4
%27 = load i32*, i32** %6
%28 = call i32 @fputs(i8* %26, i32* %27)
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
%31 = load i8*, i8** %5
%32 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %31)
store i32 1, i32* %3
br label %33
33: ; preds = %25, %20
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [40 x i8] c"Problem with provided output file name:\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"out.c\00"
@.str.3 = [27 x i8] c"File %s has been created.\0A\00"
define i32 @save_code(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @puts(i8* dereferenceable(1) %8)
%10 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %5
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @puts(i8* dereferenceable(1) %15)
br label %22
17: ; preds = %5, %2
%18 = phi i32* [ %11, %5 ], [ %3, %2 ]
%19 = tail call i32 @fputs(i8* %0, i32* %18)
%20 = tail call i32 @fclose(i32* %18)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %22
22: ; preds = %17, %13
%23 = phi i32 [ 0, %13 ], [ 1, %17 ]
ret i32 %23
}