#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *xmalloc(int);
char *xrealloc(char *, int);
char *cstring_from_file(const char *path) {
  char *text = NULL;
  int size = 255;
  int used = 0;
  int file = -1;
  int rc;
  if (path == 0) {
    fprintf(stderr, "trying to read from NULL path\n");
    goto cleanup;
  }
  if ((file = open(path, O_RDONLY)) == -1) {
    perror(path);
    goto cleanup;
  }
  text = xmalloc(size + 1);
  for (;;) {
    if (used == size) {
      size *= 2;
      text = xrealloc(text, size);
    }
    rc = read(file, text + used, size - used);
    if (rc == 0) {
      break;
    }
    if (rc == -1) {
      perror(path);
      break;
    }
    used += rc;
  }
  text[used] = 0;
cleanup:
  if (file != -1) {
    if (close(file) != 0)
      perror(path);
  }
  return text;
}


x86 assembly (clang, -O0):
<cstring_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0xff,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11be <cstring_from_file+0x4e>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe4c(%rip),%rsi
call   1070 <fprintf@plt>
jmp    127b <cstring_from_file+0x10b>
mov    -0x8(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    11ea <cstring_from_file+0x7a>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
jmp    127b <cstring_from_file+0x10b>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    121e <cstring_from_file+0xae>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <xrealloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1249 <cstring_from_file+0xd9>
jmp    126f <cstring_from_file+0xff>
cmpl   $0xffffffff,-0x20(%rbp)
jne    1261 <cstring_from_file+0xf1>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
jmp    126f <cstring_from_file+0xff>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11f9 <cstring_from_file+0x89>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0xffffffff,-0x1c(%rbp)
je     12a5 <cstring_from_file+0x135>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     12a0 <cstring_from_file+0x130>
mov    -0x8(%rbp),%rdi
call   1090 <perror@plt>
jmp    12a5 <cstring_from_file+0x135>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstring_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11f9 <cstring_from_file+0x89>
mov    %rdi,%r14
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1213 <cstring_from_file+0xa3>
mov    %eax,%r15d
mov    $0x100,%edi
call   1040 <xmalloc@plt>
mov    %rax,%r12
xor    %ebp,%ebp
mov    $0xff,%r13d
cmp    %r13d,%ebp
jne    11d1 <cstring_from_file+0x61>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %r13d,%r13d
mov    %r12,%rdi
mov    %r13d,%esi
call   1030 <xrealloc@plt>
mov    %rax,%r12
movslq %ebp,%rbx
add    %r12,%rbx
mov    %r13d,%edx
sub    %ebp,%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <read@plt>
test   %eax,%eax
je     1228 <cstring_from_file+0xb8>
cmp    $0xffffffff,%eax
je     1220 <cstring_from_file+0xb0>
add    %eax,%ebp
cmp    %r13d,%ebp
je     11c0 <cstring_from_file+0x50>
jmp    11d1 <cstring_from_file+0x61>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1070 <fprintf@plt>
xor    %r12d,%r12d
jmp    1238 <cstring_from_file+0xc8>
mov    %r14,%rdi
call   1090 <perror@plt>
xor    %r12d,%r12d
jmp    1238 <cstring_from_file+0xc8>
mov    %r14,%rdi
call   1090 <perror@plt>
movb   $0x0,(%rbx)
mov    %r15d,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    124a <cstring_from_file+0xda>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
call   1090 <perror@plt>
jmp    1238 <cstring_from_file+0xc8>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"trying to read from NULL path\0A\00"
@O_RDONLY = global i32 0
define i8* @cstring_from_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
store i32 255, i32* %4
store i32 0, i32* %5
store i32 -1, i32* %6
%8 = load i8*, i8** %2
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %63
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %2
%20 = call i32 @perror(i8* %19)
br label %63
21: ; preds = %13
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
%24 = call i8* @xmalloc(i32 %23)
store i8* %24, i8** %3
br label %25
25: ; preds = %54, %21
%26 = load i32, i32* %5
%27 = load i32, i32* %4
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %4
%31 = mul nsw i32 %30, 2
store i32 %31, i32* %4
%32 = load i8*, i8** %3
%33 = load i32, i32* %4
%34 = call i8* @xrealloc(i8* %32, i32 %33)
store i8* %34, i8** %3
br label %35
35: ; preds = %29, %25
%36 = load i32, i32* %6
%37 = load i8*, i8** %3
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i32, i32* %4
%42 = load i32, i32* %5
%43 = sub nsw i32 %41, %42
%44 = call i32 @read(i32 %36, i8* %40, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %35
br label %58
48: ; preds = %35
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %2
%53 = call i32 @perror(i8* %52)
br label %58
54: ; preds = %48
%55 = load i32, i32* %7
%56 = load i32, i32* %5
%57 = add nsw i32 %56, %55
store i32 %57, i32* %5
br label %25
58: ; preds = %51, %47
%59 = load i8*, i8** %3
%60 = load i32, i32* %5
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
store i8 0, i8* %62
br label %63
63: ; preds = %58, %18, %10
%64 = load i32, i32* %6
%65 = icmp ne i32 %64, -1
br i1 %65, label %66, label %74
66: ; preds = %63
%67 = load i32, i32* %6
%68 = call i64 @close(i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = load i8*, i8** %2
%72 = call i32 @perror(i8* %71)
br label %73
73: ; preds = %70, %66
br label %74
74: ; preds = %73, %63
%75 = load i8*, i8** %3
ret i8* %75
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"trying to read from NULL path\0A\00"
@O_RDONLY = global i32 0
define i8* @cstring_from_file(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %6
3: ; preds = %1
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %38
6: ; preds = %1
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @perror(i8* %0)
br label %38
12: ; preds = %6
%13 = tail call i8* @xmalloc(i32 256)
br label %14
14: ; preds = %31, %12
%15 = phi i32 [ 255, %12 ], [ %23, %31 ]
%16 = phi i32 [ 0, %12 ], [ %32, %31 ]
%17 = phi i8* [ %13, %12 ], [ %24, %31 ]
%18 = icmp eq i32 %16, %15
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = shl nsw i32 %15, 1
%21 = tail call i8* @xrealloc(i8* %17, i32 %20)
br label %22
22: ; preds = %19, %14
%23 = phi i32 [ %20, %19 ], [ %15, %14 ]
%24 = phi i8* [ %21, %19 ], [ %17, %14 ]
%25 = sext i32 %16 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = sub nsw i32 %23, %16
%28 = tail call i32 @read(i32 %8, i8* %26, i32 %27)
switch i32 %28, label %31 [
i32 0, label %33
i32 -1, label %29
]
29: ; preds = %22
%30 = tail call i32 @perror(i8* %0)
br label %33
31: ; preds = %22
%32 = add nsw i32 %28, %16
br label %14
33: ; preds = %22, %29
store i8 0, i8* %26
%34 = tail call i64 @close(i32 %8)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 @perror(i8* %0)
br label %38
38: ; preds = %10, %3, %33, %36
%39 = phi i8* [ %24, %33 ], [ %24, %36 ], [ null, %3 ], [ null, %10 ]
ret i8* %39
}