#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 /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
size_t fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int unichars(char *);
int main(int argc, char *argv[]) {
  FILE *pFile;
  long lSize;
  char *buffer;
  size_t result;
  int n;
  pFile = fopen(argv[1], "rb");
  if (pFile == NULL) {
    fputs("File error\n", stderr);
    exit(1);
  }
  fseek(pFile, 0, SEEK_END);
  lSize = ftell(pFile);
  rewind(pFile);
  buffer = (char *)malloc(sizeof(char) * (lSize + 1));
  if (buffer == NULL) {
    fputs("Memory error\n", stderr);
    exit(2);
  }
  result = fread(buffer, 1, lSize, pFile);
  if (result != lSize) {
    fputs("Reading error\n", stderr);
    exit(3);
  }
  *(buffer + lSize) = 0;
  printf("file size: %ld\n", lSize);
  n = unichars(buffer);
  printf("Number of unichars: %d\n", n);
  fclose(pFile);
  free(buffer);
  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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1b(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1218 <main+0x58>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0xdfa(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1283 <main+0xc3>
mov    0x2d6d(%rip),%rax
mov    (%rax),%esi
lea    0xd9b(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12ca <main+0x10a>
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
lea    0xd62(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %eax,%esi
lea    0xd49(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   10d0 <unichars@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd3e(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x8(%rsi),%rdi
lea    0xe30(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1270 <main+0xb0>
mov    %rax,%r14
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10b0 <fseek@plt>
mov    %r14,%rdi
call   1090 <ftell@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <rewind@plt>
lea    0x1(%rbx),%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     128f <main+0xcf>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    %rbx,%rax
jne    12ae <main+0xee>
movb   $0x0,(%r15,%rbx,1)
lea    0xdf1(%rip),%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    %r15,%rdi
call   10d0 <unichars@plt>
lea    0xdeb(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
lea    0xd83(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
lea    0xd70(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%esi
lea    0xd5f(%rip),%rdi
call   1080 <fputs@plt>
mov    $0x3,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [12 x i8] c"File error\0A\00"
@stderr = global i32 0
@SEEK_END = global i32 0
@.str.2 = [14 x i8] c"Memory error\0A\00"
@.str.3 = [15 x i8] c"Reading error\0A\00"
@.str.4 = [16 x i8] c"file size: %ld\0A\00"
@.str.5 = [24 x i8] c"Number of unichars: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32*, i32** %6
%23 = load i32, i32* @SEEK_END
%24 = call i32 @fseek(i32* %22, i32 0, i32 %23)
%25 = load i32*, i32** %6
%26 = call i64 @ftell(i32* %25)
store i64 %26, i64* %7
%27 = load i32*, i32** %6
%28 = call i32 @rewind(i32* %27)
%29 = load i64, i64* %7
%30 = add nsw i64 %29, 1
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %41
37: ; preds = %21
%38 = load i32, i32* @stderr
%39 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %38)
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %21
%42 = load i8*, i8** %8
%43 = load i64, i64* %7
%44 = load i32*, i32** %6
%45 = call i64 @fread(i8* %42, i32 1, i64 %43, i32* %44)
store i64 %45, i64* %9
%46 = load i64, i64* %9
%47 = load i64, i64* %7
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = load i32, i32* @stderr
%51 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %50)
%52 = call i32 @exit(i32 3)
unreachable
53: ; preds = %41
%54 = load i8*, i8** %8
%55 = load i64, i64* %7
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 0, i8* %56
%57 = load i64, i64* %7
%58 = trunc i64 %57 to i32
%59 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %58)
%60 = load i8*, i8** %8
%61 = call i32 @unichars(i8* %60)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %62)
%64 = load i32*, i32** %6
%65 = call i32 @fclose(i32* %64)
%66 = load i8*, i8** %8
%67 = call i32 @free(i8* %66)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [12 x i8] c"File error\0A\00"
@stderr = global i32 0
@SEEK_END = global i32 0
@.str.2 = [14 x i8] c"Memory error\0A\00"
@.str.3 = [15 x i8] c"Reading error\0A\00"
@.str.4 = [16 x i8] c"file size: %ld\0A\00"
@.str.5 = [24 x i8] c"Number of unichars: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fputs(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %5, i32 0, i32 %12)
%14 = tail call i64 @ftell(i32* %5)
%15 = tail call i32 @rewind(i32* %5)
%16 = trunc i64 %14 to i32
%17 = add i32 %16, 1
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i64 %18, 0
br i1 %20, label %21, label %25
21: ; preds = %11
%22 = load i32, i32* @stderr
%23 = tail call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %11
%26 = tail call i64 @fread(i8* %19, i32 1, i64 %14, i32* %5)
%27 = icmp eq i64 %26, %14
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = tail call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = tail call i32 @exit(i32 3)
unreachable
32: ; preds = %25
%33 = getelementptr inbounds i8, i8* %19, i64 %14
store i8 0, i8* %33
%34 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %16)
%35 = tail call i32 @unichars(i8* %19)
%36 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = tail call i32 @fclose(i32* %5)
%38 = tail call i32 @free(i8* %19)
ret i32 0
}