#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 EOF;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int SetOrigFileLength(int /*<<< orphan*/ *, int, unsigned long);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int AddTextFile(char *textfile, FILE *ofptr, int pos) {
  int c;
  unsigned long origlen;
  FILE *ifptr;
  printf("Adding text file: %s\n", textfile);
  ifptr = fopen(textfile, "rb");
  if (!ifptr) {
    printf("Cannot open file %s\n", textfile);
    fclose(ifptr);
    return 1;
  }
  fseek(ifptr, 0, SEEK_END);
  origlen = ftell(ifptr);
  fseek(ifptr, 0, SEEK_SET);
  while ((c = fgetc(ifptr)) != EOF)
    if (fputc(c, ofptr) == EOF)
      return 1;
  fclose(ifptr);
  return SetOrigFileLength(ofptr, pos, origlen);
}


x86 assembly (clang, -O0):
<AddTextFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e7 <AddTextFile+0x67>
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    127f <AddTextFile+0xff>
mov    -0x30(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%eax
je     1263 <AddTextFile+0xe3>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <fputc@plt>
mov    0x2d96(%rip),%rcx
cmp    (%rcx),%eax
jne    125e <AddTextFile+0xde>
movl   $0x1,-0x4(%rbp)
jmp    127f <AddTextFile+0xff>
jmp    121c <AddTextFile+0x9c>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1070 <SetOrigFileLength@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AddTextFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
lea    0xe68(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
lea    0xe6f(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     120c <AddTextFile+0x8c>
mov    %rax,%rbp
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbp,%rdi
call   1080 <ftell@plt>
mov    %rax,%r15
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    0x2df1(%rip),%rbx
nop
mov    %rbp,%rdi
call   1050 <fgetc@plt>
cmp    (%rbx),%eax
je     1230 <AddTextFile+0xb0>
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <fputc@plt>
cmp    (%rbx),%eax
jne    11f0 <AddTextFile+0x70>
jmp    1222 <AddTextFile+0xa2>
lea    0xe06(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1030 <fclose@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <SetOrigFileLength@plt>

LLVM IR (-O0):
@.str = [22 x i8] c"Adding text file: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [21 x i8] c"Cannot open file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
define i32 @AddTextFile(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp ne i32* %15, null
br i1 %16, label %22, label %17
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32*, i32** %10
%21 = call i32 @fclose(i32* %20)
store i32 1, i32* %4
br label %51
22: ; preds = %3
%23 = load i32*, i32** %10
%24 = load i32, i32* @SEEK_END
%25 = call i32 @fseek(i32* %23, i32 0, i32 %24)
%26 = load i32*, i32** %10
%27 = call i64 @ftell(i32* %26)
store i64 %27, i64* %9
%28 = load i32*, i32** %10
%29 = load i32, i32* @SEEK_SET
%30 = call i32 @fseek(i32* %28, i32 0, i32 %29)
br label %31
31: ; preds = %43, %22
%32 = load i32*, i32** %10
%33 = call i32 @fgetc(i32* %32)
store i32 %33, i32* %8
%34 = load i32, i32* @EOF
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %44
36: ; preds = %31
%37 = load i32, i32* %8
%38 = load i32*, i32** %6
%39 = call i32 @fputc(i32 %37, i32* %38)
%40 = load i32, i32* @EOF
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 1, i32* %4
br label %51
43: ; preds = %36
br label %31
44: ; preds = %31
%45 = load i32*, i32** %10
%46 = call i32 @fclose(i32* %45)
%47 = load i32*, i32** %6
%48 = load i32, i32* %7
%49 = load i64, i64* %9
%50 = call i32 @SetOrigFileLength(i32* %47, i32 %48, i64 %49)
store i32 %50, i32* %4
br label %51
51: ; preds = %44, %42, %17
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [22 x i8] c"Adding text file: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [21 x i8] c"Cannot open file %s\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@EOF = global i32 0
define i32 @AddTextFile(i8* %0, i32* %1, i32 %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%9 = tail call i32 @fclose(i32* null)
br label %27
10: ; preds = %3
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %5, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %5)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %5, i32 0, i32 %14)
br label %16
16: ; preds = %20, %10
%17 = tail call i32 @fgetc(i32* %5)
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = tail call i32 @fputc(i32 %17, i32* %1)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %27, label %16
24: ; preds = %16
%25 = tail call i32 @fclose(i32* %5)
%26 = tail call i32 @SetOrigFileLength(i32* %1, i32 %2, i64 %13)
br label %27
27: ; preds = %20, %24, %7
%28 = phi i32 [ %26, %24 ], [ 1, %7 ], [ 1, %20 ]
ret i32 %28
}