#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 */
char *calloc(int, int);
int cp2utf(char *, char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int filesize(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf("usage: %s cp1251.txt utf8.txt\n", argv[0]);
    return 1;
  }
  FILE *f = fopen(argv[1], "r");
  if (f == NULL) {
    printf("Can't open file: %s\n", argv[1]);
    return 1;
  }
  int fs = filesize(f);
  char *i = calloc(fs, 1);
  fread(i, 1, fs, f);
  fclose(f);
  char *u = calloc(fs * 6, 1);
  int ws = cp2utf(i, u, fs);
  free(i);
  FILE *o = fopen(argv[2], "w");
  fwrite(u, 1, ws, o);
  fclose(o);
  free(u);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe42(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c1 <main+0x131>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1212 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe20(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c1 <main+0x131>
mov    -0x18(%rbp),%rdi
call   1090 <filesize@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
imul   $0x6,-0x1c(%rbp),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1080 <cp2utf@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdac(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
cmp    $0x3,%edi
jne    125f <main+0xcf>
mov    0x8(%r12),%rdi
lea    0xe6c(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     126c <main+0xdc>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <filesize@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    (%r15,%r15,1),%eax
lea    (%rax,%rax,2),%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r14
mov    %r13,%rdi
mov    %rax,%rsi
mov    %r15d,%edx
call   1080 <cp2utf@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x10(%r12),%rdi
lea    0xe05(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1282 <main+0xf2>
mov    (%r12),%rsi
lea    0xd96(%rip),%rdi
jmp    1278 <main+0xe8>
mov    0x8(%r12),%rsi
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"usage: %s cp1251.txt utf8.txt\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"Can't open file: %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %63
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
br label %63
31: ; preds = %19
%32 = load i32*, i32** %6
%33 = call i32 @filesize(i32* %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = call i8* @calloc(i32 %34, i32 1)
store i8* %35, i8** %8
%36 = load i8*, i8** %8
%37 = load i32, i32* %7
%38 = load i32*, i32** %6
%39 = call i32 @fread(i8* %36, i32 1, i32 %37, i32* %38)
%40 = load i32*, i32** %6
%41 = call i32 @fclose(i32* %40)
%42 = load i32, i32* %7
%43 = mul nsw i32 %42, 6
%44 = call i8* @calloc(i32 %43, i32 1)
store i8* %44, i8** %9
%45 = load i8*, i8** %8
%46 = load i8*, i8** %9
%47 = load i32, i32* %7
%48 = call i32 @cp2utf(i8* %45, i8* %46, i32 %47)
store i32 %48, i32* %10
%49 = load i8*, i8** %8
%50 = call i32 @free(i8* %49)
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %54, i32** %11
%55 = load i8*, i8** %9
%56 = load i32, i32* %10
%57 = load i32*, i32** %11
%58 = call i32 @fwrite(i8* %55, i32 1, i32 %56, i32* %57)
%59 = load i32*, i32** %11
%60 = call i32 @fclose(i32* %59)
%61 = load i8*, i8** %9
%62 = call i32 @free(i8* %61)
store i32 0, i32* %3
br label %63
63: ; preds = %31, %26, %14
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [31 x i8] c"usage: %s cp1251.txt utf8.txt\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"Can't open file: %s\0A\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %30
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = load i8*, i8** %8
%14 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %30
15: ; preds = %7
%16 = tail call i32 @filesize(i32* %10)
%17 = tail call i8* @calloc(i32 %16, i32 1)
%18 = tail call i32 @fread(i8* %17, i32 1, i32 %16, i32* %10)
%19 = tail call i32 @fclose(i32* %10)
%20 = mul nsw i32 %16, 6
%21 = tail call i8* @calloc(i32 %20, i32 1)
%22 = tail call i32 @cp2utf(i8* %17, i8* %21, i32 %16)
%23 = tail call i32 @free(i8* %17)
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = tail call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%27 = tail call i32 @fwrite(i8* %21, i32 1, i32 %22, i32* %26)
%28 = tail call i32 @fclose(i32* %26)
%29 = tail call i32 @free(i8* %21)
br label %30
30: ; preds = %12, %15, %4
%31 = phi i32 [ 1, %4 ], [ 1, %12 ], [ 0, %15 ]
ret i32 %31
}