#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 {
  int st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ ftruncate(int, int);
int xpread(int, char *, int, int);
int xpwrite(int, char *, int, int);
int rd_copy_uncompressed(int ffd, int dfd) {
  char buffer[65536];
  off_t bytes;
  struct stat st;
  ((void)0);
  if (ffd < 0 || fstat(ffd, &st) || !S_ISREG(st.st_mode) ||
      (bytes = st.st_size) == 0)
    return -1;
  while (bytes) {
    ssize_t blocksize = ((bytes - 1) & (65536 - 1)) + 1;
    off_t offset = bytes - blocksize;
    ((void)0);
    if (xpread(ffd, buffer, blocksize, offset) != blocksize ||
        xpwrite(dfd, buffer, blocksize, offset) != blocksize)
      return -1;
    ftruncate(ffd, offset);
    bytes = offset;
  }
  return 0;
}


x86 assembly (clang, -O0):
<rd_copy_uncompressed>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11ad <rd_copy_uncompressed+0x5d>
mov    -0x8(%rbp),%edi
lea    -0x10020(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jne    11ad <rd_copy_uncompressed+0x5d>
mov    -0x1001c(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%eax
je     11ad <rd_copy_uncompressed+0x5d>
mov    -0x10020(%rbp),%eax
mov    %eax,-0x10014(%rbp)
cmp    $0x0,%eax
jne    11b9 <rd_copy_uncompressed+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <rd_copy_uncompressed+0x124>
jmp    11be <rd_copy_uncompressed+0x6e>
cmpl   $0x0,-0x10014(%rbp)
je     126d <rd_copy_uncompressed+0x11d>
mov    -0x10014(%rbp),%eax
sub    $0x1,%eax
and    $0xffff,%eax
add    $0x1,%eax
mov    %eax,-0x10024(%rbp)
mov    -0x10014(%rbp),%eax
sub    -0x10024(%rbp),%eax
mov    %eax,-0x10028(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    -0x10024(%rbp),%edx
mov    -0x10028(%rbp),%ecx
call   1050 <xpread@plt>
cmp    -0x10024(%rbp),%eax
jne    1242 <rd_copy_uncompressed+0xf2>
mov    -0xc(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    -0x10024(%rbp),%edx
mov    -0x10028(%rbp),%ecx
call   1030 <xpwrite@plt>
cmp    -0x10024(%rbp),%eax
je     124e <rd_copy_uncompressed+0xfe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <rd_copy_uncompressed+0x124>
mov    -0x8(%rbp),%edi
mov    -0x10028(%rbp),%esi
call   1040 <ftruncate@plt>
mov    -0x10028(%rbp),%eax
mov    %eax,-0x10014(%rbp)
jmp    11be <rd_copy_uncompressed+0x6e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rd_copy_uncompressed>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    $0xffffffff,%r14d
test   %edi,%edi
js     1180 <rd_copy_uncompressed+0x30>
mov    %esi,%r15d
mov    %edi,%r13d
lea    0x8(%rsp),%rsi
call   1070 <fstat@plt>
test   %rax,%rax
je     1195 <rd_copy_uncompressed+0x45>
mov    %r14d,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0xc(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %eax,%eax
je     1180 <rd_copy_uncompressed+0x30>
mov    0x8(%rsp),%ebp
test   %ebp,%ebp
je     1180 <rd_copy_uncompressed+0x30>
lea    0x10(%rsp),%r12
nop
lea    -0x1(%rbp),%eax
movzwl %ax,%ebx
add    $0x1,%ebx
sub    %ebx,%ebp
mov    %r13d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1050 <xpread@plt>
cmp    %ebx,%eax
jne    1180 <rd_copy_uncompressed+0x30>
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1030 <xpwrite@plt>
cmp    %ebx,%eax
jne    1180 <rd_copy_uncompressed+0x30>
mov    %r13d,%edi
mov    %ebp,%esi
call   1040 <ftruncate@plt>
test   %ebp,%ebp
jne    11b0 <rd_copy_uncompressed+0x60>
xor    %r14d,%r14d
jmp    1180 <rd_copy_uncompressed+0x30>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
define i32 @rd_copy_uncompressed(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [65536 x i8]
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %26, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = call i64 @fstat(i32 %14, %struct.stat* %8)
%16 = icmp ne i64 %15, 0
br i1 %16, label %26, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @S_ISREG(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %7
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22, %17, %13, %2
store i32 -1, i32* %3
br label %61
27: ; preds = %22
br label %28
28: ; preds = %55, %27
%29 = load i32, i32* %7
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %60
31: ; preds = %28
%32 = load i32, i32* %7
%33 = sub nsw i32 %32, 1
%34 = and i32 %33, 65535
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
%36 = load i32, i32* %7
%37 = load i32, i32* %9
%38 = sub nsw i32 %36, %37
store i32 %38, i32* %10
%39 = load i32, i32* %4
%40 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%41 = load i32, i32* %9
%42 = load i32, i32* %10
%43 = call i32 @xpread(i32 %39, i8* %40, i32 %41, i32 %42)
%44 = load i32, i32* %9
%45 = icmp ne i32 %43, %44
br i1 %45, label %54, label %46
46: ; preds = %31
%47 = load i32, i32* %5
%48 = getelementptr inbounds [65536 x i8], [65536 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* %9
%50 = load i32, i32* %10
%51 = call i32 @xpwrite(i32 %47, i8* %48, i32 %49, i32 %50)
%52 = load i32, i32* %9
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %55
54: ; preds = %46, %31
store i32 -1, i32* %3
br label %61
55: ; preds = %46
%56 = load i32, i32* %4
%57 = load i32, i32* %10
%58 = call i32 @ftruncate(i32 %56, i32 %57)
%59 = load i32, i32* %10
store i32 %59, i32* %7
br label %28
60: ; preds = %28
store i32 0, i32* %3
br label %61
61: ; preds = %60, %54, %26
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
define i32 @rd_copy_uncompressed(i32 %0, i32 %1) {
%3 = alloca [65536 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp slt i32 %0, 0
br i1 %7, label %34, label %8
8: ; preds = %2
%9 = call i64 @fstat(i32 %0, %struct.stat* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %34
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i32 @S_ISREG(i32 %13)
%15 = icmp eq i32 %14, 0
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = icmp eq i32 %17, 0
%19 = select i1 %15, i1 true, i1 %18
br i1 %19, label %34, label %20
20: ; preds = %11, %31
%21 = phi i32 [ %25, %31 ], [ %17, %11 ]
%22 = add i32 %21, 65535
%23 = and i32 %22, 65535
%24 = add nuw nsw i32 %23, 1
%25 = sub nsw i32 %21, %24
%26 = call i32 @xpread(i32 %0, i8* %5, i32 %24, i32 %25)
%27 = icmp eq i32 %26, %24
br i1 %27, label %28, label %34
28: ; preds = %20
%29 = call i32 @xpwrite(i32 %1, i8* %5, i32 %24, i32 %25)
%30 = icmp eq i32 %29, %24
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = call i32 @ftruncate(i32 %0, i32 %25)
%33 = icmp eq i32 %25, 0
br i1 %33, label %34, label %20
34: ; preds = %31, %28, %20, %2, %8, %11
%35 = phi i32 [ -1, %11 ], [ -1, %8 ], [ -1, %2 ], [ 0, %31 ], [ -1, %28 ], [ -1, %20 ]
ret i32 %35
}