#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 *);
scalar_t__ strcmp(char *, char const *);
int /*<<< orphan*/ strtok(char *, char *);
int get_branch_number(const char *branch_name) {
  FILE *fbranches = fopen(".beargit/.branches", "r");
  int branch_index = -1;
  int counter = 0;
  char line[512];
  while (fgets(line, sizeof(line), fbranches)) {
    strtok(line, "\n");
    if (strcmp(line, branch_name) == 0) {
      branch_index = counter;
    }
    counter++;
  }
  fclose(fbranches);
  return branch_index;
}


x86 assembly (clang, -O0):
<get_branch_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xea6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11e4 <get_branch_number+0x94>
lea    -0x220(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1070 <strtok@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11d6 <get_branch_number+0x86>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1184 <get_branch_number+0x34>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_branch_number>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%r14
lea    0xe95(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     11d4 <get_branch_number+0x84>
xor    %ebx,%ebx
lea    0xe78(%rip),%r15
mov    %rsp,%r13
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <strtok@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
cmove  %ebx,%ebp
add    $0x1,%ebx
mov    %r13,%rdi
mov    $0x200,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    11a0 <get_branch_number+0x50>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c".beargit/.branches\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @get_branch_number(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
store i8* %0, i8** %2
%7 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 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** %3
store i32 -1, i32* %4
store i32 0, i32* %5
br label %8
8: ; preds = %22, %1
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%10 = load i32*, i32** %3
%11 = call i64 @fgets(i8* %9, i32 512, i32* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %8
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%15 = call i32 @strtok(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%17 = load i8*, i8** %2
%18 = call i64 @strcmp(i8* %16, i8* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = load i32, i32* %5
store i32 %21, i32* %4
br label %22
22: ; preds = %20, %13
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %8
25: ; preds = %8
%26 = load i32*, i32** %3
%27 = call i32 @fclose(i32* %26)
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [19 x i8] c".beargit/.branches\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @get_branch_number(i8* %0) {
%2 = alloca [512 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%5 = call i64 @fgets(i8* %4, i32 512, i32* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %17, label %7
7: ; preds = %1, %7
%8 = phi i32 [ %14, %7 ], [ 0, %1 ]
%9 = phi i32 [ %13, %7 ], [ -1, %1 ]
%10 = call i32 @strtok(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = call i64 @strcmp(i8* %4, i8* %0)
%12 = icmp eq i64 %11, 0
%13 = select i1 %12, i32 %8, i32 %9
%14 = add nuw nsw i32 %8, 1
%15 = call i64 @fgets(i8* %4, i32 512, i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %7
17: ; preds = %7, %1
%18 = phi i32 [ -1, %1 ], [ %13, %7 ]
%19 = call i32 @fclose(i32* %3)
ret i32 %18
}