#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*/ UFILE;
typedef scalar_t__ UChar;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fseek(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ u_fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ u_feof(int /*<<< orphan*/ *);
scalar_t__ u_fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ u_fgetfile(int /*<<< orphan*/ *);
int /*<<< orphan*/ *u_fopen(char const *, char *, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ u_frewind(int /*<<< orphan*/ *);
UChar *read_utf8_file(const char *filename, long *size) {
  UChar *str;
  UFILE *f = u_fopen(filename, "r", NULL, "UTF-8");
  long fsize;
  fseek(u_fgetfile(f), 0, SEEK_END);
  fsize = ftell(u_fgetfile(f));
  u_frewind(f);
  str = (UChar *)malloc(sizeof(UChar) * (fsize + 1));
  for ((*size) = 0; !u_feof(f); ++(*size)) {
    str[*size] = u_fgetc(f);
  }
  str[*size] = 0;
  u_fclose(f);
  return str;
}


x86 assembly (clang, -O0):
<read_utf8_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe55(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    0xe4c(%rip),%rcx
call   10a0 <u_fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <u_fgetfile@plt>
mov    %eax,%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <u_fgetfile@plt>
mov    %eax,%edi
call   1050 <ftell@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <u_frewind@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1090 <u_feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1237 <read_utf8_file+0xa7>
jmp    1265 <read_utf8_file+0xd5>
mov    -0x20(%rbp),%rdi
call   1070 <u_fgetc@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    1219 <read_utf8_file+0x89>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x20(%rbp),%rdi
call   1040 <u_fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_utf8_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
lea    0xe61(%rip),%rsi
lea    0xe5c(%rip),%rcx
xor    %edx,%edx
call   10a0 <u_fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <u_fgetfile@plt>
mov    0x2e29(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   10b0 <u_fgetfile@plt>
mov    %eax,%edi
call   1050 <ftell@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <u_frewind@plt>
lea    0x8(,%r15,8),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
movq   $0x0,(%r14)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <u_feof@plt>
test   %eax,%eax
jne    1221 <read_utf8_file+0x91>
mov    %rbx,%rdi
call   1070 <u_fgetc@plt>
mov    (%r14),%rcx
mov    %rax,(%r15,%rcx,8)
addq   $0x1,(%r14)
jmp    1200 <read_utf8_file+0x70>
mov    (%r14),%rax
movq   $0x0,(%r15,%rax,8)
mov    %rbx,%rdi
call   1040 <u_fclose@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@SEEK_END = global i32 0
define i64* @read_utf8_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i64*
%6 = alloca i32*
%7 = alloca i64
store i8* %0, i8** %3
store i64* %1, i64** %4
%8 = load i8*, i8** %3
%9 = call i32* @u_fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = call i32 @u_fgetfile(i32* %10)
%12 = load i32, i32* @SEEK_END
%13 = call i32 @fseek(i32 %11, i32 0, i32 %12)
%14 = load i32*, i32** %6
%15 = call i32 @u_fgetfile(i32* %14)
%16 = call i64 @ftell(i32 %15)
store i64 %16, i64* %7
%17 = load i32*, i32** %6
%18 = call i32 @u_frewind(i32* %17)
%19 = load i64, i64* %7
%20 = add nsw i64 %19, 1
%21 = mul i64 8, %20
%22 = trunc i64 %21 to i32
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i64*
store i64* %24, i64** %5
%25 = load i64*, i64** %4
store i64 0, i64* %25
br label %26
26: ; preds = %38, %2
%27 = load i32*, i32** %6
%28 = call i32 @u_feof(i32* %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = load i32*, i32** %6
%33 = call i64 @u_fgetc(i32* %32)
%34 = load i64*, i64** %5
%35 = load i64*, i64** %4
%36 = load i64, i64* %35
%37 = getelementptr inbounds i64, i64* %34, i64 %36
store i64 %33, i64* %37
br label %38
38: ; preds = %31
%39 = load i64*, i64** %4
%40 = load i64, i64* %39
%41 = add nsw i64 %40, 1
store i64 %41, i64* %39
br label %26
42: ; preds = %26
%43 = load i64*, i64** %5
%44 = load i64*, i64** %4
%45 = load i64, i64* %44
%46 = getelementptr inbounds i64, i64* %43, i64 %45
store i64 0, i64* %46
%47 = load i32*, i32** %6
%48 = call i32 @u_fclose(i32* %47)
%49 = load i64*, i64** %5
ret i64* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"UTF-8\00"
@SEEK_END = global i32 0
define i64* @read_utf8_file(i8* %0, i64* nocapture %1) {
%3 = tail call i32* @u_fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 @u_fgetfile(i32* %3)
%5 = load i32, i32* @SEEK_END
%6 = tail call i32 @fseek(i32 %4, i32 0, i32 %5)
%7 = tail call i32 @u_fgetfile(i32* %3)
%8 = tail call i64 @ftell(i32 %7)
%9 = tail call i32 @u_frewind(i32* %3)
%10 = trunc i64 %8 to i32
%11 = shl i32 %10, 3
%12 = add i32 %11, 8
%13 = tail call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i64*
store i64 0, i64* %1
%15 = tail call i32 @u_feof(i32* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %2, %17
%18 = tail call i64 @u_fgetc(i32* %3)
%19 = load i64, i64* %1
%20 = getelementptr inbounds i64, i64* %14, i64 %19
store i64 %18, i64* %20
%21 = load i64, i64* %1
%22 = add nsw i64 %21, 1
store i64 %22, i64* %1
%23 = tail call i32 @u_feof(i32* %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %17, label %25
25: ; preds = %17, %2
%26 = load i64, i64* %1
%27 = getelementptr inbounds i64, i64* %14, i64 %26
store i64 0, i64* %27
%28 = tail call i32 @u_fclose(i32* %3)
ret i64* %14
}