#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fstat(int, struct stat *);
size_t lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
size_t get_fd_size(int fd) {
  struct stat stat;
  fstat(fd, &stat);
  if (stat.st_size)
    return stat.st_size;
  size_t ret = lseek(fd, 0, SEEK_END);
  lseek(fd, 0, SEEK_SET);
  return ret;
}


x86 assembly (clang, -O0):
<get_fd_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <fstat@plt>
cmpq   $0x0,-0x18(%rbp)
je     114f <get_fd_size+0x2f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1181 <get_fd_size+0x61>
mov    -0xc(%rbp),%edi
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_fd_size>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%rsi
call   1040 <fstat@plt>
mov    (%rsp),%rax
test   %rax,%rax
jne    1161 <get_fd_size+0x41>
mov    0x2eaa(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,%r14
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @get_fd_size(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i64
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @fstat(i32 %6, %struct.stat* %4)
%8 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%9 = load i64, i64* %8
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* %2
br label %22
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i32, i32* @SEEK_END
%17 = call i64 @lseek(i32 %15, i32 0, i32 %16)
store i64 %17, i64* %5
%18 = load i32, i32* %3
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @lseek(i32 %18, i32 0, i32 %19)
%21 = load i64, i64* %5
store i64 %21, i64* %2
br label %22
22: ; preds = %14, %11
%23 = load i64, i64* %2
ret i64 %23
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @get_fd_size(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @fstat(i32 %0, %struct.stat* %2)
%5 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%6 = load i64, i64* %5
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @SEEK_END
%10 = tail call i64 @lseek(i32 %0, i32 0, i32 %9)
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @lseek(i32 %0, i32 0, i32 %11)
br label %13
13: ; preds = %1, %8
%14 = phi i64 [ %10, %8 ], [ %6, %1 ]
ret i64 %14
}