#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fail(char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
void copy_rest(FILE *f, FILE *g) {
  char buf[4096];
  int len;
  while ((len = fread(buf, 1, sizeof buf, f)) > 0) {
    if (fwrite(buf, 1, len, g) != len)
      fail("write error");
  }
  if (len < 0)
    fail("read error");
}


x86 assembly (clang, -O0):
<copy_rest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %eax,-0x1014(%rbp)
cmp    $0x0,%eax
jle    11a4 <copy_rest+0x74>
lea    -0x1010(%rbp),%rdi
mov    -0x1014(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
cmp    -0x1014(%rbp),%eax
je     119f <copy_rest+0x6f>
lea    0xe66(%rip),%rdi
call   1040 <fail@plt>
jmp    1143 <copy_rest+0x13>
cmpl   $0x0,-0x1014(%rbp)
jge    11bd <copy_rest+0x8d>
lea    0xe54(%rip),%rdi
call   1040 <fail@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_rest>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1000,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11ab <copy_rest+0x7b>
mov    %rsp,%rbx
lea    0xe96(%rip),%r12
jmp    118b <copy_rest+0x5b>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11ab <copy_rest+0x7b>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1050 <fwrite@plt>
cmp    %ebp,%eax
je     1170 <copy_rest+0x40>
mov    %r12,%rdi
call   1040 <fail@plt>
jmp    1170 <copy_rest+0x40>
test   %ebp,%ebp
js     11bf <copy_rest+0x8f>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe46(%rip),%rdi
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fail@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"write error\00"
@.str.1 = [11 x i8] c"read error\00"
define void @copy_rest(i32* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [4096 x i8]
%6 = alloca i32
store i32* %0, i32** %3
store i32* %1, i32** %4
br label %7
7: ; preds = %21, %2
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = load i32*, i32** %3
%10 = call i32 @fread(i8* %8, i32 1, i32 4096, i32* %9)
store i32 %10, i32* %6
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %7
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%14 = load i32, i32* %6
%15 = load i32*, i32** %4
%16 = call i32 @fwrite(i8* %13, i32 1, i32 %14, i32* %15)
%17 = load i32, i32* %6
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @fail(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %12
br label %7
22: ; preds = %7
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @fail(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"write error\00"
@.str.1 = [11 x i8] c"read error\00"
define void @copy_rest(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %0)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %2, %13
%8 = phi i32 [ %14, %13 ], [ %5, %2 ]
%9 = call i32 @fwrite(i8* %4, i32 1, i32 %8, i32* %1)
%10 = icmp eq i32 %9, %8
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @fail(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = call i32 @fread(i8* %4, i32 1, i32 4096, i32* %0)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %7, label %16
16: ; preds = %13, %2
%17 = phi i32 [ %5, %2 ], [ %14, %13 ]
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @fail(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
ret void
}