#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*/ assert(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int reference_new(char *filename) {
  FILE *f;
  char *home;
  char ref_name[2048];
  int ref;
  assert(filename);
  home = getenv("HOME");
  snprintf(ref_name, 2048, "%s/%s", home, filename);
  f = fopen(ref_name, "r");
  if (!f) {
    f = fopen(ref_name, "w");
    if (!f)
      return 0;
    fprintf(f, "1\n");
    fclose(f);
    return 1;
  }
  fscanf(f, "%d", &ref);
  fclose(f);
  ++ref;
  f = fopen(ref_name, "w");
  if (f) {
    fprintf(f, "%d\n", ref);
    fclose(f);
  }
  return ref;
}


x86 assembly (clang, -O0):
<reference_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <assert@plt>
lea    0xe71(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x800,%esi
lea    0xe52(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122f <reference_new+0xbf>
lea    -0x820(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1208 <reference_new+0x98>
movl   $0x0,-0x4(%rbp)
jmp    12aa <reference_new+0x13a>
mov    -0x18(%rbp),%rdi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    12aa <reference_new+0x13a>
mov    -0x18(%rbp),%rdi
lea    0xdd8(%rip),%rsi
lea    -0x824(%rbp),%rdx
call   1060 <fscanf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
lea    -0x820(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a1 <reference_new+0x131>
mov    -0x18(%rbp),%rdi
mov    -0x824(%rbp),%edx
lea    0xd84(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x824(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reference_new>:
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    %rdi,%rbx
call   1070 <assert@plt>
lea    0xe67(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe60(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x800,%esi
mov    %rax,%rcx
mov    %rbx,%r8
call   1050 <snprintf@plt>
lea    0xe47(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1231 <reference_new+0xb1>
mov    %rax,%rbx
lea    0xe37(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1060 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
addl   $0x1,0xc(%rsp)
lea    0xe11(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     122b <reference_new+0xab>
mov    %rax,%rbx
mov    0xc(%rsp),%edx
lea    0xdfc(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%eax
jmp    1274 <reference_new+0xf4>
lea    0xdd5(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1272 <reference_new+0xf2>
mov    %rax,%rbx
lea    0xdbe(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    1274 <reference_new+0xf4>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"1\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @reference_new(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca [2048 x i8]
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @assert(i8* %8)
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %5
%11 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %5
%13 = load i8*, i8** %3
%14 = call i32 @snprintf(i8* %11, i32 2048, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %13)
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %4
%17 = load i32*, i32** %4
%18 = icmp ne i32* %17, null
br i1 %18, label %30, label %19
19: ; preds = %1
%20 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %4
%22 = load i32*, i32** %4
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i32 0, i32* %2
br label %49
25: ; preds = %19
%26 = load i32*, i32** %4
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%28 = load i32*, i32** %4
%29 = call i32 @fclose(i32* %28)
store i32 1, i32* %2
br label %49
30: ; preds = %1
%31 = load i32*, i32** %4
%32 = call i32 @fscanf(i32* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %7)
%33 = load i32*, i32** %4
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
%37 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %38, i32** %4
%39 = load i32*, i32** %4
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %47
41: ; preds = %30
%42 = load i32*, i32** %4
%43 = load i32, i32* %7
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %43)
%45 = load i32*, i32** %4
%46 = call i32 @fclose(i32* %45)
br label %47
47: ; preds = %41, %30
%48 = load i32, i32* %7
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %25, %24
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [3 x i8] c"1\0A\00"
@.str.5 = [3 x i8] c"%d\00"
@.str.6 = [4 x i8] c"%d\0A\00"
define i32 @reference_new(i8* %0) {
%2 = alloca [2048 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 @assert(i8* %0)
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @snprintf(i8* %4, i32 2048, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %0)
%9 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %30, label %14
14: ; preds = %11
%15 = call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i64 2, i64 1, i32* %12)
%16 = call i32 @fclose(i32* %12)
br label %30
17: ; preds = %1
%18 = call i32 @fscanf(i32* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %3)
%19 = call i32 @fclose(i32* %9)
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
store i32 %21, i32* %3
%22 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %28, label %24
24: ; preds = %17
%25 = load i32, i32* %3
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %25)
%27 = call i32 @fclose(i32* %22)
br label %28
28: ; preds = %24, %17
%29 = load i32, i32* %3
br label %30
30: ; preds = %11, %28, %14
%31 = phi i32 [ %29, %28 ], [ 1, %14 ], [ 0, %11 ]
ret i32 %31
}