#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 ssize_t;

/* Variables and functions */
int read(int, char *, int);
size_t case_get_file_len(int f) {
  size_t total = 0;
  ssize_t result;
  char buf[500];
  do {
    result = read(f, buf, 500);
    if (result == -1)
      return 0;
    total += result;
  } while (result > 0);
  return total;
}


x86 assembly (clang, -O0):
<case_get_file_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x1f4,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1154 <case_get_file_len+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1172 <case_get_file_len+0x62>
movslq -0x1c(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1126 <case_get_file_len+0x16>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<case_get_file_len>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %edi,%ebp
xor    %ebx,%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1f4,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1150 <case_get_file_len+0x40>
movslq %eax,%rcx
add    %rcx,%rbx
test   %eax,%eax
jg     1130 <case_get_file_len+0x20>
jmp    1152 <case_get_file_len+0x42>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @case_get_file_len(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca [500 x i8]
store i32 %0, i32* %3
store i64 0, i64* %4
br label %7
7: ; preds = %19, %1
%8 = load i32, i32* %3
%9 = getelementptr inbounds [500 x i8], [500 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 500)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %7
store i64 0, i64* %2
br label %24
14: ; preds = %7
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = load i64, i64* %4
%18 = add i64 %17, %16
store i64 %18, i64* %4
br label %19
19: ; preds = %14
%20 = load i32, i32* %5
%21 = icmp sgt i32 %20, 0
br i1 %21, label %7, label %22
22: ; preds = %19
%23 = load i64, i64* %4
store i64 %23, i64* %2
br label %24
24: ; preds = %22, %13
%25 = load i64, i64* %2
ret i64 %25
}

LLVM IR (-O2):
define i64 @case_get_file_len(i32 %0) {
%2 = alloca [500 x i8]
%3 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %8, %1
%5 = phi i64 [ 0, %1 ], [ %10, %8 ]
%6 = call i32 @read(i32 %0, i8* %3, i32 500)
%7 = icmp eq i32 %6, -1
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = sext i32 %6 to i64
%10 = add i64 %5, %9
%11 = icmp sgt i32 %6, 0
br i1 %11, label %4, label %12
12: ; preds = %8, %4
%13 = phi i64 [ 0, %4 ], [ %10, %8 ]
ret i64 %13
}