#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 */
typedef int ssize_t;
typedef int off_t;
typedef int loff_t;

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ENOSYS;
int __dietlibc_sendfile64(int, int, int *, size_t);
scalar_t__ errno;
int sendfile(int, int, int *, size_t);
ssize_t sendfile64(int out_fd, int in_fd, loff_t *offset, size_t count) {
  static int havesendfile64 = 1;
  ssize_t r = -1;
  if (havesendfile64) {
    r = __dietlibc_sendfile64(out_fd, in_fd, offset, count);
    if (r == -1 && errno == ENOSYS)
      havesendfile64 = 0;
  }
  if (!havesendfile64) {
    off_t o = *offset;
    if (*offset > 0x7fffffff) {
      errno = EINVAL;
      return -1;
    }
    r = sendfile(out_fd, in_fd, &o, count);
    *offset = o;
    return r;
  }
  return r;
}


x86 assembly (clang, -O0):
<sendfile64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpl   $0x0,0x2eec(%rip)
je     1193 <sendfile64+0x73>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1030 <__dietlibc_sendfile64@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    118e <sendfile64+0x6e>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e6d(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <sendfile64+0x6e>
movl   $0x0,0x2ea2(%rip)
jmp    1193 <sendfile64+0x73>
cmpl   $0x0,0x2e96(%rip)
jne    1203 <sendfile64+0xe3>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
cmpl   $0x7fffffff,(%rax)
jle    11d9 <sendfile64+0xb9>
mov    0x2e10(%rip),%rax
mov    (%rax),%rcx
mov    0x2e0e(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <sendfile64+0xe9>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdx
call   1040 <sendfile@plt>
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1209 <sendfile64+0xe9>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfile64>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%ebp
cmpb   $0x0,0x2eff(%rip)
jne    1178 <sendfile64+0x58>
mov    %ebp,%edi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1030 <__dietlibc_sendfile64@plt>
cmp    $0xffffffff,%eax
jne    116f <sendfile64+0x4f>
mov    0x2e81(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e87(%rip),%rdx
cmp    (%rdx),%rcx
jne    116f <sendfile64+0x4f>
movb   $0x1,0x2ecb(%rip)
jmp    1178 <sendfile64+0x58>
cmpb   $0x1,0x2ec2(%rip)
jne    1196 <sendfile64+0x76>
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
mov    %r15d,%esi
mov    %r14,%rcx
call   1040 <sendfile@plt>
mov    0x4(%rsp),%ecx
mov    %ecx,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@sendfile64.havesendfile64 = internal global i32 1
@errno = global i64 0
@ENOSYS = global i64 0
@EINVAL = global i64 0
define i32 @sendfile64(i32 %0, i32 %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i64 %3, i64* %9
store i32 -1, i32* %10
%12 = load i32, i32* @sendfile64.havesendfile64
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %4
%15 = load i32, i32* %6
%16 = load i32, i32* %7
%17 = load i32*, i32** %8
%18 = load i64, i64* %9
%19 = call i32 @__dietlibc_sendfile64(i32 %15, i32 %16, i32* %17, i64 %18)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %14
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOSYS
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 0, i32* @sendfile64.havesendfile64
br label %27
27: ; preds = %26, %22, %14
br label %28
28: ; preds = %27, %4
%29 = load i32, i32* @sendfile64.havesendfile64
%30 = icmp ne i32 %29, 0
br i1 %30, label %47, label %31
31: ; preds = %28
%32 = load i32*, i32** %8
%33 = load i32, i32* %32
store i32 %33, i32* %11
%34 = load i32*, i32** %8
%35 = load i32, i32* %34
%36 = icmp sgt i32 %35, 2147483647
br i1 %36, label %37, label %39
37: ; preds = %31
%38 = load i64, i64* @EINVAL
store i64 %38, i64* @errno
store i32 -1, i32* %5
br label %49
39: ; preds = %31
%40 = load i32, i32* %6
%41 = load i32, i32* %7
%42 = load i64, i64* %9
%43 = call i32 @sendfile(i32 %40, i32 %41, i32* %11, i64 %42)
store i32 %43, i32* %10
%44 = load i32, i32* %11
%45 = load i32*, i32** %8
store i32 %44, i32* %45
%46 = load i32, i32* %10
store i32 %46, i32* %5
br label %49
47: ; preds = %28
%48 = load i32, i32* %10
store i32 %48, i32* %5
br label %49
49: ; preds = %47, %39, %37
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@sendfile64.havesendfile64 = internal unnamed_addr global i1 false
@errno = global i64 0
@ENOSYS = global i64 0
@EINVAL = global i64 0
define i32 @sendfile64(i32 %0, i32 %1, i32* %2, i64 %3) {
%5 = alloca i32
%6 = load i1, i1* @sendfile64.havesendfile64
br i1 %6, label %17, label %7
7: ; preds = %4
%8 = tail call i32 @__dietlibc_sendfile64(i32 %0, i32 %1, i32* %2, i64 %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOSYS
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i1 true, i1* @sendfile64.havesendfile64
br label %17
15: ; preds = %7, %10
%16 = load i1, i1* @sendfile64.havesendfile64
br i1 %16, label %17, label %22
17: ; preds = %4, %14, %15
%18 = bitcast i32* %5 to i8*
%19 = load i32, i32* %2
store i32 %19, i32* %5
%20 = call i32 @sendfile(i32 %0, i32 %1, i32* %5, i64 %3)
%21 = load i32, i32* %5
store i32 %21, i32* %2
br label %22
22: ; preds = %15, %17
%23 = phi i32 [ %20, %17 ], [ %8, %15 ]
ret i32 %23
}