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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int fstat(int, struct stat *);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char *);
int read_config_file(char **contents, char *prefix) {
  int fd;
  off_t size;
  size_t read_howmany;
  struct stat stats;
  FILE *config_file;
  char *file_contents;
  char *path_prefix;
  char *path_to_config_php;
  if (!prefix) {
    path_prefix = "/opt/sing-tsung";
  } else {
    path_prefix = prefix;
  }
  path_to_config_php = (char *)malloc(
      sizeof(char) * (strlen(path_prefix) + strlen("/config.php") + 1));
  path_to_config_php[0] = '\0';
  strcat(path_to_config_php, path_prefix);
  strcat(path_to_config_php, "/config.php");
  fd = open(path_to_config_php, O_RDONLY);
  if (fd == -1) {
    perror("Error");
    fprintf(stderr, "Could not open config file '%s'\n", path_to_config_php);
    free(path_to_config_php);
    exit(-1);
  }
  free(path_to_config_php);
  if (fstat(fd, &stats) == -1) {
    perror("Error");
    fprintf(stderr, "Couldn't stat file '%s'", path_to_config_php);
    exit(-1);
  }
  if (!(file_contents = (char *)malloc(stats.st_size))) {
    perror("Failed to alloc space for the config file");
    exit(-1);
  }
  if (!(config_file = fdopen(fd, "r"))) {
    perror("Couldn't stream data from config file");
    exit(-1);
  }
  read_howmany = fread(file_contents, 1, stats.st_size, config_file);
  if (read_howmany != stats.st_size) {
    fprintf(stderr, "Expected %d bytes from config file, got %d\n",
            stats.st_size, read_howmany);
  }
  if (fclose(config_file)) {
    perror("couldn't close the config file?");
    exit(-1);
  }
  *contents = file_contents;
  return read_howmany;
}


