#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 wchar_t;
typedef int uintmax_t;
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgetnwc(int *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int *realloc(int *, int);
int /*<<< orphan*/ stat(char const *, struct stat *);
wchar_t *wloadfile(const char *in_fname, wchar_t *wbuffer) {
  FILE *infile = fopen(in_fname, "r");
  if (!infile)
    return NULL;
  struct stat infile_st;
  stat(in_fname, &infile_st);
  uintmax_t infile_size = infile_st.st_size;
  if (!wbuffer) {
    wbuffer = calloc(infile_size, sizeof(wchar_t));
  } else {
    wbuffer = realloc(wbuffer, infile_size * sizeof(wchar_t) + 16);
    wbuffer[infile_size + 1] = L'\0';
  }
  fgetnwc(wbuffer, 0, infile);
  fclose(infile);
  return wbuffer;
}


x86 assembly (clang, -O0):
<wloadfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <wloadfile+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    121f <wloadfile+0xbf>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d0 <wloadfile+0x70>
mov    -0x2c(%rbp),%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11ff <wloadfile+0x9f>
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    $0x10,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <fgetnwc@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wloadfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe8b(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11b4 <wloadfile+0x54>
mov    %rax,%r15
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
movslq (%rsp),%r12
test   %r14,%r14
je     11b8 <wloadfile+0x58>
lea    0x10(,%r12,4),%esi
mov    %r14,%rdi
call   1070 <realloc@plt>
mov    %rax,%rbx
movl   $0x0,0x4(%rax,%r12,4)
jmp    11c8 <wloadfile+0x68>
xor    %ebx,%ebx
jmp    11dd <wloadfile+0x7d>
mov    %r12d,%edi
mov    $0x4,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1030 <fgetnwc@plt>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i32* @wloadfile(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = load i8*, i8** %4
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32* null, i32** %3
br label %44
14: ; preds = %2
%15 = load i8*, i8** %4
%16 = call i32 @stat(i8* %15, %struct.stat* %7)
%17 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %8
%19 = load i32*, i32** %5
%20 = icmp ne i32* %19, null
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = load i32, i32* %8
%23 = call i32* @calloc(i32 %22, i32 4)
store i32* %23, i32** %5
br label %37
24: ; preds = %14
%25 = load i32*, i32** %5
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 4
%29 = add i64 %28, 16
%30 = trunc i64 %29 to i32
%31 = call i32* @realloc(i32* %25, i32 %30)
store i32* %31, i32** %5
%32 = load i32*, i32** %5
%33 = load i32, i32* %8
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %32, i64 %35
store i32 0, i32* %36
br label %37
37: ; preds = %24, %21
%38 = load i32*, i32** %5
%39 = load i32*, i32** %6
%40 = call i32 @fgetnwc(i32* %38, i32 0, i32* %39)
%41 = load i32*, i32** %6
%42 = call i32 @fclose(i32* %41)
%43 = load i32*, i32** %5
store i32* %43, i32** %3
br label %44
44: ; preds = %37, %13
%45 = load i32*, i32** %3
ret i32* %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
define i32* @wloadfile(i8* nocapture %0, i32* %1) {
%3 = alloca %struct.stat
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = bitcast %struct.stat* %3 to i8*
%8 = call i32 @stat(i8* %0, %struct.stat* %3)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp eq i32* %1, null
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32* @calloc(i32 %10, i32 4)
br label %21
14: ; preds = %6
%15 = shl i32 %10, 2
%16 = add i32 %15, 16
%17 = tail call i32* @realloc(i32* %1, i32 %16)
%18 = add nsw i32 %10, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
store i32 0, i32* %20
br label %21
21: ; preds = %14, %12
%22 = phi i32* [ %17, %14 ], [ %13, %12 ]
%23 = tail call i32 @fgetnwc(i32* %22, i32 0, i32* %4)
%24 = tail call i32 @fclose(i32* %4)
br label %25
25: ; preds = %2, %21
%26 = phi i32* [ %22, %21 ], [ null, %2 ]
ret i32* %26
}