#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ BUFSIZ;
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ cfg_free(char *);
char *cfg_malloc(size_t);
int /*<<< orphan*/ cfg_set_error(char *, ...);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int /*<<< orphan*/);
int stat(char const *, struct stat *);
char *cfg_read_file(const char *path) {
  char *content, *ptr;
  struct stat st;
  size_t sz;
  int fd;
  fd = open(path, O_RDONLY);
  if (fd == -1) {
    cfg_set_error("cannot open file %s: %m", path);
    return NULL;
  }
  if (stat(path, &st) == -1) {
    cfg_set_error("cannot stat file %s: %m", path);
    close(fd);
    return NULL;
  }
  sz = (size_t)st.st_size;
  content = cfg_malloc(sz + 1);
  if (!content) {
    cfg_set_error("cannot allocate %zu bytes: %m", sz);
    close(fd);
    return NULL;
  }
  ptr = content;
  for (;;) {
    ssize_t ret;
    ret = read(fd, ptr, BUFSIZ);
    if (ret < 0) {
      if (errno == EINTR)
        continue;
      cfg_set_error("error while reading %s: %m", path);
      close(fd);
      cfg_free(content);
      return NULL;
    }
    if (ret == 0)
      break;
    ptr += ret;
  }
  *ptr = '\0';
  close(fd);
  return content;
}


