#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ ftruncate(int /*<<< orphan*/, int /*<<< orphan*/);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int copy_contents(FILE *src, FILE *dest) {
  char buf[1024];
  int len;
  rewind(src);
  rewind(dest);
  if (ftruncate(fileno(dest), 0))
    return 1;
  while (!feof(src)) {
    len = fread(buf, 1, 1024, src);
    if (len > 0) {
      if (fwrite(buf, 1, len, dest) != len) {
        return 1;
      }
    } else {
      return !feof(src);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<copy_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
je     11ad <copy_contents+0x4d>
movl   $0x1,-0x4(%rbp)
jmp    125f <copy_contents+0xff>
jmp    11b2 <copy_contents+0x52>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11d0 <copy_contents+0x70>
jmp    1258 <copy_contents+0xf8>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jle    1235 <copy_contents+0xd5>
lea    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
cmp    -0x424(%rbp),%eax
je     1230 <copy_contents+0xd0>
movl   $0x1,-0x4(%rbp)
jmp    125f <copy_contents+0xff>
jmp    1253 <copy_contents+0xf3>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    125f <copy_contents+0xff>
jmp    11b2 <copy_contents+0x52>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_contents>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
call   1040 <rewind@plt>
mov    %r14,%rdi
call   1040 <rewind@plt>
mov    %r14,%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1050 <ftruncate@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     11af <copy_contents+0x4f>
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%r15
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11ff <copy_contents+0x9f>
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1203 <copy_contents+0xa3>
mov    %eax,%ebx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
cmp    %ebx,%eax
je     11c0 <copy_contents+0x60>
jmp    119d <copy_contents+0x3d>
xor    %ebp,%ebp
jmp    119d <copy_contents+0x3d>
mov    %r12,%rdi
call   1060 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
sete   %bpl
jmp    119d <copy_contents+0x3d>

LLVM IR (-O0):
define i32 @copy_contents(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
%8 = load i32*, i32** %4
%9 = call i32 @rewind(i32* %8)
%10 = load i32*, i32** %5
%11 = call i32 @rewind(i32* %10)
%12 = load i32*, i32** %5
%13 = call i32 @fileno(i32* %12)
%14 = call i64 @ftruncate(i32 %13, i32 0)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %3
br label %46
17: ; preds = %2
br label %18
18: ; preds = %44, %17
%19 = load i32*, i32** %4
%20 = call i32 @feof(i32* %19)
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
br i1 %22, label %23, label %45
23: ; preds = %18
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = load i32*, i32** %4
%26 = call i32 @fread(i8* %24, i32 1, i32 1024, i32* %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %23
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%31 = load i32, i32* %7
%32 = load i32*, i32** %5
%33 = call i32 @fwrite(i8* %30, i32 1, i32 %31, i32* %32)
%34 = load i32, i32* %7
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 1, i32* %3
br label %46
37: ; preds = %29
br label %44
38: ; preds = %23
%39 = load i32*, i32** %4
%40 = call i32 @feof(i32* %39)
%41 = icmp ne i32 %40, 0
%42 = xor i1 %41, true
%43 = zext i1 %42 to i32
store i32 %43, i32* %3
br label %46
44: ; preds = %37
br label %18
45: ; preds = %18
store i32 0, i32* %3
br label %46
46: ; preds = %45, %38, %36, %16
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
define i32 @copy_contents(i32* nocapture %0, i32* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @rewind(i32* %0)
%6 = tail call i32 @rewind(i32* %1)
%7 = tail call i32 @fileno(i32* %1)
%8 = tail call i64 @ftruncate(i32 %7, i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %2, %16
%11 = tail call i32 @feof(i32* %0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = call i32 @fread(i8* %4, i32 1, i32 1024, i32* %0)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @fwrite(i8* %4, i32 1, i32 %14, i32* %1)
%18 = icmp eq i32 %17, %14
br i1 %18, label %10, label %23
19: ; preds = %13
%20 = tail call i32 @feof(i32* %0)
%21 = icmp eq i32 %20, 0
%22 = zext i1 %21 to i32
br label %23
23: ; preds = %10, %16, %2, %19
%24 = phi i32 [ %22, %19 ], [ 1, %2 ], [ 0, %10 ], [ 1, %16 ]
ret i32 %24
}