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

/* Variables and functions */
int EIO;
int ENOMEM;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
long fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
char *ndt_file_slurp(const char *name, int *p) {
  long len;
  int ret = 0;
  char *file = NULL;
  FILE *fdes = fopen(name ? name : "", "rb");
  if (!fdes) {
    ret = errno;
    goto end;
  }
  if (fseek(fdes, 0L, SEEK_END) < 0) {
    ret = errno;
    goto end;
  }
  if ((len = ftell(fdes)) < 0) {
    ret = errno;
    goto end;
  }
  if (fseek(fdes, 0L, SEEK_SET) < 0) {
    ret = errno;
    goto end;
  }
  file = malloc(len + 2);
  if (!file) {
    ret = ENOMEM;
    goto end;
  }
  if (fread(file, 1, len, fdes) != len) {
    ret = EIO;
    goto end;
  }
  if (file[len - 1] != '\n') {
    file[len++] = '\n';
  }
  file[len] = '\0';
  ret = 0;
end:
  if (ret) {
    free(file);
    file = NULL;
  }
  if (p)
    *p = ret;
  fclose(fdes);
  return file;
}


x86 assembly (clang, -O0):
<ndt_file_slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a7 <ndt_file_slurp+0x37>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11b7 <ndt_file_slurp+0x47>
lea    0xe54(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    11b7 <ndt_file_slurp+0x47>
mov    -0x38(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e7 <ndt_file_slurp+0x77>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <ndt_file_slurp+0x1a2>
mov    -0x30(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    1218 <ndt_file_slurp+0xa8>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <ndt_file_slurp+0x1a2>
mov    -0x30(%rbp),%rdi
call   1060 <ftell@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1240 <ndt_file_slurp+0xd0>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <ndt_file_slurp+0x1a2>
mov    -0x30(%rbp),%rdi
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
cmp    $0x0,%rax
jge    1271 <ndt_file_slurp+0x101>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <ndt_file_slurp+0x1a2>
mov    -0x18(%rbp),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    129e <ndt_file_slurp+0x12e>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <ndt_file_slurp+0x1a2>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x18(%rbp),%rax
je     12cf <ndt_file_slurp+0x15f>
mov    0x2d13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1312 <ndt_file_slurp+0x1a2>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     12ff <ndt_file_slurp+0x18f>
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
movb   $0xa,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     132d <ndt_file_slurp+0x1bd>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1341 <ndt_file_slurp+0x1d1>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ndt_file_slurp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
test   %rdi,%rdi
lea    0xe7a(%rip),%rax
cmove  %rax,%rdi
lea    0xe6d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    0x2e26(%rip),%r13
xor    %ebx,%ebx
test   %rax,%rax
je     123c <ndt_file_slurp+0xcc>
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     123c <ndt_file_slurp+0xcc>
mov    %r15,%rdi
call   1060 <ftell@plt>
test   %rax,%rax
js     123a <ndt_file_slurp+0xca>
mov    %rax,%r12
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
test   %rax,%rax
js     123c <ndt_file_slurp+0xcc>
lea    0x2(%r12),%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1233 <ndt_file_slurp+0xc3>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    1279 <ndt_file_slurp+0x109>
cmpb   $0xa,-0x1(%r12,%rbx,1)
je     122c <ndt_file_slurp+0xbc>
movb   $0xa,(%rbx,%r12,1)
add    $0x1,%r12
movb   $0x0,(%rbx,%r12,1)
jmp    1270 <ndt_file_slurp+0x100>
mov    0x2d96(%rip),%r13
xor    %ebx,%ebx
mov    0x0(%r13),%ebp
test   %ebp,%ebp
je     1270 <ndt_file_slurp+0x100>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
test   %r14,%r14
je     1256 <ndt_file_slurp+0xe6>
mov    %ebp,(%r14)
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
test   %r14,%r14
jne    1253 <ndt_file_slurp+0xe3>
jmp    1256 <ndt_file_slurp+0xe6>
mov    0x2d58(%rip),%r13
jmp    123c <ndt_file_slurp+0xcc>

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
@.str.1 = [3 x i8] c"rb\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@ENOMEM = global i32 0
@EIO = global i32 0
define i8* @ndt_file_slurp(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 0, i32* %6
store i8* null, i8** %7
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i8*, i8** %3
br label %14
13: ; preds = %2
br label %14
14: ; preds = %13, %11
%15 = phi i8* [ %12, %11 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %13 ]
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp ne i32* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = load i32, i32* @errno
store i32 %20, i32* %6
br label %75
21: ; preds = %14
%22 = load i32*, i32** %8
%23 = load i32, i32* @SEEK_END
%24 = call i64 @fseek(i32* %22, i64 0, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = load i32, i32* @errno
store i32 %27, i32* %6
br label %75
28: ; preds = %21
%29 = load i32*, i32** %8
%30 = call i64 @ftell(i32* %29)
store i64 %30, i64* %5
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = load i32, i32* @errno
store i32 %33, i32* %6
br label %75
34: ; preds = %28
%35 = load i32*, i32** %8
%36 = load i32, i32* @SEEK_SET
%37 = call i64 @fseek(i32* %35, i64 0, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = load i32, i32* @errno
store i32 %40, i32* %6
br label %75
41: ; preds = %34
%42 = load i64, i64* %5
%43 = add nsw i64 %42, 2
%44 = call i8* @malloc(i64 %43)
store i8* %44, i8** %7
%45 = load i8*, i8** %7
%46 = icmp ne i8* %45, null
br i1 %46, label %49, label %47
47: ; preds = %41
%48 = load i32, i32* @ENOMEM
store i32 %48, i32* %6
br label %75
49: ; preds = %41
%50 = load i8*, i8** %7
%51 = load i64, i64* %5
%52 = load i32*, i32** %8
%53 = call i64 @fread(i8* %50, i32 1, i64 %51, i32* %52)
%54 = load i64, i64* %5
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %49
%57 = load i32, i32* @EIO
store i32 %57, i32* %6
br label %75
58: ; preds = %49
%59 = load i8*, i8** %7
%60 = load i64, i64* %5
%61 = sub nsw i64 %60, 1
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 10
br i1 %65, label %66, label %71
66: ; preds = %58
%67 = load i8*, i8** %7
%68 = load i64, i64* %5
%69 = add nsw i64 %68, 1
store i64 %69, i64* %5
%70 = getelementptr inbounds i8, i8* %67, i64 %68
store i8 10, i8* %70
br label %71
71: ; preds = %66, %58
%72 = load i8*, i8** %7
%73 = load i64, i64* %5
%74 = getelementptr inbounds i8, i8* %72, i64 %73
store i8 0, i8* %74
store i32 0, i32* %6
br label %75
75: ; preds = %71, %56, %47, %39, %32, %26, %19
%76 = load i32, i32* %6
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i8*, i8** %7
%80 = call i32 @free(i8* %79)
store i8* null, i8** %7
br label %81
81: ; preds = %78, %75
%82 = load i32*, i32** %4
%83 = icmp ne i32* %82, null
br i1 %83, label %84, label %87
84: ; preds = %81
%85 = load i32, i32* %6
%86 = load i32*, i32** %4
store i32 %85, i32* %86
br label %87
87: ; preds = %84, %81
%88 = load i32*, i32** %8
%89 = call i32 @fclose(i32* %88)
%90 = load i8*, i8** %7
ret i8* %90
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [3 x i8] c"rb\00"
@errno = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@ENOMEM = global i32 0
@EIO = global i32 0
define noalias i8* @ndt_file_slurp(i8* %0, i32* writeonly %1) {
%3 = icmp eq i8* %0, null
%4 = select i1 %3, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8* %0
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %36, label %7
7: ; preds = %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i64 @fseek(i32* %5, i64 0, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %36, label %11
11: ; preds = %7
%12 = tail call i64 @ftell(i32* %5)
%13 = icmp slt i64 %12, 0
br i1 %13, label %36, label %14
14: ; preds = %11
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @fseek(i32* %5, i64 0, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %36, label %18
18: ; preds = %14
%19 = add nuw nsw i64 %12, 2
%20 = tail call i8* @malloc(i64 %19)
%21 = icmp eq i8* %20, null
br i1 %21, label %36, label %22
22: ; preds = %18
%23 = tail call i64 @fread(i8* %20, i32 1, i64 %12, i32* %5)
%24 = icmp eq i64 %23, %12
br i1 %24, label %25, label %36
25: ; preds = %22
%26 = add nsw i64 %12, -1
%27 = getelementptr inbounds i8, i8* %20, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 10
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = add nuw nsw i64 %12, 1
%32 = getelementptr inbounds i8, i8* %20, i64 %12
store i8 10, i8* %32
br label %33
33: ; preds = %25, %30
%34 = phi i64 [ %31, %30 ], [ %12, %25 ]
%35 = getelementptr inbounds i8, i8* %20, i64 %34
store i8 0, i8* %35
br label %43
36: ; preds = %22, %18, %14, %11, %7, %2
%37 = phi i32* [ @errno, %2 ], [ @errno, %7 ], [ @errno, %11 ], [ @errno, %14 ], [ @ENOMEM, %18 ], [ @EIO, %22 ]
%38 = phi i8* [ null, %2 ], [ null, %7 ], [ null, %11 ], [ null, %14 ], [ null, %18 ], [ %20, %22 ]
%39 = load i32, i32* %37
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %36
%42 = tail call i32 @free(i8* %38)
br label %43
43: ; preds = %33, %41, %36
%44 = phi i32 [ %39, %41 ], [ 0, %36 ], [ 0, %33 ]
%45 = phi i8* [ null, %41 ], [ %38, %36 ], [ %20, %33 ]
%46 = icmp eq i32* %1, null
br i1 %46, label %48, label %47
47: ; preds = %43
store i32 %44, i32* %1
br label %48
48: ; preds = %47, %43
%49 = tail call i32 @fclose(i32* %5)
ret i8* %45
}