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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int strlen(char *);
void state_get(char *dir, char *name, char **buf) {
  FILE *fp;
  char *path;
  int path_len;
  int size;
  struct stat st;
  int err;
  err = stat(dir, &st);
  if (err == -1) {
    if (errno == ENOENT) {
      fprintf(stderr, "Error: %s directory does not exist\n", dir);
    } else {
      perror("stat error");
    }
    exit(EXIT_FAILURE);
  } else {
    if (!S_ISDIR(st.st_mode)) {
      fprintf(stderr, "Error: %s is not a directory\n", dir);
      exit(EXIT_FAILURE);
    }
  }
  path_len = strlen(dir) + strlen(name) + 2;
  path = malloc(path_len);
  if (path == NULL) {
    perror("malloc path");
    return;
  }
  snprintf(path, path_len, "%s/%s", dir, name);
  fp = fopen(path, "r");
  if (fp == NULL) {
    perror("State file open failed");
    return;
  }
  fseek(fp, 0, SEEK_END);
  size = ftell(fp);
  fseek(fp, 0, SEEK_SET);
  *buf = malloc(size);
  if (*buf == NULL) {
    perror("Can't malloc for buffer");
    return;
  }
  fread(*buf, 1, size, fp);
  fclose(fp);
  free(path);
}


