#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;

/* Variables and functions */
int send(int, char *, size_t, int /*<<< orphan*/);
int sendfile(int, int, int *, size_t);
int stx_sendfile(int fd, int sock, off_t *offset, size_t count, off_t *sent,
                 char *headers, size_t headers_len) {
  ssize_t tx;
  if (NULL != headers) {
    if (-1 == (tx = send(sock, headers, headers_len, 0))) {
      return -1;
    }
  }
  tx = sendfile(sock, fd, offset, count);
  if (-1 == tx) {
    return -1;
  } else {
    *sent = tx;
  }
  return 0;
}


x86 assembly (clang, -O0):
<stx_sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
je     1183 <stx_sendfile+0x63>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x10(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <send@plt>
mov    %eax,%ecx
mov    %ecx,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    117e <stx_sendfile+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <stx_sendfile+0xa3>
jmp    1183 <stx_sendfile+0x63>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <sendfile@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
jne    11b3 <stx_sendfile+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <stx_sendfile+0xa3>
mov    -0x34(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stx_sendfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebx
mov    %edi,%ebp
test   %r9,%r9
je     1150 <stx_sendfile+0x30>
mov    0x30(%rsp),%rdx
mov    %ebx,%edi
mov    %r9,%rsi
xor    %ecx,%ecx
call   1030 <send@plt>
cmp    $0xffffffff,%eax
je     116b <stx_sendfile+0x4b>
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <sendfile@plt>
cmp    $0xffffffff,%eax
je     116b <stx_sendfile+0x4b>
mov    %eax,(%r14)
xor    %eax,%eax
jmp    1170 <stx_sendfile+0x50>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stx_sendfile(i32 %0, i32 %1, i32* %2, i64 %3, i32* %4, i8* %5, i64 %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i32*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
store i32 %0, i32* %9
store i32 %1, i32* %10
store i32* %2, i32** %11
store i64 %3, i64* %12
store i32* %4, i32** %13
store i8* %5, i8** %14
store i64 %6, i64* %15
%17 = load i8*, i8** %14
%18 = icmp ne i8* null, %17
br i1 %18, label %19, label %27
19: ; preds = %7
%20 = load i32, i32* %10
%21 = load i8*, i8** %14
%22 = load i64, i64* %15
%23 = call i32 @send(i32 %20, i8* %21, i64 %22, i32 0)
store i32 %23, i32* %16
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %8
br label %40
26: ; preds = %19
br label %27
27: ; preds = %26, %7
%28 = load i32, i32* %10
%29 = load i32, i32* %9
%30 = load i32*, i32** %11
%31 = load i64, i64* %12
%32 = call i32 @sendfile(i32 %28, i32 %29, i32* %30, i64 %31)
store i32 %32, i32* %16
%33 = load i32, i32* %16
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 -1, i32* %8
br label %40
36: ; preds = %27
%37 = load i32, i32* %16
%38 = load i32*, i32** %13
store i32 %37, i32* %38
br label %39
39: ; preds = %36
store i32 0, i32* %8
br label %40
40: ; preds = %39, %35, %25
%41 = load i32, i32* %8
ret i32 %41
}

LLVM IR (-O2):
define i32 @stx_sendfile(i32 %0, i32 %1, i32* %2, i64 %3, i32* nocapture writeonly %4, i8* %5, i64 %6) {
%8 = icmp eq i8* %5, null
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = tail call i32 @send(i32 %1, i8* %5, i64 %6, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %16, label %12
12: ; preds = %9, %7
%13 = tail call i32 @sendfile(i32 %1, i32 %0, i32* %2, i64 %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %16, label %15
15: ; preds = %12
store i32 %13, i32* %4
br label %16
16: ; preds = %12, %9, %15
%17 = phi i32 [ 0, %15 ], [ -1, %9 ], [ -1, %12 ]
ret i32 %17
}