x86 assembly (clang, -O0):
<read_config_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11eb <read_config_file+0x2b>
lea    0xe1e(%rip),%rax
mov    %rax,-0x40(%rbp)
jmp    11f3 <read_config_file+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x4c(%rbp)
lea    0xe0a(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x4c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0x48(%rbp),%rdi
lea    0xdcb(%rip),%rsi
call   10c0 <strcat@plt>
mov    -0x48(%rbp),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12a3 <read_config_file+0xe3>
lea    0xdac(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10e0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    12f2 <read_config_file+0x132>
lea    0xd54(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rdx
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    131e <read_config_file+0x15e>
lea    0xd4c(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xcf8(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1351 <read_config_file+0x191>
lea    0xd43(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
cmp    %rcx,%rax
je     1399 <read_config_file+0x1d9>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
je     13c2 <read_config_file+0x202>
lea    0xd24(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_config_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
lea    0xe25(%rip),%rax
cmove  %rax,%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0xe20(%rip),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
movb   $0x0,(%rax)
mov    %rax,%rdi
mov    %rbx,%rsi
call   10c0 <strcat@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10c0 <strcat@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12e5 <read_config_file+0x125>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   10e0 <fstat@plt>
cmp    $0xffffffff,%eax
je     131d <read_config_file+0x15d>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     134d <read_config_file+0x18d>
mov    %rax,%r15
lea    0xdad(%rip),%rsi
mov    %ebx,%edi
call   1090 <fdopen@plt>
test   %rax,%rax
je     1356 <read_config_file+0x196>
mov    %rax,%rbx
movslq %ebp,%r13
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
mov    %rax,%r12
cmp    %r13,%rax
je     12bf <read_config_file+0xff>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %ebp,%edx
mov    %r12,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jne    135f <read_config_file+0x19f>
mov    %r15,(%r14)
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd30(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
lea    0xd21(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xcf8(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ca8(%rip),%rax
mov    (%rax),%edi
lea    0xd0a(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    0xd07(%rip),%rdi
jmp    1366 <read_config_file+0x1a6>
lea    0xd28(%rip),%rdi
jmp    1366 <read_config_file+0x1a6>
lea    0xd71(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [16 x i8] c"/opt/sing-tsung\00"
@.str.1 = [12 x i8] c"/config.php\00"
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"Error\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Could not open config file '%s'\0A\00"
@.str.4 = [24 x i8] c"Couldn't stat file '%s'\00"
@.str.5 = [42 x i8] c"Failed to alloc space for the config file\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [38 x i8] c"Couldn't stream data from config file\00"
@.str.8 = [44 x i8] c"Expected %d bytes from config file, got %d\0A\00"
@.str.9 = [32 x i8] c"couldn't close the config file?\00"
define i32 @read_config_file(i8** %0, i8* %1) {
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8** %0, i8*** %3
store i8* %1, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %2
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %11
br label %18
16: ; preds = %2
%17 = load i8*, i8** %4
store i8* %17, i8** %11
br label %18
18: ; preds = %16, %15
%19 = load i8*, i8** %11
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = add nsw i32 %20, %21
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
%25 = mul i64 1, %24
%26 = trunc i64 %25 to i32
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %12
%29 = load i8*, i8** %12
%30 = getelementptr inbounds i8, i8* %29, i64 0
store i8 0, i8* %30
%31 = load i8*, i8** %12
%32 = load i8*, i8** %11
%33 = call i32 @strcat(i8* %31, i8* %32)
%34 = load i8*, i8** %12
%35 = call i32 @strcat(i8* %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%36 = load i8*, i8** %12
%37 = load i32, i32* @O_RDONLY
%38 = call i32 @open(i8* %36, i32 %37)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %49
41: ; preds = %18
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %12
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %12
%47 = call i32 @free(i8* %46)
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %18
%50 = load i8*, i8** %12
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %5
%53 = call i32 @fstat(i32 %52, %struct.stat* %8)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %12
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %58)
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %49
%62 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @malloc(i32 %63)
%65 = inttoptr i64 %64 to i8*
store i8* %65, i8** %10
%66 = icmp ne i8* %65, null
br i1 %66, label %70, label %67
67: ; preds = %61
%68 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 @exit(i32 -1)
unreachable
70: ; preds = %61
%71 = load i32, i32* %5
%72 = call i32* @fdopen(i32 %71, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %72, i32** %9
%73 = icmp ne i32* %72, null
br i1 %73, label %77, label %74
74: ; preds = %70
%75 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
%76 = call i32 @exit(i32 -1)
unreachable
77: ; preds = %70
%78 = load i8*, i8** %10
%79 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%80 = load i32, i32* %79
%81 = load i32*, i32** %9
%82 = call i64 @fread(i8* %78, i32 1, i32 %80, i32* %81)
store i64 %82, i64* %7
%83 = load i64, i64* %7
%84 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%85 = load i32, i32* %84
%86 = sext i32 %85 to i64
%87 = icmp ne i64 %83, %86
br i1 %87, label %88, label %94
88: ; preds = %77
%89 = load i32, i32* @stderr
%90 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%91 = load i32, i32* %90
%92 = load i64, i64* %7
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i32 %91, i64 %92)
br label %94
94: ; preds = %88, %77
%95 = load i32*, i32** %9
%96 = call i64 @fclose(i32* %95)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %101
98: ; preds = %94
%99 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
%100 = call i32 @exit(i32 -1)
unreachable
101: ; preds = %94
%102 = load i8*, i8** %10
%103 = load i8**, i8*** %3
store i8* %102, i8** %103
%104 = load i64, i64* %7
%105 = trunc i64 %104 to i32
ret i32 %105
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [16 x i8] c"/opt/sing-tsung\00"
@.str.1 = [12 x i8] c"/config.php\00"
@O_RDONLY = global i32 0
@.str.2 = [6 x i8] c"Error\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Could not open config file '%s'\0A\00"
@.str.4 = [24 x i8] c"Couldn't stat file '%s'\00"
@.str.5 = [42 x i8] c"Failed to alloc space for the config file\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [38 x i8] c"Couldn't stream data from config file\00"
@.str.8 = [44 x i8] c"Expected %d bytes from config file, got %d\0A\00"
@.str.9 = [32 x i8] c"couldn't close the config file?\00"
define i32 @read_config_file(i8** nocapture writeonly %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i8* %1, null
%6 = select i1 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %1
%7 = tail call i32 @strlen(i8* %6)
%8 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%9 = add i32 %7, 1
%10 = add i32 %9, %8
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8 0, i8* %12
%13 = tail call i32 @strcat(i8* %12, i8* %6)
%14 = tail call i32 @strcat(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* %12, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%22 = tail call i32 @free(i8* %12)
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
%25 = tail call i32 @free(i8* %12)
%26 = call i32 @fstat(i32 %16, %struct.stat* %3)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %12)
%32 = tail call i32 @exit(i32 -1)
unreachable
33: ; preds = %24
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = tail call i64 @malloc(i32 %35)
%37 = inttoptr i64 %36 to i8*
%38 = icmp eq i64 %36, 0
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
%41 = tail call i32 @exit(i32 -1)
unreachable
42: ; preds = %33
%43 = tail call i32* @fdopen(i32 %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0))
%47 = tail call i32 @exit(i32 -1)
unreachable
48: ; preds = %42
%49 = tail call i64 @fread(i8* %37, i32 1, i32 %35, i32* %43)
%50 = sext i32 %35 to i64
%51 = icmp eq i64 %49, %50
br i1 %51, label %55, label %52
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = tail call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.8, i64 0, i64 0), i32 %35, i64 %49)
br label %55
55: ; preds = %52, %48
%56 = tail call i64 @fclose(i32* %43)
%57 = icmp eq i64 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %55
%59 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
%60 = tail call i32 @exit(i32 -1)
unreachable
61: ; preds = %55
store i8* %37, i8** %0
%62 = trunc i64 %49 to i32
ret i32 %62
}