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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fputs(char *, int /*<<< orphan*/ *);
int XTL_CopyFileContents(char *FileName, FILE *outfile) {
  FILE *fh;
  char buffer[1000];
  fh = fopen(FileName, "r");
  if (fh == NULL)
    return -1;
  while (fgets(buffer, sizeof(buffer) - 1, fh) != NULL) {
    if (fputs(buffer, outfile) <= 0)
      return -2;
  }
  fclose(fh);
  return 0;
}


x86 assembly (clang, -O0):
<XTL_CopyFileContents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117e <XTL_CopyFileContents+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <XTL_CopyFileContents+0x9d>
jmp    1183 <XTL_CopyFileContents+0x43>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3e7,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11cd <XTL_CopyFileContents+0x8d>
lea    -0x410(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fputs@plt>
cmp    $0x0,%rax
jg     11c8 <XTL_CopyFileContents+0x88>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11dd <XTL_CopyFileContents+0x9d>
jmp    1183 <XTL_CopyFileContents+0x43>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<XTL_CopyFileContents>:
push   %r15
push   %r14
push   %rbx
sub    $0x3f0,%rsp
mov    %rsi,%r14
lea    0xeaa(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     119c <XTL_CopyFileContents+0x5c>
mov    %rax,%rbx
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    $0x3e7,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11a3 <XTL_CopyFileContents+0x63>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <fputs@plt>
test   %rax,%rax
jg     1170 <XTL_CopyFileContents+0x30>
mov    $0xfffffffe,%eax
jmp    11ad <XTL_CopyFileContents+0x6d>
mov    $0xffffffff,%eax
jmp    11ad <XTL_CopyFileContents+0x6d>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @XTL_CopyFileContents(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca [1000 x i8]
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %29
13: ; preds = %2
br label %14
14: ; preds = %25, %13
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%16 = load i32*, i32** %6
%17 = call i32* @fgets(i8* %15, i32 999, i32* %16)
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%21 = load i32*, i32** %5
%22 = call i64 @fputs(i8* %20, i32* %21)
%23 = icmp sle i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -2, i32* %3
br label %29
25: ; preds = %19
br label %14
26: ; preds = %14
%27 = load i32*, i32** %6
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %3
br label %29
29: ; preds = %26, %24, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @XTL_CopyFileContents(i8* nocapture %0, i32* nocapture %1) {
%3 = alloca [1000 x i8]
%4 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %15, label %7
7: ; preds = %2, %10
%8 = call i32* @fgets(i8* %4, i32 999, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %7
%11 = call i64 @fputs(i8* %4, i32* %1)
%12 = icmp slt i64 %11, 1
br i1 %12, label %15, label %7
13: ; preds = %7
%14 = call i32 @fclose(i32* %5)
br label %15
15: ; preds = %10, %2, %13
%16 = phi i32 [ 0, %13 ], [ -1, %2 ], [ -2, %10 ]
ret i32 %16
}