#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
long my_fgetsize(char *path) {
  long i;
  int j;
  int fd;
  char buff[4096];
  if (path == NULL)
    return (-1);
  i = 0;
  if ((fd = open(path, O_RDONLY)) < 0)
    return (-1);
  while ((j = read(fd, buff, 4096)) > 0)
    i += j;
  close(fd);
  return (i);
}


x86 assembly (clang, -O0):
<my_fgetsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1157 <my_fgetsize+0x27>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d0 <my_fgetsize+0xa0>
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    118a <my_fgetsize+0x5a>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11d0 <my_fgetsize+0xa0>
jmp    118f <my_fgetsize+0x5f>
mov    -0x20(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    11c0 <my_fgetsize+0x90>
movslq -0x1c(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    118f <my_fgetsize+0x5f>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fgetsize>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    $0xffffffffffffffff,%rbx
test   %rdi,%rdi
je     1182 <my_fgetsize+0x52>
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1182 <my_fgetsize+0x52>
mov    %eax,%ebp
xor    %ecx,%ecx
mov    %rsp,%r14
mov    %rcx,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,%ecx
add    %rbx,%rcx
test   %eax,%eax
jg     1160 <my_fgetsize+0x30>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rbx,%rax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
define i64 @my_fgetsize(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i64 -1, i64* %2
br label %32
11: ; preds = %1
store i64 0, i64* %4
%12 = load i8*, i8** %3
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i64 -1, i64* %2
br label %32
17: ; preds = %11
br label %18
18: ; preds = %23, %17
%19 = load i32, i32* %6
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 4096)
store i32 %21, i32* %5
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = load i64, i64* %4
%27 = add nsw i64 %26, %25
store i64 %27, i64* %4
br label %18
28: ; preds = %18
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
%31 = load i64, i64* %4
store i64 %31, i64* %2
br label %32
32: ; preds = %28, %16, %10
%33 = load i64, i64* %2
ret i64 %33
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i64 @my_fgetsize(i8* %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %17, label %5
5: ; preds = %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %5, %9
%10 = phi i64 [ %14, %9 ], [ 0, %5 ]
%11 = call i32 @read(i32 %7, i8* %3, i32 4096)
%12 = icmp sgt i32 %11, 0
%13 = zext i32 %11 to i64
%14 = add nuw nsw i64 %10, %13
br i1 %12, label %9, label %15
15: ; preds = %9
%16 = tail call i32 @close(i32 %7)
br label %17
17: ; preds = %5, %1, %15
%18 = phi i64 [ %10, %15 ], [ -1, %1 ], [ -1, %5 ]
ret i64 %18
}