#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int get_file_size(const char *pathname) {
  int fd, result = 0;
  char buffer[256];
  fd = open(pathname, O_RDONLY);
  if (fd < 0) {
    return -1;
  }
  for (;;) {
    int ret = read(fd, buffer, sizeof buffer);
    if (ret < 0) {
      if (errno == EINTR) {
        continue;
      }
      break;
    }
    if (ret == 0) {
      break;
    }
    result += ret;
  }
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<get_file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1171 <get_file_size+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <get_file_size+0xc2>
jmp    1176 <get_file_size+0x46>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1040 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11c1 <get_file_size+0x91>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jne    11bc <get_file_size+0x8c>
jmp    1176 <get_file_size+0x46>
jmp    11e4 <get_file_size+0xb4>
cmpl   $0x0,-0x124(%rbp)
jne    11d3 <get_file_size+0xa3>
jmp    11e4 <get_file_size+0xb4>
mov    -0x124(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1176 <get_file_size+0x46>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_size>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     119d <get_file_size+0x6d>
mov    %eax,%ebp
xor    %r14d,%r14d
mov    %rsp,%r12
mov    0x2e78(%rip),%r15
mov    0x2e69(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x100,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    118d <get_file_size+0x5d>
mov    (%r15),%rax
cmp    (%rbx),%rax
je     1170 <get_file_size+0x40>
jmp    1194 <get_file_size+0x64>
add    %eax,%r14d
test   %eax,%eax
jne    1170 <get_file_size+0x40>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11a3 <get_file_size+0x73>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @get_file_size(i8* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %22, label %7
7: ; preds = %1, %16
%8 = phi i32 [ %18, %16 ], [ 0, %1 ]
br label %9
9: ; preds = %7, %12
%10 = call i32 @read(i32 %5, i8* %3, i32 256)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %19
16: ; preds = %9
%17 = icmp eq i32 %10, 0
%18 = add nsw i32 %10, %8
br i1 %17, label %19, label %7
19: ; preds = %16, %12
%20 = phi i32 [ %8, %12 ], [ %18, %16 ]
%21 = tail call i32 @close(i32 %5)
br label %22
22: ; preds = %1, %19
%23 = phi i32 [ %20, %19 ], [ -1, %1 ]
ret i32 %23
}