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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
scalar_t__ sendfile(int, int, int /*<<< orphan*/, scalar_t__);
int stat(char *, struct stat *);
int xyz_sock_sendfile(int sockfd, char *file) {
  int fd;
  struct stat statbuf;
  if (stat(file, &statbuf) == -1) {
    return -1;
  }
  fd = open(file, O_RDONLY);
  if (fd == -1) {
    return -1;
  }
  if (statbuf.st_size != sendfile(sockfd, fd, 0, statbuf.st_size)) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<xyz_sock_sendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1171 <xyz_sock_sendfile+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <xyz_sock_sendfile+0xa8>
mov    -0x10(%rbp),%rdi
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119c <xyz_sock_sendfile+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <xyz_sock_sendfile+0xa8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rcx
xor    %edx,%edx
call   1050 <sendfile@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     11d9 <xyz_sock_sendfile+0x99>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <xyz_sock_sendfile+0xa8>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_sock_sendfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%r14d
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1040 <stat@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     119e <xyz_sock_sendfile+0x5e>
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     119e <xyz_sock_sendfile+0x5e>
mov    %eax,%ebp
mov    (%rsp),%r15
mov    %r14d,%edi
mov    %eax,%esi
xor    %edx,%edx
mov    %r15,%rcx
call   1050 <sendfile@plt>
xor    %ebx,%ebx
cmp    %rax,%r15
setne  %bl
mov    %ebp,%edi
call   1030 <close@plt>
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
define i32 @xyz_sock_sendfile(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @stat(i8* %8, %struct.stat* %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %3
br label %34
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 -1, i32* %3
br label %34
19: ; preds = %12
%20 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i32, i32* %4
%23 = load i32, i32* %6
%24 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%25 = load i64, i64* %24
%26 = call i64 @sendfile(i32 %22, i32 %23, i32 0, i64 %25)
%27 = icmp ne i64 %21, %26
br i1 %27, label %28, label %31
28: ; preds = %19
%29 = load i32, i32* %6
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %3
br label %34
31: ; preds = %19
%32 = load i32, i32* %6
%33 = call i32 @close(i32 %32)
store i32 0, i32* %3
br label %34
34: ; preds = %31, %28, %18, %11
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
define i32 @xyz_sock_sendfile(i32 %0, i8* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %1, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %18, label %7
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* %1, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %18, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i64, i64* %12
%14 = tail call i64 @sendfile(i32 %0, i32 %9, i32 0, i64 %13)
%15 = icmp ne i64 %13, %14
%16 = tail call i32 @close(i32 %9)
%17 = sext i1 %15 to i32
br label %18
18: ; preds = %11, %7, %2
%19 = phi i32 [ -1, %2 ], [ -1, %7 ], [ %17, %11 ]
ret i32 %19
}