#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 */
struct ts_node {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int ts_save_file(struct ts_node *, int /*<<< orphan*/ *);
int ts_save(struct ts_node *tree, const char *fname) {
  FILE *fp;
  int res;
  if (!(fp = fopen(fname, "wb"))) {
    fprintf(stderr, "ts_save: failed to open file: %s: %s\n", fname,
            strerror(errno));
    return 0;
  }
  res = ts_save_file(tree, fp);
  fclose(fp);
  return res;
}


x86 assembly (clang, -O0):
<ts_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c2 <ts_save+0x72>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <ts_save+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <ts_save_file@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ts_save>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe9c(%rip),%rsi
mov    %r15,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     118d <ts_save+0x3d>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <ts_save_file@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    11ba <ts_save+0x6a>
mov    0x2e4c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe58(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ts_node = type { i32 }
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"ts_save: failed to open file: %s: %s\0A\00"
@errno = global i32 0
define i32 @ts_save(%struct.ts_node* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.ts_node*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store %struct.ts_node* %0, %struct.ts_node** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = icmp ne i32* %9, null
br i1 %10, label %17, label %11
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %5
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %15)
store i32 0, i32* %3
br label %24
17: ; preds = %2
%18 = load %struct.ts_node*, %struct.ts_node** %4
%19 = load i32*, i32** %6
%20 = call i32 @ts_save_file(%struct.ts_node* %18, i32* %19)
store i32 %20, i32* %7
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %7
store i32 %23, i32* %3
br label %24
24: ; preds = %17, %11
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.ts_node = type { i32 }
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [38 x i8] c"ts_save: failed to open file: %s: %s\0A\00"
@errno = global i32 0
define i32 @ts_save(%struct.ts_node* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %8)
br label %13
10: ; preds = %2
%11 = tail call i32 @ts_save_file(%struct.ts_node* %0, i32* %3)
%12 = tail call i32 @fclose(i32* %3)
br label %13
13: ; preds = %10, %5
%14 = phi i32 [ %11, %10 ], [ 0, %5 ]
ret i32 %14
}