x86 assembly (clang, -O0):
<cfg_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11ba <cfg_read_file+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1060 <cfg_set_error@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f5 <cfg_read_file+0x185>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11f7 <cfg_read_file+0x87>
mov    -0x10(%rbp),%rsi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1060 <cfg_set_error@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f5 <cfg_read_file+0x185>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1090 <cfg_malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1242 <cfg_read_file+0xd2>
mov    -0x30(%rbp),%rsi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1060 <cfg_set_error@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f5 <cfg_read_file+0x185>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    12bd <cfg_read_file+0x14d>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    128d <cfg_read_file+0x11d>
jmp    124a <cfg_read_file+0xda>
mov    -0x10(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1060 <cfg_set_error@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <cfg_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12f5 <cfg_read_file+0x185>
cmpq   $0x0,-0x40(%rbp)
jne    12cd <cfg_read_file+0x15d>
jmp    12de <cfg_read_file+0x16e>
mov    -0x40(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    124a <cfg_read_file+0xda>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfg_read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     121c <cfg_read_file+0xac>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     1257 <cfg_read_file+0xe7>
mov    0x10(%rsp),%r14
lea    0x1(%r14),%rdi
call   1090 <cfg_malloc@plt>
test   %rax,%rax
je     1265 <cfg_read_file+0xf5>
mov    0x2e0a(%rip),%r12
mov    0x2dfb(%rip),%r13
mov    %rax,%rcx
mov    0x2de9(%rip),%r14
mov    %rax,0x8(%rsp)
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <read@plt>
test   %rax,%rax
jns    120e <cfg_read_file+0x9e>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     11f0 <cfg_read_file+0x80>
jmp    1231 <cfg_read_file+0xc1>
lea    (%rbx,%rax,1),%rcx
test   %rax,%rax
je     1291 <cfg_read_file+0x121>
mov    %rcx,%rbx
jmp    11f0 <cfg_read_file+0x80>
lea    0xddd(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <cfg_set_error@plt>
jmp    127f <cfg_read_file+0x10f>
lea    0xe16(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <cfg_set_error@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%rdi
call   1030 <cfg_free@plt>
xor    %ebx,%ebx
jmp    127f <cfg_read_file+0x10f>
lea    0xdba(%rip),%rdi
xor    %ebx,%ebx
mov    %r15,%rsi
jmp    1271 <cfg_read_file+0x101>
lea    0xdc4(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <cfg_set_error@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
movb   $0x0,(%rbx)
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x8(%rsp),%rbx
jmp    127f <cfg_read_file+0x10f>

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [24 x i8] c"cannot open file %s: %m\00"
@.str.1 = [24 x i8] c"cannot stat file %s: %m\00"
@.str.2 = [30 x i8] c"cannot allocate %zu bytes: %m\00"
@BUFSIZ = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [27 x i8] c"error while reading %s: %m\00"
define i8* @cfg_read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %3
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16)
store i8* null, i8** %2
br label %74
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i32 @stat(i8* %19, %struct.stat* %6)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
store i8* null, i8** %2
br label %74
27: ; preds = %18
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%29 = load i64, i64* %28
store i64 %29, i64* %7
%30 = load i64, i64* %7
%31 = add i64 %30, 1
%32 = call i8* @cfg_malloc(i64 %31)
store i8* %32, i8** %4
%33 = load i8*, i8** %4
%34 = icmp ne i8* %33, null
br i1 %34, label %40, label %35
35: ; preds = %27
%36 = load i64, i64* %7
%37 = call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 %36)
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
store i8* null, i8** %2
br label %74
40: ; preds = %27
%41 = load i8*, i8** %4
store i8* %41, i8** %5
br label %42
42: ; preds = %65, %53, %40
%43 = load i32, i32* %8
%44 = load i8*, i8** %5
%45 = load i32, i32* @BUFSIZ
%46 = call i64 @read(i32 %43, i8* %44, i32 %45)
store i64 %46, i64* %9
%47 = load i64, i64* %9
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %61
49: ; preds = %42
%50 = load i64, i64* @errno
%51 = load i64, i64* @EINTR
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
br label %42
54: ; preds = %49
%55 = load i8*, i8** %3
%56 = call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i32, i32* %8
%58 = call i32 @close(i32 %57)
%59 = load i8*, i8** %4
%60 = call i32 @cfg_free(i8* %59)
store i8* null, i8** %2
br label %74
61: ; preds = %42
%62 = load i64, i64* %9
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %61
br label %69
65: ; preds = %61
%66 = load i64, i64* %9
%67 = load i8*, i8** %5
%68 = getelementptr inbounds i8, i8* %67, i64 %66
store i8* %68, i8** %5
br label %42
69: ; preds = %64
%70 = load i8*, i8** %5
store i8 0, i8* %70
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
%73 = load i8*, i8** %4
store i8* %73, i8** %2
br label %74
74: ; preds = %69, %54, %35, %22, %15
%75 = load i8*, i8** %2
ret i8* %75
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [24 x i8] c"cannot open file %s: %m\00"
@.str.1 = [24 x i8] c"cannot stat file %s: %m\00"
@.str.2 = [30 x i8] c"cannot allocate %zu bytes: %m\00"
@BUFSIZ = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [27 x i8] c"error while reading %s: %m\00"
define i8* @cfg_read_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %43
9: ; preds = %1
%10 = call i32 @stat(i8* %0, %struct.stat* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32 @close(i32 %5)
br label %43
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i64, i64* %16
%18 = add i64 %17, 1
%19 = tail call i8* @cfg_malloc(i64 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %39
21: ; preds = %15
%22 = tail call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 %17)
%23 = tail call i32 @close(i32 %5)
br label %43
24: ; preds = %39, %28
%25 = load i32, i32* @BUFSIZ
%26 = tail call i64 @read(i32 %5, i8* %40, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp eq i64 %29, %30
br i1 %31, label %24, label %32
32: ; preds = %28
%33 = tail call i32 (i8*, ...) @cfg_set_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%34 = tail call i32 @close(i32 %5)
%35 = tail call i32 @cfg_free(i8* %19)
br label %43
36: ; preds = %24
%37 = icmp eq i64 %26, 0
%38 = getelementptr inbounds i8, i8* %40, i64 %26
br i1 %37, label %41, label %39
39: ; preds = %15, %36
%40 = phi i8* [ %38, %36 ], [ %19, %15 ]
br label %24
41: ; preds = %36
store i8 0, i8* %40
%42 = tail call i32 @close(i32 %5)
br label %43
43: ; preds = %32, %41, %21, %12, %7
%44 = phi i8* [ null, %7 ], [ null, %12 ], [ %19, %41 ], [ null, %21 ], [ null, %32 ]
ret i8* %44
}