#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
char *readFromFile(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *strtok_r(char *, char *, char **);
char *returnBalance(char *fileName) {
  FILE *fp = fopen(fileName, "r");
  char delim[] = ",\n";
  char *str = readFromFile(fp);
  fclose(fp);
  char *save_ptr;
  char *bal = (char *)malloc(2 * sizeof(char));
  bal[0] = '0';
  bal[1] = 0;
  char *tok = strtok_r(str, delim, &save_ptr);
  int cnt = 0;
  do {
    if (cnt == 2) {
      bal = (char *)malloc(((strlen(tok) + 1)) * sizeof(char));
      strcpy(bal, tok);
      bal[strlen(tok)] = 0;
    }
    tok = NULL;
    cnt++;
  } while ((tok = strtok_r(NULL, delim, &save_ptr)) != NULL && cnt < 3);
  if (str != NULL)
    free(str);
  str = NULL;
  return bal;
}


x86 assembly (clang, -O0):
<returnBalance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    0xe5b(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe53(%rip),%al
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <readFromFile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x30,(%rax)
mov    -0x30(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x13(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x2,-0x3c(%rbp)
jne    1252 <returnBalance+0xd2>
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax,%rcx,1)
movq   $0x0,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
lea    -0x13(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x28(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x49(%rbp)
je     1294 <returnBalance+0x114>
cmpl   $0x3,-0x3c(%rbp)
setl   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1203 <returnBalance+0x83>
cmpq   $0x0,-0x20(%rbp)
je     12b3 <returnBalance+0x133>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x30(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<returnBalance>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
movb   $0x0,0xe(%rsp)
movw   $0xa2c,0xc(%rsp)
mov    %rax,%rdi
call   1080 <readFromFile@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0x2,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
movw   $0x30,(%rax)
lea    0xc(%rsp),%rbx
lea    0x10(%rsp),%r12
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <strtok_r@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1070 <strtok_r@plt>
test   %rax,%rax
je     124a <returnBalance+0xca>
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1070 <strtok_r@plt>
test   %rax,%rax
je     124a <returnBalance+0xca>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
movb   $0x0,(%r15,%rax,1)
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1070 <strtok_r@plt>
test   %r14,%r14
je     1257 <returnBalance+0xd7>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@__const.returnBalance.delim = [3 x i8] c",\0A\00"
define i8* @returnBalance(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [3 x i8]
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %3
%12 = bitcast [3 x i8]* %4 to i8*
%13 = load i32*, i32** %3
%14 = call i8* @readFromFile(i32* %13)
store i8* %14, i8** %5
%15 = load i32*, i32** %3
%16 = call i32 @fclose(i32* %15)
%17 = call i64 @malloc(i32 2)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = getelementptr inbounds i8, i8* %19, i64 0
store i8 48, i8* %20
%21 = load i8*, i8** %7
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8 0, i8* %22
%23 = load i8*, i8** %5
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%25 = call i8* @strtok_r(i8* %23, i8* %24, i8** %6)
store i8* %25, i8** %8
store i32 0, i32* %9
br label %26
26: ; preds = %54, %1
%27 = load i32, i32* %9
%28 = icmp eq i32 %27, 2
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = load i8*, i8** %8
%31 = call i64 @strlen(i8* %30)
%32 = add i64 %31, 1
%33 = mul i64 %32, 1
%34 = trunc i64 %33 to i32
%35 = call i64 @malloc(i32 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %7
%37 = load i8*, i8** %7
%38 = load i8*, i8** %8
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %7
%41 = load i8*, i8** %8
%42 = call i64 @strlen(i8* %41)
%43 = getelementptr inbounds i8, i8* %40, i64 %42
store i8 0, i8* %43
br label %44
44: ; preds = %29, %26
store i8* null, i8** %8
%45 = load i32, i32* %9
%46 = add nsw i32 %45, 1
store i32 %46, i32* %9
br label %47
47: ; preds = %44
%48 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%49 = call i8* @strtok_r(i8* null, i8* %48, i8** %6)
store i8* %49, i8** %8
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %9
%53 = icmp slt i32 %52, 3
br label %54
54: ; preds = %51, %47
%55 = phi i1 [ false, %47 ], [ %53, %51 ]
br i1 %55, label %26, label %56
56: ; preds = %54
%57 = load i8*, i8** %5
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i8*, i8** %5
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %59, %56
store i8* null, i8** %5
%63 = load i8*, i8** %7
ret i8* %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@__const.returnBalance.delim = [3 x i8] c",\0A\00"
define i8* @returnBalance(i8* nocapture %0) {
%2 = alloca [3 x i8]
%3 = alloca i8*
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %2, i64 0, i64 0
%6 = tail call i8* @readFromFile(i32* %4)
%7 = tail call i32 @fclose(i32* %4)
%8 = bitcast i8** %3 to i8*
%9 = tail call i64 @malloc(i32 2)
%10 = inttoptr i64 %9 to i8*
store i8 48, i8* %10
%11 = getelementptr inbounds i8, i8* %10, i64 1
store i8 0, i8* %11
%12 = call i8* @strtok_r(i8* %6, i8* %5, i8** %3)
%13 = call i8* @strtok_r(i8* null, i8* %5, i8** %3)
%14 = icmp eq i8* %13, null
br i1 %14, label %28, label %15
15: ; preds = %1
%16 = call i8* @strtok_r(i8* null, i8* %5, i8** %3)
%17 = icmp eq i8* %16, null
br i1 %17, label %28, label %18
18: ; preds = %15
%19 = call i64 @strlen(i8* dereferenceable(1) %16)
%20 = trunc i64 %19 to i32
%21 = add i32 %20, 1
%22 = call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = call i32 @strcpy(i8* %23, i8* %16)
%25 = call i64 @strlen(i8* dereferenceable(1) %16)
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
%27 = call i8* @strtok_r(i8* null, i8* %5, i8** %3)
br label %28
28: ; preds = %18, %15, %1
%29 = phi i8* [ %10, %1 ], [ %10, %15 ], [ %23, %18 ]
%30 = icmp eq i8* %6, null
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = call i32 @free(i8* %6)
br label %33
33: ; preds = %31, %28
ret i8* %29
}