#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 */
struct stCharsetProcessor {
  int dummy;
};
struct stCharset {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *append(char *, char *);
int charsetProcessorWrite(struct stCharsetProcessor *, struct stCharset *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int writeCharset(struct stCharsetProcessor *charsetProcessor,
                 struct stCharset *charset, char *pcxFilename, int verbose) {
  int i = 0;
  char *chrFilename = NULL;
  char *clrFilename = NULL;
  FILE *chrFile = NULL;
  FILE *clrFile = NULL;
  chrFilename = append(pcxFilename, ".chr");
  clrFilename = append(pcxFilename, ".clr");
  if (verbose)
    printf("Writing output files %s, %s...\n", chrFilename, clrFilename);
  if (!(chrFile = fopen(chrFilename, "wb"))) {
    printf("ERROR: Could not create %s.\n", chrFilename);
    i = 31;
    goto out;
  }
  if (!(clrFile = fopen(clrFilename, "wb"))) {
    printf("ERROR: Could not create %s.\n", clrFilename);
    i = 32;
    goto out;
  }
  if ((i = charsetProcessorWrite(charsetProcessor, charset, chrFile,
                                 clrFile))) {
    printf("ERROR: Failed writing %s and/or %s.\n", chrFilename, clrFilename);
    goto out;
  }
out:
  if (chrFilename)
    free(chrFilename);
  if (clrFilename)
    free(clrFilename);
  if (chrFile)
    fclose(chrFile);
  if (clrFile)
    fclose(clrFile);
  return i;
}


x86 assembly (clang, -O0):
<writeCharset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1040 <append@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1040 <append@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11e6 <writeCharset+0x86>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    1222 <writeCharset+0xc2>
mov    -0x28(%rbp),%rsi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1f,-0x20(%rbp)
jmp    129f <writeCharset+0x13f>
mov    -0x30(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    125e <writeCharset+0xfe>
mov    -0x30(%rbp),%rsi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x20,-0x20(%rbp)
jmp    129f <writeCharset+0x13f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1070 <charsetProcessorWrite@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     129a <writeCharset+0x13a>
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    129f <writeCharset+0x13f>
jmp    129f <writeCharset+0x13f>
cmpq   $0x0,-0x28(%rbp)
je     12b3 <writeCharset+0x153>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x30(%rbp)
je     12c7 <writeCharset+0x167>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
je     12db <writeCharset+0x17b>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x40(%rbp)
je     12ef <writeCharset+0x18f>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeCharset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,(%rsp)
lea    0xe82(%rip),%rsi
mov    %rdx,%rdi
call   1040 <append@plt>
mov    %rax,%r13
lea    0xe75(%rip),%rsi
mov    %rbx,%rdi
call   1040 <append@plt>
mov    %rax,%r14
test   %ebp,%ebp
je     11b3 <writeCharset+0x53>
lea    0xe64(%rip),%rdi
mov    %r13,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe70(%rip),%rsi
mov    %r13,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     121b <writeCharset+0xbb>
mov    %r12,%rbx
lea    0xe56(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     123b <writeCharset+0xdb>
mov    %rax,%r12
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rax,%rcx
call   1070 <charsetProcessorWrite@plt>
test   %eax,%eax
je     1299 <writeCharset+0x139>
mov    %eax,%ebp
lea    0xe43(%rip),%rdi
mov    %r13,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
test   %r13,%r13
jne    1259 <writeCharset+0xf9>
jmp    1261 <writeCharset+0x101>
lea    0xe0b(%rip),%rdi
xor    %r12d,%r12d
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1f,%ebp
test   %r13,%r13
jne    1259 <writeCharset+0xf9>
jmp    1261 <writeCharset+0x101>
lea    0xdeb(%rip),%rdi
xor    %r12d,%r12d
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x20,%ebp
test   %r13,%r13
je     1261 <writeCharset+0x101>
mov    %r13,%rdi
call   1030 <free@plt>
test   %r14,%r14
je     126e <writeCharset+0x10e>
mov    %r14,%rdi
call   1030 <free@plt>
test   %r15,%r15
je     127b <writeCharset+0x11b>
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %r12,%r12
je     1288 <writeCharset+0x128>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
test   %r13,%r13
jne    1259 <writeCharset+0xf9>
jmp    1261 <writeCharset+0x101>

LLVM IR (-O0):
%struct.stCharsetProcessor = type { i32 }
%struct.stCharset = type { i32 }
@.str = [5 x i8] c".chr\00"
@.str.1 = [5 x i8] c".clr\00"
@.str.2 = [32 x i8] c"Writing output files %s, %s...\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [29 x i8] c"ERROR: Could not create %s.\0A\00"
@.str.5 = [37 x i8] c"ERROR: Failed writing %s and/or %s.\0A\00"
define i32 @writeCharset(%struct.stCharsetProcessor* %0, %struct.stCharset* %1, i8* %2, i32 %3) {
%5 = alloca %struct.stCharsetProcessor*
%6 = alloca %struct.stCharset*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32*
store %struct.stCharsetProcessor* %0, %struct.stCharsetProcessor** %5
store %struct.stCharset* %1, %struct.stCharset** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
store i8* null, i8** %10
store i8* null, i8** %11
store i32* null, i32** %12
store i32* null, i32** %13
%14 = load i8*, i8** %7
%15 = call i8* @append(i8* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %15, i8** %10
%16 = load i8*, i8** %7
%17 = call i8* @append(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %11
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = load i8*, i8** %10
%22 = load i8*, i8** %11
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %21, i8* %22)
br label %24
24: ; preds = %20, %4
%25 = load i8*, i8** %10
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %12
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %24
%29 = load i8*, i8** %10
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %29)
store i32 31, i32* %9
br label %50
31: ; preds = %24
%32 = load i8*, i8** %11
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %33, i32** %13
%34 = icmp ne i32* %33, null
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i8*, i8** %11
%37 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %36)
store i32 32, i32* %9
br label %50
38: ; preds = %31
%39 = load %struct.stCharsetProcessor*, %struct.stCharsetProcessor** %5
%40 = load %struct.stCharset*, %struct.stCharset** %6
%41 = load i32*, i32** %12
%42 = load i32*, i32** %13
%43 = call i32 @charsetProcessorWrite(%struct.stCharsetProcessor* %39, %struct.stCharset* %40, i32* %41, i32* %42)
store i32 %43, i32* %9
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %38
%46 = load i8*, i8** %10
%47 = load i8*, i8** %11
%48 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %46, i8* %47)
br label %50
49: ; preds = %38
br label %50
50: ; preds = %49, %45, %35, %28
%51 = load i8*, i8** %10
%52 = icmp ne i8* %51, null
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i8*, i8** %10
%55 = call i32 @free(i8* %54)
br label %56
56: ; preds = %53, %50
%57 = load i8*, i8** %11
%58 = icmp ne i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i8*, i8** %11
%61 = call i32 @free(i8* %60)
br label %62
62: ; preds = %59, %56
%63 = load i32*, i32** %12
%64 = icmp ne i32* %63, null
br i1 %64, label %65, label %68
65: ; preds = %62
%66 = load i32*, i32** %12
%67 = call i32 @fclose(i32* %66)
br label %68
68: ; preds = %65, %62
%69 = load i32*, i32** %13
%70 = icmp ne i32* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32*, i32** %13
%73 = call i32 @fclose(i32* %72)
br label %74
74: ; preds = %71, %68
%75 = load i32, i32* %9
ret i32 %75
}

LLVM IR (-O2):
%struct.stCharsetProcessor = type { i32 }
%struct.stCharset = type { i32 }
@.str = [5 x i8] c".chr\00"
@.str.1 = [5 x i8] c".clr\00"
@.str.2 = [32 x i8] c"Writing output files %s, %s...\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [29 x i8] c"ERROR: Could not create %s.\0A\00"
@.str.5 = [37 x i8] c"ERROR: Failed writing %s and/or %s.\0A\00"
define i32 @writeCharset(%struct.stCharsetProcessor* %0, %struct.stCharset* %1, i8* %2, i32 %3) {
%5 = tail call i8* @append(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i8* @append(i8* %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32 %3, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %6)
br label %10
10: ; preds = %8, %4
%11 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %5)
br label %25
15: ; preds = %10
%16 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %25
20: ; preds = %15
%21 = tail call i32 @charsetProcessorWrite(%struct.stCharsetProcessor* %0, %struct.stCharset* %1, i32* %11, i32* %16)
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i8* %5, i8* %6)
br label %25
25: ; preds = %20, %23, %18, %13
%26 = phi i32 [ %21, %23 ], [ 0, %20 ], [ 32, %18 ], [ 31, %13 ]
%27 = phi i32* [ %16, %23 ], [ %16, %20 ], [ null, %18 ], [ null, %13 ]
%28 = icmp eq i8* %5, null
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = tail call i32 @free(i8* %5)
br label %31
31: ; preds = %29, %25
%32 = icmp eq i8* %6, null
br i1 %32, label %35, label %33
33: ; preds = %31
%34 = tail call i32 @free(i8* %6)
br label %35
35: ; preds = %33, %31
br i1 %12, label %38, label %36
36: ; preds = %35
%37 = tail call i32 @fclose(i32* %11)
br label %38
38: ; preds = %36, %35
%39 = icmp eq i32* %27, null
br i1 %39, label %42, label %40
40: ; preds = %38
%41 = tail call i32 @fclose(i32* %27)
br label %42
42: ; preds = %40, %38
ret i32 %26
}