#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 */
unsigned int CalculateCheckSum(char *);
int CountFiles(int /*<<< orphan*/ *);
scalar_t__ InitialiseHeader(int /*<<< orphan*/ *, int);
scalar_t__ MoveTextFiles(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int CreateHelpFile(char *listfile, char *helpfile) {
  FILE *ifptr, *ofptr;
  int filecount, i;
  unsigned csum;
  ifptr = fopen(listfile, "rb");
  if (!ifptr) {
    printf("Listfile cannot be opened.");
    return 1;
  }
  ofptr = fopen(helpfile, "wb");
  if (!ofptr) {
    fclose(ifptr);
    printf("Unable to open destination help file.");
    return 1;
  }
  filecount = CountFiles(ifptr);
  if (filecount == 0)
    return 0;
  if ((filecount < 0) || (InitialiseHeader(ofptr, filecount)) ||
      (MoveTextFiles(ifptr, ofptr))) {
    printf("Error processing listfile.\n");
    fclose(ifptr);
    fclose(ofptr);
    return 1;
  }
  fclose(ifptr);
  fclose(ofptr);
  csum = CalculateCheckSum(helpfile);
  printf("Checksum = %X\n", csum);
  ofptr = fopen(helpfile, "ab");
  if (!ofptr) {
    fclose(ifptr);
    printf("Unable to open destination help file.");
    return 1;
  }
  fseek(ofptr, 0, SEEK_END);
  for (i = 0; i < sizeof(unsigned); i++)
    fputc(*(((char *)(&csum)) + i), ofptr);
  fclose(ofptr);
  return 0;
}


x86 assembly (clang, -O0):
<CreateHelpFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe55(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d9 <CreateHelpFile+0x49>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136c <CreateHelpFile+0x1dc>
mov    -0x18(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    121b <CreateHelpFile+0x8b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136c <CreateHelpFile+0x1dc>
mov    -0x20(%rbp),%rdi
call   10b0 <CountFiles@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    123d <CreateHelpFile+0xad>
movl   $0x0,-0x4(%rbp)
jmp    136c <CreateHelpFile+0x1dc>
cmpl   $0x0,-0x2c(%rbp)
jl     1274 <CreateHelpFile+0xe4>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <InitialiseHeader@plt>
cmp    $0x0,%rax
jne    1274 <CreateHelpFile+0xe4>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <MoveTextFiles@plt>
cmp    $0x0,%rax
je     12a0 <CreateHelpFile+0x110>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    136c <CreateHelpFile+0x1dc>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <CalculateCheckSum@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1311 <CreateHelpFile+0x181>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136c <CreateHelpFile+0x1dc>
mov    -0x28(%rbp),%rdi
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
movl   $0x0,-0x30(%rbp)
movslq -0x30(%rbp),%rax
cmp    $0x4,%rax
jae    135c <CreateHelpFile+0x1cc>
movslq -0x30(%rbp),%rax
mov    -0x34(%rbp,%rax,1),%al
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1070 <fputc@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    132c <CreateHelpFile+0x19c>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CreateHelpFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1228 <CreateHelpFile+0x88>
mov    %rax,%rbx
lea    0xe58(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12db <CreateHelpFile+0x13b>
mov    %rax,%rbp
mov    %rbx,%rdi
call   10c0 <CountFiles@plt>
test   %eax,%eax
je     1234 <CreateHelpFile+0x94>
js     1207 <CreateHelpFile+0x67>
mov    %rbp,%rdi
mov    %eax,%esi
call   1030 <InitialiseHeader@plt>
test   %rax,%rax
jne    1207 <CreateHelpFile+0x67>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <MoveTextFiles@plt>
test   %rax,%rax
je     123b <CreateHelpFile+0x9b>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
jmp    12f1 <CreateHelpFile+0x151>
lea    0xdd4(%rip),%rdi
jmp    12ea <CreateHelpFile+0x14a>
xor    %ebp,%ebp
jmp    12f6 <CreateHelpFile+0x156>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %rbp,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1050 <CalculateCheckSum@plt>
mov    %eax,%r15d
lea    0xdea(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xde7(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12db <CreateHelpFile+0x13b>
mov    %rax,%r14
mov    %r15d,%r12d
shr    $0x10,%r12d
mov    %r15d,%ebx
shr    $0x8,%ebx
mov    0x2d55(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
movsbl %r15b,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
movsbl %bl,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
movsbl %r12b,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
sar    $0x18,%r15d
mov    %r15d,%edi
mov    %r14,%rsi
call   1080 <fputc@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
jmp    12f6 <CreateHelpFile+0x156>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xd37(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [27 x i8] c"Listfile cannot be opened.\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [38 x i8] c"Unable to open destination help file.\00"
@.str.4 = [28 x i8] c"Error processing listfile.\0A\00"
@.str.5 = [15 x i8] c"Checksum = %X\0A\00"
@.str.6 = [3 x i8] c"ab\00"
@SEEK_END = global i32 0
define i32 @CreateHelpFile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %90
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %7
%20 = load i32*, i32** %7
%21 = icmp ne i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %17
%23 = load i32*, i32** %6
%24 = call i32 @fclose(i32* %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %90
26: ; preds = %17
%27 = load i32*, i32** %6
%28 = call i32 @CountFiles(i32* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 0, i32* %3
br label %90
32: ; preds = %26
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 0
br i1 %34, label %45, label %35
35: ; preds = %32
%36 = load i32*, i32** %7
%37 = load i32, i32* %8
%38 = call i64 @InitialiseHeader(i32* %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %45, label %40
40: ; preds = %35
%41 = load i32*, i32** %6
%42 = load i32*, i32** %7
%43 = call i64 @MoveTextFiles(i32* %41, i32* %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %40, %35, %32
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = load i32*, i32** %7
%50 = call i32 @fclose(i32* %49)
store i32 1, i32* %3
br label %90
51: ; preds = %40
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %7
%55 = call i32 @fclose(i32* %54)
%56 = load i8*, i8** %5
%57 = call i32 @CalculateCheckSum(i8* %56)
store i32 %57, i32* %10
%58 = load i32, i32* %10
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %58)
%60 = load i8*, i8** %5
%61 = call i32* @fopen(i8* %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i32* %61, i32** %7
%62 = load i32*, i32** %7
%63 = icmp ne i32* %62, null
br i1 %63, label %68, label %64
64: ; preds = %51
%65 = load i32*, i32** %6
%66 = call i32 @fclose(i32* %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %90
68: ; preds = %51
%69 = load i32*, i32** %7
%70 = load i32, i32* @SEEK_END
%71 = call i32 @fseek(i32* %69, i32 0, i32 %70)
store i32 0, i32* %9
br label %72
72: ; preds = %84, %68
%73 = load i32, i32* %9
%74 = sext i32 %73 to i64
%75 = icmp ult i64 %74, 4
br i1 %75, label %76, label %87
76: ; preds = %72
%77 = bitcast i32* %10 to i8*
%78 = load i32, i32* %9
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
%81 = load i8, i8* %80
%82 = load i32*, i32** %7
%83 = call i32 @fputc(i8 signext %81, i32* %82)
br label %84
84: ; preds = %76
%85 = load i32, i32* %9
%86 = add nsw i32 %85, 1
store i32 %86, i32* %9
br label %72
87: ; preds = %72
%88 = load i32*, i32** %7
%89 = call i32 @fclose(i32* %88)
store i32 0, i32* %3
br label %90
90: ; preds = %87, %64, %45, %31, %22, %15
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [27 x i8] c"Listfile cannot be opened.\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [38 x i8] c"Unable to open destination help file.\00"
@.str.5 = [15 x i8] c"Checksum = %X\0A\00"
@.str.6 = [3 x i8] c"ab\00"
@SEEK_END = global i32 0
@str = [27 x i8] c"Error processing listfile.\00"
define i32 @CreateHelpFile(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %53
7: ; preds = %2
%8 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = tail call i32 @fclose(i32* %3)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %53
13: ; preds = %7
%14 = tail call i32 @CountFiles(i32* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %53, label %16
16: ; preds = %13
%17 = icmp slt i32 %14, 0
br i1 %17, label %24, label %18
18: ; preds = %16
%19 = tail call i64 @InitialiseHeader(i32* %8, i32 %14)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i64 @MoveTextFiles(i32* %3, i32* %8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %21, %18, %16
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @fclose(i32* %3)
%27 = tail call i32 @fclose(i32* %8)
br label %53
28: ; preds = %21
%29 = tail call i32 @fclose(i32* %3)
%30 = tail call i32 @fclose(i32* %8)
%31 = tail call i32 @CalculateCheckSum(i8* %1)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = tail call i32 @fclose(i32* %3)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %53
38: ; preds = %28
%39 = lshr i32 %31, 24
%40 = trunc i32 %39 to i8
%41 = lshr i32 %31, 16
%42 = trunc i32 %41 to i8
%43 = lshr i32 %31, 8
%44 = trunc i32 %43 to i8
%45 = trunc i32 %31 to i8
%46 = load i32, i32* @SEEK_END
%47 = tail call i32 @fseek(i32* %33, i32 0, i32 %46)
%48 = tail call i32 @fputc(i8 signext %45, i32* %33)
%49 = tail call i32 @fputc(i8 signext %44, i32* %33)
%50 = tail call i32 @fputc(i8 signext %42, i32* %33)
%51 = tail call i32 @fputc(i8 signext %40, i32* %33)
%52 = tail call i32 @fclose(i32* %33)
br label %53
53: ; preds = %13, %38, %35, %24, %10, %5
%54 = phi i32 [ 1, %24 ], [ 0, %38 ], [ 1, %35 ], [ 1, %10 ], [ 1, %5 ], [ 0, %13 ]
ret i32 %54
}