#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
void *lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
long long int getfilesize_fd(int fd, char *filename, int sizebyfilename) {
  if (sizebyfilename == 1)
    fd = open(filename, O_RDONLY);
  if (fd < 0) {
    printf("*** ZVM Error get filesize wrong file descriptor %d\n", fd);
    return -1;
  }
  long fsize;
  fsize = lseek(fd, 0, SEEK_END);
  if (fsize < 0) {
    printf("*** ZVM Error get filesize for file descriptor %d\n", fd);
    if (sizebyfilename == 1)
      close(fd);
    return -1;
  }
  int temp = lseek(fd, 0, SEEK_SET);
  if (temp < 0)
    printf("*** ZVM Warning! I can't SEEK_SET execute for file descriptor %d\n",
           fd);
  if (sizebyfilename == 1)
    close(fd);
  return fsize;
}


x86 assembly (clang, -O0):
<getfilesize_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    1171 <getfilesize_fd+0x31>
mov    -0x18(%rbp),%rdi
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1197 <getfilesize_fd+0x57>
mov    -0xc(%rbp),%esi
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1230 <getfilesize_fd+0xf0>
mov    -0xc(%rbp),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11e7 <getfilesize_fd+0xa7>
mov    -0xc(%rbp),%esi
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    11da <getfilesize_fd+0x9a>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1230 <getfilesize_fd+0xf0>
mov    -0xc(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1216 <getfilesize_fd+0xd6>
mov    -0xc(%rbp),%esi
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x1,-0x1c(%rbp)
jne    1228 <getfilesize_fd+0xe8>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getfilesize_fd>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %edi,%ebx
cmp    $0x1,%edx
jne    1163 <getfilesize_fd+0x23>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    %rsi,%rdi
mov    %eax,%esi
call   1060 <open@plt>
mov    %eax,%ebx
test   %ebx,%ebx
js     1195 <getfilesize_fd+0x55>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   %rax,%rax
jns    11ac <getfilesize_fd+0x6c>
lea    0xeb0(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0xffffffffffffffff,%rbp
jmp    11d6 <getfilesize_fd+0x96>
lea    0xe64(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    $0xffffffffffffffff,%rbp
jmp    11e3 <getfilesize_fd+0xa3>
mov    %rax,%rbp
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
test   $0x80000000,%eax
je     11d6 <getfilesize_fd+0x96>
lea    0xe99(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
cmp    $0x1,%r14d
jne    11e3 <getfilesize_fd+0xa3>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [53 x i8] c"*** ZVM Error get filesize wrong file descriptor %d\0A\00"
@SEEK_END = global i32 0
@.str.1 = [51 x i8] c"*** ZVM Error get filesize for file descriptor %d\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [66 x i8] c"*** ZVM Warning! I can't SEEK_SET execute for file descriptor %d\0A\00"
define i64 @getfilesize_fd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %5
br label %16
16: ; preds = %12, %3
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %5
%21 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %20)
store i64 -1, i64* %4
br label %56
22: ; preds = %16
%23 = load i32, i32* %5
%24 = load i32, i32* @SEEK_END
%25 = call i8* @lseek(i32 %23, i32 0, i32 %24)
%26 = ptrtoint i8* %25 to i64
store i64 %26, i64* %8
%27 = load i64, i64* %8
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %22
%30 = load i32, i32* %5
%31 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %7
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %29
store i64 -1, i64* %4
br label %56
38: ; preds = %22
%39 = load i32, i32* %5
%40 = load i32, i32* @SEEK_SET
%41 = call i8* @lseek(i32 %39, i32 0, i32 %40)
%42 = ptrtoint i8* %41 to i32
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* %5
%47 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %45, %38
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32, i32* %5
%53 = call i32 @close(i32 %52)
br label %54
54: ; preds = %51, %48
%55 = load i64, i64* %8
store i64 %55, i64* %4
br label %56
56: ; preds = %54, %37, %19
%57 = load i64, i64* %4
ret i64 %57
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [53 x i8] c"*** ZVM Error get filesize wrong file descriptor %d\0A\00"
@SEEK_END = global i32 0
@.str.1 = [51 x i8] c"*** ZVM Error get filesize for file descriptor %d\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [66 x i8] c"*** ZVM Warning! I can't SEEK_SET execute for file descriptor %d\0A\00"
define i64 @getfilesize_fd(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 1
br i1 %4, label %5, label %8
5: ; preds = %3
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %1, i32 %6)
br label %8
8: ; preds = %5, %3
%9 = phi i32 [ %7, %5 ], [ %0, %3 ]
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %33
13: ; preds = %8
%14 = load i32, i32* @SEEK_END
%15 = tail call i8* @lseek(i32 %9, i32 0, i32 %14)
%16 = ptrtoint i8* %15 to i64
%17 = icmp slt i8* %15, null
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = tail call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br i1 %4, label %20, label %33
20: ; preds = %18
%21 = tail call i32 @close(i32 %9)
br label %33
22: ; preds = %13
%23 = load i32, i32* @SEEK_SET
%24 = tail call i8* @lseek(i32 %9, i32 0, i32 %23)
%25 = ptrtoint i8* %24 to i64
%26 = and i64 %25, 2147483648
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = tail call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %30
30: ; preds = %28, %22
br i1 %4, label %31, label %33
31: ; preds = %30
%32 = tail call i32 @close(i32 %9)
br label %33
33: ; preds = %20, %18, %31, %30, %11
%34 = phi i64 [ -1, %11 ], [ -1, %20 ], [ -1, %18 ], [ %16, %31 ], [ %16, %30 ]
ret i64 %34
}