#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 */
scalar_t__ EEXIST;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
char *join_path(char const *, char const *);
int make_dir(char const *);
int /*<<< orphan*/ stderr;
void dump_to_file(void *ptr, int len, const char *name, const char *cache_dir) {
  int status;
  status = make_dir(cache_dir);
  if (status == 0 || errno == EEXIST) {
    char *fname = join_path(cache_dir, name);
    if (fname == NULL) {
      fprintf(stderr, "Path to cache tables is too long\n");
    } else {
      FILE *f = fopen(fname, "wb");
      fwrite(ptr, len, 1, f);
      free(fname);
      fclose(f);
    }
  } else {
    fprintf(stderr, "cannot create cache tables directory\n");
  }
}


x86 assembly (clang, -O0):
<dump_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <make_dir@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11b7 <dump_to_file+0x47>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    122d <dump_to_file+0xbd>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <join_path@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ed <dump_to_file+0x7d>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1228 <dump_to_file+0xb8>
mov    -0x30(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1242 <dump_to_file+0xd2>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1060 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_to_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rcx,%rdi
call   1070 <make_dir@plt>
test   %eax,%eax
je     11a5 <dump_to_file+0x35>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11f7 <dump_to_file+0x87>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <join_path@plt>
test   %rax,%rax
je     1209 <dump_to_file+0x99>
mov    %rax,%rbp
lea    0xe63(%rip),%rsi
mov    %rax,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
jmp    1219 <dump_to_file+0xa9>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [34 x i8] c"Path to cache tables is too long\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [38 x i8] c"cannot create cache tables directory\0A\00"
define void @dump_to_file(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%12 = load i8*, i8** %8
%13 = call i32 @make_dir(i8* %12)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %4
%17 = load i64, i64* @errno
%18 = load i64, i64* @EEXIST
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %41
20: ; preds = %16, %4
%21 = load i8*, i8** %8
%22 = load i8*, i8** %7
%23 = call i8* @join_path(i8* %21, i8* %22)
store i8* %23, i8** %10
%24 = load i8*, i8** %10
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %40
29: ; preds = %20
%30 = load i8*, i8** %10
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %11
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = load i32*, i32** %11
%35 = call i32 @fwrite(i8* %32, i32 %33, i32 1, i32* %34)
%36 = load i8*, i8** %10
%37 = call i32 @free(i8* %36)
%38 = load i32*, i32** %11
%39 = call i32 @fclose(i32* %38)
br label %40
40: ; preds = %29, %26
br label %44
41: ; preds = %16
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %41, %40
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str = [34 x i8] c"Path to cache tables is too long\0A\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [38 x i8] c"cannot create cache tables directory\0A\00"
define void @dump_to_file(i8* nocapture %0, i32 %1, i8* %2, i8* %3) {
%5 = tail call i32 @make_dir(i8* %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EEXIST
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %22
11: ; preds = %7, %4
%12 = tail call i8* @join_path(i8* %3, i8* %2)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %25
17: ; preds = %11
%18 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @fwrite(i8* %0, i32 %1, i32 1, i32* %18)
%20 = tail call i32 @free(i8* %12)
%21 = tail call i32 @fclose(i32* %18)
br label %25
22: ; preds = %7
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %14, %17, %22
ret void
}