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

/* Variables and functions */
char EOF;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getFile(char *);
char getWordTotal(char *);
char *initTriangleSeq(int);
char *readWords(int /*<<< orphan*/ *);
scalar_t__ validTriangleNum(char, char *);
uint32_t countTriangleNumbers(void) {
  FILE *file = getFile("../res/coded_triangle_numbers");
  char *words = readWords(file);
  uint32_t *tri = initTriangleSeq(50);
  uint32_t count = 0;
  for (int i = 0; words[i] != EOF; i++) {
    if (words[i] == '\0')
      continue;
    uint32_t total = getWordTotal(words + i);
    count += validTriangleNum(total, tri + 1);
    while (*(words + i) != '\0')
      i++;
  }
  free(words);
  free(tri);
  return count;
}


x86 assembly (clang, -O0):
<countTriangleNumbers>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe91(%rip),%rdi
call   1040 <getFile@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <readWords@plt>
mov    %rax,-0x10(%rbp)
mov    $0x32,%edi
call   1080 <initTriangleSeq@plt>
mov    %rax,-0x18(%rbp)
movb   $0x0,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    0x2e37(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1240 <countTriangleNumbers+0xe0>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11d6 <countTriangleNumbers+0x76>
jmp    1232 <countTriangleNumbers+0xd2>
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
call   1060 <getWordTotal@plt>
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
movsbl %al,%edi
call   1050 <validTriangleNum@plt>
mov    %rax,%rcx
movsbq -0x19(%rbp),%rax
add    %rcx,%rax
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     122d <countTriangleNumbers+0xcd>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    120a <countTriangleNumbers+0xaa>
jmp    1232 <countTriangleNumbers+0xd2>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119e <countTriangleNumbers+0x3e>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movsbl -0x19(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<countTriangleNumbers>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe8e(%rip),%rdi
call   1040 <getFile@plt>
mov    %rax,%rdi
call   1070 <readWords@plt>
mov    %rax,%rbx
mov    $0x32,%edi
call   1080 <initTriangleSeq@plt>
mov    %rax,%r14
mov    (%rbx),%cl
mov    0x2e50(%rip),%r13
mov    0x0(%r13),%al
xor    %ebp,%ebp
mov    $0x0,%r15d
cmp    %al,%cl
jne    11ca <countTriangleNumbers+0x6a>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%r12
add    $0x1,%r12
mov    %rbx,%rdi
xor    %r15d,%r15d
jmp    11f7 <countTriangleNumbers+0x97>
nopl   0x0(%rax)
movslq %ebp,%rcx
lea    0x1(%rcx),%rbp
lea    (%rbx,%rcx,1),%rdi
add    $0x1,%rdi
mov    0x1(%rbx,%rcx,1),%cl
cmp    %al,%cl
je     11a8 <countTriangleNumbers+0x48>
test   %cl,%cl
je     11e0 <countTriangleNumbers+0x80>
call   1060 <getWordTotal@plt>
movsbl %al,%edi
mov    %r12,%rsi
call   1050 <validTriangleNum@plt>
movslq %ebp,%rcx
lea    -0x1(%rcx),%ebp
add    %rbx,%rcx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%ebp
cmpb   $0x0,(%rcx)
lea    0x1(%rcx),%rcx
jne    1220 <countTriangleNumbers+0xc0>
add    %al,%r15b
mov    0x0(%r13),%al
jmp    11e0 <countTriangleNumbers+0x80>

LLVM IR (-O0):
@.str = [30 x i8] c"../res/coded_triangle_numbers\00"
@EOF = global i8 0
define signext i8 @countTriangleNumbers() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
%6 = alloca i8
%7 = call i32* @getFile(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %1
%8 = load i32*, i32** %1
%9 = call i8* @readWords(i32* %8)
store i8* %9, i8** %2
%10 = call i8* @initTriangleSeq(i32 50)
store i8* %10, i8** %3
store i8 0, i8* %4
store i32 0, i32* %5
br label %11
11: ; preds = %56, %0
%12 = load i8*, i8** %2
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = load i8, i8* @EOF
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %17, %19
br i1 %20, label %21, label %59
21: ; preds = %11
%22 = load i8*, i8** %2
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %21
br label %56
30: ; preds = %21
%31 = load i8*, i8** %2
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = call signext i8 @getWordTotal(i8* %34)
store i8 %35, i8* %6
%36 = load i8, i8* %6
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = call i64 @validTriangleNum(i8 signext %36, i8* %38)
%40 = load i8, i8* %4
%41 = sext i8 %40 to i64
%42 = add nsw i64 %41, %39
%43 = trunc i64 %42 to i8
store i8 %43, i8* %4
br label %44
44: ; preds = %52, %30
%45 = load i8*, i8** %2
%46 = load i32, i32* %5
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %44
%53 = load i32, i32* %5
%54 = add nsw i32 %53, 1
store i32 %54, i32* %5
br label %44
55: ; preds = %44
br label %56
56: ; preds = %55, %29
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %11
59: ; preds = %11
%60 = load i8*, i8** %2
%61 = call i32 @free(i8* %60)
%62 = load i8*, i8** %3
%63 = call i32 @free(i8* %62)
%64 = load i8, i8* %4
ret i8 %64
}

LLVM IR (-O2):
@.str = [30 x i8] c"../res/coded_triangle_numbers\00"
@EOF = global i8 0
define signext i8 @countTriangleNumbers() {
%1 = tail call i32* @getFile(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%2 = tail call i8* @readWords(i32* %1)
%3 = tail call i8* @initTriangleSeq(i32 50)
%4 = load i8, i8* %2
%5 = load i8, i8* @EOF
%6 = icmp eq i8 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = getelementptr inbounds i8, i8* %3, i64 1
br label %13
9: ; preds = %35, %0
%10 = phi i8 [ 0, %0 ], [ %38, %35 ]
%11 = tail call i32 @free(i8* %2)
%12 = tail call i32 @free(i8* %3)
ret i8 %10
13: ; preds = %7, %35
%14 = phi i8 [ %5, %7 ], [ %36, %35 ]
%15 = phi i8 [ %4, %7 ], [ %42, %35 ]
%16 = phi i8* [ %2, %7 ], [ %41, %35 ]
%17 = phi i8 [ 0, %7 ], [ %38, %35 ]
%18 = phi i32 [ 0, %7 ], [ %39, %35 ]
%19 = icmp eq i8 %15, 0
br i1 %19, label %35, label %20
20: ; preds = %13
%21 = tail call signext i8 @getWordTotal(i8* %16)
%22 = tail call i64 @validTriangleNum(i8 signext %21, i8* %8)
%23 = trunc i64 %22 to i8
%24 = sext i32 %18 to i64
br label %25
25: ; preds = %25, %20
%26 = phi i64 [ %30, %25 ], [ %24, %20 ]
%27 = getelementptr inbounds i8, i8* %2, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
%30 = add i64 %26, 1
br i1 %29, label %31, label %25
31: ; preds = %25
%32 = add i8 %17, %23
%33 = trunc i64 %26 to i32
%34 = load i8, i8* @EOF
br label %35
35: ; preds = %31, %13
%36 = phi i8 [ %14, %13 ], [ %34, %31 ]
%37 = phi i32 [ %18, %13 ], [ %33, %31 ]
%38 = phi i8 [ %17, %13 ], [ %32, %31 ]
%39 = add nsw i32 %37, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %2, i64 %40
%42 = load i8, i8* %41
%43 = icmp eq i8 %42, %36
br i1 %43, label %9, label %13
}