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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ log_dump(char *, int /*<<< orphan*/, int);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
void subfunc() {
  FILE *fp = NULL;
  struct stat stat_buf;
  char *p_file_buf = NULL;
  char buf[256] = {0};
  int read_size = 0;
  int total_read_size = 0;
  ;
  if (0 != stat("test.txt", &stat_buf)) {
    ;
    goto exit;
  };
  if (0 >= stat_buf.st_size) {
    ;
    goto exit;
  }
  p_file_buf = (char *)malloc(stat_buf.st_size);
  fp = fopen("test.txt", "r");
  if (NULL == fp) {
    ;
    goto exit;
  }
  while (0 < (read_size = fread(buf, 1, sizeof(buf), fp))) {
    memcpy(&p_file_buf[total_read_size], buf, read_size);
    total_read_size += read_size;
  }
  log_dump(p_file_buf, 0, total_read_size - 1);
exit:
  if (NULL != fp) {
    fclose(fp);
  }
  if (NULL != p_file_buf) {
    free(p_file_buf);
  };
  return;
}


x86 assembly (clang, -O0):
<subfunc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
movl   $0x0,-0x124(%rbp)
movl   $0x0,-0x128(%rbp)
lea    0xe27(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1070 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11f5 <subfunc+0x65>
jmp    12b7 <subfunc+0x127>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jl     1206 <subfunc+0x76>
jmp    12b7 <subfunc+0x127>
mov    -0x10(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    0xde6(%rip),%rdi
lea    0xde8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x8(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
jne    123b <subfunc+0xab>
jmp    12b7 <subfunc+0x127>
jmp    1240 <subfunc+0xb0>
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1040 <fread@plt>
mov    %eax,%ecx
mov    %ecx,-0x124(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jge    12a3 <subfunc+0x113>
mov    -0x18(%rbp),%rdi
movslq -0x128(%rbp),%rax
add    %rax,%rdi
lea    -0x120(%rbp),%rsi
mov    -0x124(%rbp),%edx
call   1080 <memcpy@plt>
mov    -0x124(%rbp),%eax
add    -0x128(%rbp),%eax
mov    %eax,-0x128(%rbp)
jmp    1240 <subfunc+0xb0>
mov    -0x18(%rbp),%rdi
mov    -0x128(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   10b0 <log_dump@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
je     12cc <subfunc+0x13c>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     12e1 <subfunc+0x151>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<subfunc>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xdfc(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
jne    12c9 <subfunc+0x149>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
jle    12c9 <subfunc+0x149>
call   1080 <malloc@plt>
mov    %rax,%r14
lea    0xdcc(%rip),%rdi
lea    0xdce(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12bc <subfunc+0x13c>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    12a3 <subfunc+0x123>
mov    %eax,%ebp
xor    %ebx,%ebx
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1070 <memcpy@plt>
add    %ebp,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1270 <subfunc+0xf0>
add    $0xffffffff,%ebx
jmp    12a8 <subfunc+0x128>
mov    $0xffffffff,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   10a0 <log_dump@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
test   %r14,%r14
je     12c9 <subfunc+0x149>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
define void @subfunc() {
%1 = alloca i32*
%2 = alloca %struct.stat
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = alloca i32
store i32* null, i32** %1
store i8* null, i8** %3
%7 = bitcast [256 x i8]* %4 to i8*
store i32 0, i32* %5
store i32 0, i32* %6
%8 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%9 = icmp ne i64 0, %8
br i1 %9, label %10, label %11
10: ; preds = %0
br label %47
11: ; preds = %0
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%13 = load i64, i64* %12
%14 = icmp sge i64 0, %13
br i1 %14, label %15, label %16
15: ; preds = %11
br label %47
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%18 = load i64, i64* %17
%19 = call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %3
%21 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %1
%22 = load i32*, i32** %1
%23 = icmp eq i32* null, %22
br i1 %23, label %24, label %25
24: ; preds = %16
br label %47
25: ; preds = %16
br label %26
26: ; preds = %31, %25
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%28 = load i32*, i32** %1
%29 = call i32 @fread(i8* %27, i32 1, i32 256, i32* %28)
store i32 %29, i32* %5
%30 = icmp slt i32 0, %29
br i1 %30, label %31, label %42
31: ; preds = %26
%32 = load i8*, i8** %3
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%37 = load i32, i32* %5
%38 = call i32 @memcpy(i8* %35, i8* %36, i32 %37)
%39 = load i32, i32* %5
%40 = load i32, i32* %6
%41 = add nsw i32 %40, %39
store i32 %41, i32* %6
br label %26
42: ; preds = %26
%43 = load i8*, i8** %3
%44 = load i32, i32* %6
%45 = sub nsw i32 %44, 1
%46 = call i32 @log_dump(i8* %43, i32 0, i32 %45)
br label %47
47: ; preds = %42, %24, %15, %10
%48 = load i32*, i32** %1
%49 = icmp ne i32* null, %48
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32*, i32** %1
%52 = call i32 @fclose(i32* %51)
br label %53
53: ; preds = %50, %47
%54 = load i8*, i8** %3
%55 = icmp ne i8* null, %54
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i8*, i8** %3
%58 = call i32 @free(i8* %57)
br label %59
59: ; preds = %56, %53
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [2 x i8] c"r\00"
define void @subfunc() {
%1 = alloca %struct.stat
%2 = alloca [256 x i8]
%3 = bitcast %struct.stat* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = call i64 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%6 = icmp ne i64 %5, 0
%7 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%8 = load i64, i64* %7
%9 = icmp slt i64 %8, 1
%10 = select i1 %6, i1 true, i1 %9
br i1 %10, label %38, label %11
11: ; preds = %0
%12 = call i64 @malloc(i64 %8)
%13 = inttoptr i64 %12 to i8*
%14 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %34, label %16
16: ; preds = %11
%17 = call i32 @fread(i8* %4, i32 1, i32 256, i32* %14)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %16, %19
%20 = phi i32 [ %26, %19 ], [ %17, %16 ]
%21 = phi i32 [ %25, %19 ], [ 0, %16 ]
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %13, i64 %22
%24 = call i32 @memcpy(i8* %23, i8* %4, i32 %20)
%25 = add nsw i32 %20, %21
%26 = call i32 @fread(i8* %4, i32 1, i32 256, i32* %14)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %19, label %28
28: ; preds = %19
%29 = add nsw i32 %25, -1
br label %30
30: ; preds = %28, %16
%31 = phi i32 [ -1, %16 ], [ %29, %28 ]
%32 = call i32 @log_dump(i8* %13, i32 0, i32 %31)
%33 = call i32 @fclose(i32* %14)
br label %34
34: ; preds = %11, %30
%35 = icmp eq i64 %12, 0
br i1 %35, label %38, label %36
36: ; preds = %34
%37 = call i32 @free(i8* %13)
br label %38
38: ; preds = %0, %36, %34
ret void
}