#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 countDigits(unsigned long long);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned long long);
bool save(unsigned long long in) {
  FILE *file = fopen("output.txt", "ar+");
  if (file == NULL) {
    puts("fopen ERROR");
    return false;
  }
  char *buff;
  buff = malloc(countDigits(in) + 1);
  sprintf(buff, "%llu", in);
  buff[countDigits(in)] = (char)10;
  int savedBytes = fwrite(buff, sizeof(char), countDigits(in) + 1, file);
  if (savedBytes <= 0) {
    puts("Write error");
    return false;
  }
  if (fclose(file)) {
    puts("Close error!");
    return false;
  }
  free(buff);
  return true;
}


x86 assembly (clang, -O0):
<save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6d(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c6 <save+0x46>
lea    0xe5a(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1297 <save+0x117>
mov    -0x10(%rbp),%rdi
call   1060 <countDigits@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe2f(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <countDigits@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <countDigits@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
add    $0x1,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     125c <save+0xdc>
lea    0xdd5(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1297 <save+0x117>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     1287 <save+0x107>
lea    0xdb6(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    1297 <save+0x117>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
lea    0xe71(%rip),%rdi
lea    0xe75(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     120c <save+0x8c>
mov    %rax,%r15
mov    %rbx,%rdi
call   1060 <countDigits@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe5e(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1060 <countDigits@plt>
cltq
movb   $0xa,(%r14,%rax,1)
mov    %rbx,%rdi
call   1060 <countDigits@plt>
lea    0x1(%rax),%edx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rcx
call   10a0 <fwrite@plt>
test   %eax,%eax
jle    1215 <save+0x95>
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
je     1229 <save+0xa9>
lea    0xe22(%rip),%rdi
jmp    121c <save+0x9c>
lea    0xdfc(%rip),%rdi
jmp    121c <save+0x9c>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    1223 <save+0xa3>

LLVM IR (-O0):
@.str = [11 x i8] c"output.txt\00"
@.str.1 = [4 x i8] c"ar+\00"
@.str.2 = [12 x i8] c"fopen ERROR\00"
@.str.3 = [5 x i8] c"%llu\00"
@.str.4 = [12 x i8] c"Write error\00"
@.str.5 = [13 x i8] c"Close error!\00"
define i32 @save(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i64 %0, i64* %3
%7 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %4
%8 = load i32*, i32** %4
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %44
12: ; preds = %1
%13 = load i64, i64* %3
%14 = call i32 @countDigits(i64 %13)
%15 = add nsw i32 %14, 1
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = load i64, i64* %3
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %18)
%20 = load i8*, i8** %5
%21 = load i64, i64* %3
%22 = call i32 @countDigits(i64 %21)
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 10, i8* %24
%25 = load i8*, i8** %5
%26 = load i64, i64* %3
%27 = call i32 @countDigits(i64 %26)
%28 = add nsw i32 %27, 1
%29 = load i32*, i32** %4
%30 = call i32 @fwrite(i8* %25, i32 1, i32 %28, i32* %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp sle i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %12
%34 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %2
br label %44
35: ; preds = %12
%36 = load i32*, i32** %4
%37 = call i64 @fclose(i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %2
br label %44
41: ; preds = %35
%42 = load i8*, i8** %5
%43 = call i32 @free(i8* %42)
store i32 1, i32* %2
br label %44
44: ; preds = %41, %39, %33, %10
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@.str = [11 x i8] c"output.txt\00"
@.str.1 = [4 x i8] c"ar+\00"
@.str.2 = [12 x i8] c"fopen ERROR\00"
@.str.3 = [5 x i8] c"%llu\00"
@.str.4 = [12 x i8] c"Write error\00"
@.str.5 = [13 x i8] c"Close error!\00"
define i32 @save(i64 %0) {
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %27
6: ; preds = %1
%7 = tail call i32 @countDigits(i64 %0)
%8 = add nsw i32 %7, 1
%9 = tail call i8* @malloc(i32 %8)
%10 = tail call i32 @sprintf(i8* dereferenceable(1) %9, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %0)
%11 = tail call i32 @countDigits(i64 %0)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8 10, i8* %13
%14 = tail call i32 @countDigits(i64 %0)
%15 = add nsw i32 %14, 1
%16 = tail call i32 @fwrite(i8* %9, i32 1, i32 %15, i32* %2)
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %20
18: ; preds = %6
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %27
20: ; preds = %6
%21 = tail call i64 @fclose(i32* %2)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = tail call i32 @free(i8* %9)
br label %27
27: ; preds = %18, %23, %25, %4
%28 = phi i32 [ 0, %4 ], [ 0, %18 ], [ 0, %23 ], [ 1, %25 ]
ret i32 %28
}