x86 assembly (clang, -O0):
<state_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   10b0 <stat@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1260 <state_get+0x80>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%rax
jne    1246 <state_get+0x66>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc4(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1252 <state_get+0x72>
lea    0xdd7(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x38(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1298 <state_get+0xb8>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdaa(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
jmp    129d <state_get+0xbd>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12e7 <state_get+0x107>
lea    0xd70(%rip),%rdi
call   10f0 <perror@plt>
jmp    13c1 <state_get+0x1e1>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd5c(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xd7f(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1332 <state_get+0x152>
lea    0xd37(%rip),%rdi
call   10f0 <perror@plt>
jmp    13c1 <state_get+0x1e1>
mov    -0x20(%rbp),%rdi
mov    0x2cab(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2c6b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    -0x30(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1397 <state_get+0x1b7>
lea    0xce9(%rip),%rdi
call   10f0 <perror@plt>
jmp    13c1 <state_get+0x1e1>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<state_get>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %rsp,%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
je     1308 <state_get+0x128>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     132c <state_get+0x14c>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1060 <strlen@plt>
add    %eax,%ebx
add    $0x2,%ebx
mov    %ebx,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12dc <state_get+0xfc>
mov    %rax,%r13
lea    0xe13(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r15,%rcx
mov    %r12,%r8
call   1080 <snprintf@plt>
lea    0xe2f(%rip),%rsi
mov    %r13,%rdi
call   10e0 <fopen@plt>
test   %rax,%rax
je     12e5 <state_get+0x105>
mov    %rax,%rbx
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %rbx,%rdi
call   10a0 <ftell@plt>
mov    %eax,%ebp
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
mov    %ebp,%edi
call   10c0 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     12ee <state_get+0x10e>
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd6a(%rip),%rdi
jmp    12f5 <state_get+0x115>
lea    0xd73(%rip),%rdi
jmp    12f5 <state_get+0x115>
lea    0xd81(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10f0 <perror@plt>
mov    0x2cc1(%rip),%rax
mov    (%rax),%rax
mov    0x2c97(%rip),%rcx
cmp    (%rcx),%rax
je     1352 <state_get+0x172>
lea    0xcff(%rip),%rdi
call   10f0 <perror@plt>
jmp    1344 <state_get+0x164>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xcf3(%rip),%rsi
mov    %r15,%rdx
call   1090 <fprintf@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
lea    0xc9e(%rip),%rsi
jmp    133c <state_get+0x15c>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"Error: %s directory does not exist\0A\00"
@.str.1 = [11 x i8] c"stat error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [30 x i8] c"Error: %s is not a directory\0A\00"
@.str.3 = [12 x i8] c"malloc path\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [23 x i8] c"State file open failed\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [24 x i8] c"Can't malloc for buffer\00"
define void @state_get(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.stat
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8** %2, i8*** %6
%13 = load i8*, i8** %4
%14 = call i32 @stat(i8* %13, %struct.stat* %11)
store i32 %14, i32* %12
%15 = load i32, i32* %12
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %30
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %4
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %3
%31 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @S_ISDIR(i32 %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %4
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %30
br label %42
42: ; preds = %41
%43 = load i8*, i8** %4
%44 = call i32 @strlen(i8* %43)
%45 = load i8*, i8** %5
%46 = call i32 @strlen(i8* %45)
%47 = add nsw i32 %44, %46
%48 = add nsw i32 %47, 2
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = call i8* @malloc(i32 %49)
store i8* %50, i8** %8
%51 = load i8*, i8** %8
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %55
53: ; preds = %42
%54 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %94
55: ; preds = %42
%56 = load i8*, i8** %8
%57 = load i32, i32* %9
%58 = load i8*, i8** %4
%59 = load i8*, i8** %5
%60 = call i32 @snprintf(i8* %56, i32 %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %58, i8* %59)
%61 = load i8*, i8** %8
%62 = call i32* @fopen(i8* %61, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %62, i32** %7
%63 = load i32*, i32** %7
%64 = icmp eq i32* %63, null
br i1 %64, label %65, label %67
65: ; preds = %55
%66 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %94
67: ; preds = %55
%68 = load i32*, i32** %7
%69 = load i32, i32* @SEEK_END
%70 = call i32 @fseek(i32* %68, i32 0, i32 %69)
%71 = load i32*, i32** %7
%72 = call i32 @ftell(i32* %71)
store i32 %72, i32* %10
%73 = load i32*, i32** %7
%74 = load i32, i32* @SEEK_SET
%75 = call i32 @fseek(i32* %73, i32 0, i32 %74)
%76 = load i32, i32* %10
%77 = call i8* @malloc(i32 %76)
%78 = load i8**, i8*** %6
store i8* %77, i8** %78
%79 = load i8**, i8*** %6
%80 = load i8*, i8** %79
%81 = icmp eq i8* %80, null
br i1 %81, label %82, label %84
82: ; preds = %67
%83 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %94
84: ; preds = %67
%85 = load i8**, i8*** %6
%86 = load i8*, i8** %85
%87 = load i32, i32* %10
%88 = load i32*, i32** %7
%89 = call i32 @fread(i8* %86, i32 1, i32 %87, i32* %88)
%90 = load i32*, i32** %7
%91 = call i32 @fclose(i32* %90)
%92 = load i8*, i8** %8
%93 = call i32 @free(i8* %92)
br label %94
94: ; preds = %84, %82, %65, %53
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"Error: %s directory does not exist\0A\00"
@.str.1 = [11 x i8] c"stat error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [30 x i8] c"Error: %s is not a directory\0A\00"
@.str.3 = [12 x i8] c"malloc path\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [23 x i8] c"State file open failed\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.7 = [24 x i8] c"Can't malloc for buffer\00"
define void @state_get(i8* %0, i8* %1, i8** nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %3
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @S_ISDIR(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = tail call i32 @strlen(i8* %0)
%32 = tail call i32 @strlen(i8* %1)
%33 = add i32 %31, 2
%34 = add i32 %33, %32
%35 = tail call i8* @malloc(i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %59
39: ; preds = %30
%40 = tail call i32 @snprintf(i8* %35, i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %1)
%41 = tail call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %59
45: ; preds = %39
%46 = load i32, i32* @SEEK_END
%47 = tail call i32 @fseek(i32* %41, i32 0, i32 %46)
%48 = tail call i32 @ftell(i32* %41)
%49 = load i32, i32* @SEEK_SET
%50 = tail call i32 @fseek(i32* %41, i32 0, i32 %49)
%51 = tail call i8* @malloc(i32 %48)
store i8* %51, i8** %2
%52 = icmp eq i8* %51, null
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
br label %59
55: ; preds = %45
%56 = tail call i32 @fread(i8* %51, i32 1, i32 %48, i32* %41)
%57 = tail call i32 @fclose(i32* %41)
%58 = tail call i32 @free(i8* %35)
br label %59
59: ; preds = %55, %53, %43, %37
ret void
}