#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*/ path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int ft_read_file(char *ptr, size_t size, char *dir, char *fn) {
  char path[256];
  FILE *f = NULL;
  int ret;
  int success = 0;
  do {
    snprintf(path, sizeof(path), "%s/%s", dir, fn);
    f = fopen(path, "r");
    if (!f)
      break;
    ret = fread(ptr, 1, size - 1, f);
    ptr[ret] = '\0';
    if (!ret)
      break;
    success = 1;
  } while (0);
  if (f)
    fclose(f);
  if (!success) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<ft_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x138(%rbp)
movl   $0x0,-0x140(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    $0x100,%esi
lea    0xe75(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x130(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    11bd <ft_read_file+0x7d>
jmp    120b <ft_read_file+0xcb>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
mov    -0x138(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x13c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x13c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x13c(%rbp)
jne    1201 <ft_read_file+0xc1>
jmp    120b <ft_read_file+0xcb>
movl   $0x1,-0x140(%rbp)
cmpq   $0x0,-0x138(%rbp)
je     1225 <ft_read_file+0xe5>
mov    -0x138(%rbp),%rdi
call   1040 <fclose@plt>
cmpl   $0x0,-0x140(%rbp)
jne    123e <ft_read_file+0xfe>
movl   $0x0,-0x4(%rbp)
jmp    1245 <ft_read_file+0x105>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ft_read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rcx,%r8
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xea0(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
lea    0xe8f(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b8 <ft_read_file+0x78>
mov    %rax,%rbp
add    $0xffffffffffffffff,%rbx
mov    $0x1,%r15d
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fread@plt>
movslq %eax,%rbx
movb   $0x0,(%r14,%rbx,1)
mov    %rbp,%rdi
call   1040 <fclose@plt>
test   %ebx,%ebx
jne    11bb <ft_read_file+0x7b>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @ft_read_file(i8* nocapture %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %2, i8* %3)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %17, label %10
10: ; preds = %4
%11 = add i64 %1, -1
%12 = call i32 @fread(i8* %0, i32 1, i64 %11, i32* %8)
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %14
%15 = icmp eq i32 %12, 0
%16 = call i32 @fclose(i32* %8)
br i1 %15, label %17, label %18
17: ; preds = %4, %10
br label %18
18: ; preds = %10, %17
%19 = phi i32 [ 0, %17 ], [ 1, %10 ]
ret i32 %19
}