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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, ...);
int stat(char const *, struct stat *);
int /*<<< orphan*/ valgrind_assert_failed(char *, int);
char *slurp(const char *filename) {
  FILE *file = fopen(filename, "r");
  if (file == NULL) {
    printf("slurp(): could not read from: %s\n", filename);
    return NULL;
  }
  struct stat s;
  int rc = stat(filename, &s);
  if (!(rc == 0)) {
    valgrind_assert_failed(
        "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
        "2022-01-22/02-19-57/repos/JohnPJenkins/swift-t/refs/heads/master/"
        "c-utils/src/tools.c",
        349);
  };
  off_t length = s.st_size;
  char *result = malloc((size_t)length + 1);
  if (result == NULL) {
    printf("slurp(): could not allocate memory for: %s\n", filename);
    return NULL;
  }
  char *p = result;
  int actual = (int)fread(p, sizeof(char), (size_t)length, file);
  if (actual != length) {
    printf("could not read all %li bytes from file: %s\n", (long)length,
           filename);
    free(result);
    return NULL;
  }
  result[length] = '\0';
  fclose(file);
  return result;
}


x86 assembly (clang, -O0):
<slurp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ca <slurp+0x4a>
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <slurp+0x12c>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11f5 <slurp+0x75>
lea    0xe39(%rip),%rdi
mov    $0x15d,%esi
call   1040 <valgrind_assert_failed@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1236 <slurp+0xb6>
mov    -0x10(%rbp),%rsi
lea    0xe97(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <slurp+0x12c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     128f <slurp+0x10f>
movslq -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe73(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <slurp+0x12c>
mov    -0x30(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slurp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe6e(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11fc <slurp+0x7c>
mov    %rax,%r15
mov    %rsp,%rsi
mov    %r14,%rdi
call   1080 <stat@plt>
test   %eax,%eax
je     11bf <slurp+0x3f>
lea    0xe6f(%rip),%rdi
mov    $0x15d,%esi
call   1040 <valgrind_assert_failed@plt>
movslq (%rsp),%r12
lea    0x1(%r12),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     1205 <slurp+0x85>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %r15,%rcx
call   1050 <fread@plt>
cmp    %eax,%r12d
jne    121a <slurp+0x9a>
movb   $0x0,(%rbx,%r12,1)
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    1238 <slurp+0xb8>
lea    0xdff(%rip),%rdi
jmp    120c <slurp+0x8c>
lea    0xead(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1238 <slurp+0xb8>
lea    0xec4(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"slurp(): could not read from: %s\0A\00"
@.str.2 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/JohnPJenkins/swift-t/refs/heads/master/c-utils/src/tools.c\00"
@.str.3 = [44 x i8] c"slurp(): could not allocate memory for: %s\0A\00"
@.str.4 = [44 x i8] c"could not read all %li bytes from file: %s\0A\00"
define i8* @slurp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i8*, i8** %3
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i8* null, i8** %2
br label %63
18: ; preds = %1
%19 = load i8*, i8** %3
%20 = call i32 @stat(i8* %19, %struct.stat* %5)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = call i32 @valgrind_assert_failed(i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.2, i64 0, i64 0), i32 349)
br label %25
25: ; preds = %23, %18
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%27 = load i32, i32* %26
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = add i64 %29, 1
%31 = call i8* @malloc(i64 %30)
store i8* %31, i8** %8
%32 = load i8*, i8** %8
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %37
34: ; preds = %25
%35 = load i8*, i8** %3
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %35)
store i8* null, i8** %2
br label %63
37: ; preds = %25
%38 = load i8*, i8** %8
store i8* %38, i8** %9
%39 = load i8*, i8** %9
%40 = load i32, i32* %7
%41 = sext i32 %40 to i64
%42 = load i32*, i32** %4
%43 = call i64 @fread(i8* %39, i32 1, i64 %41, i32* %42)
%44 = trunc i64 %43 to i32
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = load i32, i32* %7
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %37
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = load i8*, i8** %3
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i64 %50, i8* %51)
%53 = load i8*, i8** %8
%54 = call i32 @free(i8* %53)
store i8* null, i8** %2
br label %63
55: ; preds = %37
%56 = load i8*, i8** %8
%57 = load i32, i32* %7
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8 0, i8* %59
%60 = load i32*, i32** %4
%61 = call i32 @fclose(i32* %60)
%62 = load i8*, i8** %8
store i8* %62, i8** %2
br label %63
63: ; preds = %55, %48, %34, %15
%64 = load i8*, i8** %2
ret i8* %64
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [34 x i8] c"slurp(): could not read from: %s\0A\00"
@.str.2 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/JohnPJenkins/swift-t/refs/heads/master/c-utils/src/tools.c\00"
@.str.3 = [44 x i8] c"slurp(): could not allocate memory for: %s\0A\00"
@.str.4 = [44 x i8] c"could not read all %li bytes from file: %s\0A\00"
define noalias i8* @slurp(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %34
7: ; preds = %1
%8 = bitcast %struct.stat* %2 to i8*
%9 = call i32 @stat(i8* %0, %struct.stat* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @valgrind_assert_failed(i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.2, i64 0, i64 0), i32 349)
br label %13
13: ; preds = %11, %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = sext i32 %15 to i64
%17 = add nsw i64 %16, 1
%18 = tail call i8* @malloc(i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %32
22: ; preds = %13
%23 = tail call i64 @fread(i8* %18, i32 1, i64 %16, i32* %3)
%24 = trunc i64 %23 to i32
%25 = icmp eq i32 %15, %24
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i64 %16, i8* %0)
%28 = tail call i32 @free(i8* %18)
br label %32
29: ; preds = %22
%30 = getelementptr inbounds i8, i8* %18, i64 %16
store i8 0, i8* %30
%31 = tail call i32 @fclose(i32* %3)
br label %32
32: ; preds = %26, %29, %20
%33 = phi i8* [ null, %20 ], [ null, %26 ], [ %18, %29 ]
br label %34
34: ; preds = %32, %5
%35 = phi i8* [ null, %5 ], [ %33, %32 ]
ret i8* %35
}