#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 */
struct stat {
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;

/* Variables and functions */
scalar_t__ ENOSYS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ complain(char *, char const *);
int doopen(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
off_t getsize(const char *path) {
  struct stat buf;
  int fd;
  static int no_stat, no_fstat;
  if (!no_stat) {
    if (stat(path, &buf) == 0) {
      return buf.st_size;
    }
    if (errno != ENOSYS) {
      complain("%s: stat", path);
      exit(1);
    }
    no_stat = 1;
  }
  fd = doopen(path, O_RDONLY, 0);
  if (!no_fstat) {
    if (fstat(fd, &buf) == 0) {
      close(fd);
      return buf.st_size;
    }
    if (errno != ENOSYS) {
      complain("%s: stat", path);
      exit(1);
    }
    no_fstat = 1;
  }
  if (lseek(fd, 0, SEEK_END) >= 0) {
    buf.st_size = lseek(fd, 0, SEEK_CUR);
    if (buf.st_size >= 0) {
      return buf.st_size;
    }
  }
  complain("%s: getting file size with lseek", path);
  close(fd);
  exit(1);
}


x86 assembly (clang, -O0):
<getsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpl   $0x0,0x2edd(%rip)
jne    11eb <getsize+0x7b>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11ad <getsize+0x3d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <getsize+0x17b>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
je     11e1 <getsize+0x71>
mov    -0x10(%rbp),%rsi
lea    0xe2e(%rip),%rdi
call   1080 <complain@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x1,0x2e75(%rip)
mov    -0x10(%rbp),%rdi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <doopen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,0x2e5b(%rip)
jne    1278 <getsize+0x108>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jne    123a <getsize+0xca>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <getsize+0x17b>
mov    0x2d7f(%rip),%rax
mov    (%rax),%rax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%rax
je     126e <getsize+0xfe>
mov    -0x10(%rbp),%rsi
lea    0xda1(%rip),%rdi
call   1080 <complain@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x1,0x2dec(%rip)
mov    -0x1c(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
cmp    $0x0,%rax
jl     12c9 <getsize+0x159>
mov    -0x1c(%rbp),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jl     12c4 <getsize+0x154>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12eb <getsize+0x17b>
jmp    12c9 <getsize+0x159>
mov    -0x10(%rbp),%rsi
lea    0xd35(%rip),%rdi
call   1080 <complain@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getsize>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
cmpb   $0x0,0x2ee3(%rip)
jne    11b4 <getsize+0x44>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
je     1257 <getsize+0xe7>
mov    0x2e26(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <getsize+0xf2>
movb   $0x1,0x2eac(%rip)
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1050 <doopen@plt>
mov    %eax,%ebp
cmpb   $0x0,0x2e94(%rip)
jne    11fe <getsize+0x8e>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1090 <fstat@plt>
test   %rax,%rax
je     1250 <getsize+0xe0>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <getsize+0xf2>
movb   $0x1,0x2e66(%rip)
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
test   %rax,%rax
js     1230 <getsize+0xc0>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,(%rsp)
test   %rax,%rax
jns    125b <getsize+0xeb>
lea    0xdd2(%rip),%rdi
mov    %rbx,%rsi
call   1080 <complain@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rsp),%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd97(%rip),%rdi
mov    %rbx,%rsi
call   1080 <complain@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@getsize.no_stat = internal global i32 0
@getsize.no_fstat = internal global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@.str = [9 x i8] c"%s: stat\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [33 x i8] c"%s: getting file size with lseek\00"
define i64 @getsize(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i32, i32* @getsize.no_stat
%7 = icmp ne i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%14 = load i64, i64* %13
store i64 %14, i64* %2
br label %71
15: ; preds = %8
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOSYS
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i32 @complain(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %15
store i32 1, i32* @getsize.no_stat
br label %24
24: ; preds = %23, %1
%25 = load i8*, i8** %3
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @doopen(i8* %25, i32 %26, i32 0)
store i32 %27, i32* %5
%28 = load i32, i32* @getsize.no_fstat
%29 = icmp ne i32 %28, 0
br i1 %29, label %48, label %30
30: ; preds = %24
%31 = load i32, i32* %5
%32 = call i64 @fstat(i32 %31, %struct.stat* %4)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%38 = load i64, i64* %37
store i64 %38, i64* %2
br label %71
39: ; preds = %30
%40 = load i64, i64* @errno
%41 = load i64, i64* @ENOSYS
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8*, i8** %3
%45 = call i32 @complain(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %39
store i32 1, i32* @getsize.no_fstat
br label %48
48: ; preds = %47, %24
%49 = load i32, i32* %5
%50 = load i32, i32* @SEEK_END
%51 = call i64 @lseek(i32 %49, i32 0, i32 %50)
%52 = icmp sge i64 %51, 0
br i1 %52, label %53, label %65
53: ; preds = %48
%54 = load i32, i32* %5
%55 = load i32, i32* @SEEK_CUR
%56 = call i64 @lseek(i32 %54, i32 0, i32 %55)
%57 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
store i64 %56, i64* %57
%58 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%59 = load i64, i64* %58
%60 = icmp sge i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %53
%62 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%63 = load i64, i64* %62
store i64 %63, i64* %2
br label %71
64: ; preds = %53
br label %65
65: ; preds = %64, %48
%66 = load i8*, i8** %3
%67 = call i32 @complain(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %66)
%68 = load i32, i32* %5
%69 = call i32 @close(i32 %68)
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %61, %34, %12
%72 = load i64, i64* %2
ret i64 %72
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@getsize.no_stat = internal unnamed_addr global i1 false
@getsize.no_fstat = internal unnamed_addr global i1 false
@errno = global i64 0
@ENOSYS = global i64 0
@.str = [9 x i8] c"%s: stat\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [33 x i8] c"%s: getting file size with lseek\00"
define i64 @getsize(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i1, i1* @getsize.no_stat
br i1 %4, label %19, label %5
5: ; preds = %1
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i64, i64* %9
br label %51
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOSYS
%14 = icmp eq i64 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = call i32 @complain(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
store i1 true, i1* @getsize.no_stat
br label %19
19: ; preds = %18, %1
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @doopen(i8* %0, i32 %20, i32 0)
%22 = load i1, i1* @getsize.no_fstat
br i1 %22, label %38, label %23
23: ; preds = %19
%24 = call i64 @fstat(i32 %21, %struct.stat* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = call i32 @close(i32 %21)
%28 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%29 = load i64, i64* %28
br label %51
30: ; preds = %23
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOSYS
%33 = icmp eq i64 %31, %32
br i1 %33, label %37, label %34
34: ; preds = %30
%35 = call i32 @complain(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30
store i1 true, i1* @getsize.no_fstat
br label %38
38: ; preds = %37, %19
%39 = load i32, i32* @SEEK_END
%40 = call i64 @lseek(i32 %21, i32 0, i32 %39)
%41 = icmp sgt i64 %40, -1
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = load i32, i32* @SEEK_CUR
%44 = call i64 @lseek(i32 %21, i32 0, i32 %43)
%45 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
store i64 %44, i64* %45
%46 = icmp sgt i64 %44, -1
br i1 %46, label %51, label %47
47: ; preds = %42, %38
%48 = call i32 @complain(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%49 = call i32 @close(i32 %21)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42, %26, %8
%52 = phi i64 [ %29, %26 ], [ %10, %8 ], [ %44, %42 ]
ret i64 %52
}