#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ fs_mv(char *, char *);
int /*<<< orphan*/ fs_rm(char *);
int /*<<< orphan*/ *stderr;
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strtok(char *, char *);
int beargit_add(const char *filename) {
  FILE *findex = fopen(".beargit/.index", "r");
  FILE *fnewindex = fopen(".beargit/.newindex", "w");
  char line[512];
  while (fgets(line, sizeof(line), findex)) {
    strtok(line, "\n");
    if (strcmp(line, filename) == 0) {
      fprintf(stderr, "ERROR: File %s already added\n", filename);
      fclose(findex);
      fclose(fnewindex);
      fs_rm(".beargit/.newindex");
      return 3;
    }
    fprintf(fnewindex, "%s\n", line);
  }
  fprintf(fnewindex, "%s\n", filename);
  fclose(findex);
  fclose(fnewindex);
  fs_mv(".beargit/.newindex", ".beargit/.index");
  return 0;
}


x86 assembly (clang, -O0):
<beargit_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
lea    0xe65(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1269 <beargit_add+0xe9>
lea    -0x220(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1090 <strtok@plt>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    124d <beargit_add+0xcd>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe09(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdd6(%rip),%rdi
call   10a0 <fs_rm@plt>
movl   $0x3,-0x4(%rbp)
jmp    12a9 <beargit_add+0x129>
mov    -0x20(%rbp),%rdi
lea    -0x220(%rbp),%rdx
lea    0xde6(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11bd <beargit_add+0x3d>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdcd(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd7c(%rip),%rdi
lea    0xd63(%rip),%rsi
call   1030 <fs_mv@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<beargit_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
lea    0xe65(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r13
lea    0xe61(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     122e <beargit_add+0xae>
lea    0xe67(%rip),%r12
mov    %rsp,%rbx
lea    0xe5f(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strtok@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1267 <beargit_add+0xe7>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r13,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11f0 <beargit_add+0x70>
lea    0xe10(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    0xdbb(%rip),%rdi
lea    0xda2(%rip),%rsi
call   1030 <fs_mv@plt>
xor    %eax,%eax
jmp    12a1 <beargit_add+0x121>
mov    0x2d72(%rip),%rax
mov    (%rax),%rdi
lea    0xdaf(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
lea    0xd7b(%rip),%rdi
call   10a0 <fs_rm@plt>
mov    $0x3,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c".beargit/.index\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c".beargit/.newindex\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"\0A\00"
@stderr = global i32* null
@.str.5 = [30 x i8] c"ERROR: File %s already added\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @beargit_add(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [512 x i8]
store i8* %0, i8** %3
%7 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %4
%8 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %8, i32** %5
br label %9
9: ; preds = %30, %1
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%11 = load i32*, i32** %4
%12 = call i64 @fgets(i8* %10, i32 512, i32* %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %34
14: ; preds = %9
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = call i32 @strtok(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%18 = load i8*, i8** %3
%19 = call i64 @strcmp(i8* %17, i8* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %14
%22 = load i32*, i32** @stderr
%23 = load i8*, i8** %3
%24 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %23)
%25 = load i32*, i32** %4
%26 = call i32 @fclose(i32* %25)
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = call i32 @fs_rm(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %2
br label %43
30: ; preds = %14
%31 = load i32*, i32** %5
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%33 = call i32 @fprintf(i32* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %32)
br label %9
34: ; preds = %9
%35 = load i32*, i32** %5
%36 = load i8*, i8** %3
%37 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %36)
%38 = load i32*, i32** %4
%39 = call i32 @fclose(i32* %38)
%40 = load i32*, i32** %5
%41 = call i32 @fclose(i32* %40)
%42 = call i32 @fs_mv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %43
43: ; preds = %34, %21
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [16 x i8] c".beargit/.index\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c".beargit/.newindex\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [2 x i8] c"\0A\00"
@stderr = global i32* null
@.str.5 = [30 x i8] c"ERROR: File %s already added\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @beargit_add(i8* %0) {
%2 = alloca [512 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = call i64 @fgets(i8* %5, i32 512, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %1, %18
%9 = call i32 @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%10 = call i64 @strcmp(i8* %5, i8* %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8
%13 = load i32*, i32** @stderr
%14 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%15 = call i32 @fclose(i32* %3)
%16 = call i32 @fclose(i32* %4)
%17 = call i32 @fs_rm(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %27
18: ; preds = %8
%19 = call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %5)
%20 = call i64 @fgets(i8* %5, i32 512, i32* %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %8
22: ; preds = %18, %1
%23 = call i32 @fprintf(i32* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %0)
%24 = call i32 @fclose(i32* %3)
%25 = call i32 @fclose(i32* %4)
%26 = call i32 @fs_mv(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %22, %12
%28 = phi i32 [ 3, %12 ], [ 0, %22 ]
ret i32 %28
}