#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;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISDIR(int /*<<< orphan*/);
char *dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strdup(char const *);
int system(char *);
int check_path(const char *filename, int create) {
  struct stat s;
  int err_stat = stat(filename, &s);
  if (err_stat != -1 && S_ISBLK(s.st_mode))
    return 0;
  if (err_stat == -1 && errno != ENOENT) {
    perror("check_path:blkfile_check:stat");
    return errno;
  }
  char *file = strdup(filename);
  char *dir = dirname(file);
  if (dir == NULL) {
    fprintf(stderr, "failed to get directory for %s.", filename);
    return -1;
  }
  if (strcmp(dir, ".") == 0 || strcmp(dir, "/") == 0 ||
      strcmp(dir, "./") == 0) {
    return 0;
  }
  err_stat = stat(dir, &s);
  if (err_stat != -1 && S_ISDIR(s.st_mode))
    return 0;
  if (err_stat == -1 && errno != ENOENT) {
    perror("check_path:dir_check:stat");
    return errno;
  }
  if (create > 0) {
    char command[1024];
    snprintf(command, 1024 - 1, "mkdir -p -m 755 %s", dir);
    if (system(command) != -1)
      return 0;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11ea <check_path+0x4a>
mov    -0x18(%rbp),%edi
call   10a0 <S_ISBLK@plt>
cmp    $0x0,%rax
je     11ea <check_path+0x4a>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    122c <check_path+0x8c>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
je     122c <check_path+0x8c>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1276 <check_path+0xd6>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb9(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
mov    -0x30(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12c4 <check_path+0x124>
mov    -0x30(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     12c4 <check_path+0x124>
mov    -0x30(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12d0 <check_path+0x130>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
mov    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     1308 <check_path+0x168>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1308 <check_path+0x168>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    134a <check_path+0x1aa>
mov    0x2cbf(%rip),%rax
mov    (%rax),%rax
mov    0x2ca5(%rip),%rcx
cmp    (%rcx),%rax
je     134a <check_path+0x1aa>
lea    0xd0e(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
cmpl   $0x0,-0x14(%rbp)
jle    139d <check_path+0x1fd>
lea    -0x430(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    $0x3ff,%esi
lea    0xcf0(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
call   1030 <system@plt>
cmp    $0xffffffff,%eax
je     1391 <check_path+0x1f1>
movl   $0x0,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <check_path+0x204>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     11d8 <check_path+0x38>
mov    0x8(%rsp),%edi
call   10a0 <S_ISBLK@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    128d <check_path+0xed>
jmp    11f3 <check_path+0x53>
mov    0x2df9(%rip),%rbp
mov    0x0(%rbp),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
jne    12e9 <check_path+0x149>
mov    %rbx,%rdi
call   10c0 <strdup@plt>
mov    %rax,%rdi
call   1060 <dirname@plt>
test   %rax,%rax
je     1270 <check_path+0xd0>
mov    %rax,%r15
lea    0xe2a(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     128d <check_path+0xed>
lea    0xe17(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     128d <check_path+0xed>
lea    0xe02(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     128d <check_path+0xed>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     129d <check_path+0xfd>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %r14d,%r14d
jle    128d <check_path+0xed>
test   %rax,%rax
je     12b8 <check_path+0x118>
jmp    128d <check_path+0xed>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d34(%rip),%rbx
mov    (%rbx),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    12fa <check_path+0x15a>
test   %r14d,%r14d
jle    128d <check_path+0xed>
lea    0xd9c(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3ff,%esi
mov    %r15,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <system@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
jmp    128d <check_path+0xed>
lea    0xd10(%rip),%rdi
call   10b0 <perror@plt>
mov    0x0(%rbp),%ebp
jmp    128d <check_path+0xed>
lea    0xd40(%rip),%rdi
call   10b0 <perror@plt>
mov    (%rbx),%ebp
jmp    128d <check_path+0xed>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [30 x i8] c"check_path:blkfile_check:stat\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"failed to get directory for %s.\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [3 x i8] c"./\00"
@.str.5 = [26 x i8] c"check_path:dir_check:stat\00"
@.str.6 = [19 x i8] c"mkdir -p -m 755 %s\00"
define i32 @check_path(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [1024 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call i32 @stat(i8* %11, %struct.stat* %6)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISBLK(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %3
br label %91
21: ; preds = %15, %2
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp ne i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%30 = load i64, i64* @errno
%31 = trunc i64 %30 to i32
store i32 %31, i32* %3
br label %91
32: ; preds = %24, %21
%33 = load i8*, i8** %4
%34 = call i8* @strdup(i8* %33)
store i8* %34, i8** %8
%35 = load i8*, i8** %8
%36 = call i8* @dirname(i8* %35)
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %43
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i8*, i8** %4
%42 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i32 -1, i32* %3
br label %91
43: ; preds = %32
%44 = load i8*, i8** %9
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp eq i64 %45, 0
br i1 %46, label %55, label %47
47: ; preds = %43
%48 = load i8*, i8** %9
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp eq i64 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = load i8*, i8** %9
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %51, %47, %43
store i32 0, i32* %3
br label %91
56: ; preds = %51
%57 = load i8*, i8** %9
%58 = call i32 @stat(i8* %57, %struct.stat* %6)
store i32 %58, i32* %7
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, -1
br i1 %60, label %61, label %67
61: ; preds = %56
%62 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @S_ISDIR(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %61
store i32 0, i32* %3
br label %91
67: ; preds = %61, %56
%68 = load i32, i32* %7
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %78
70: ; preds = %67
%71 = load i64, i64* @errno
%72 = load i64, i64* @ENOENT
%73 = icmp ne i64 %71, %72
br i1 %73, label %74, label %78
74: ; preds = %70
%75 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%76 = load i64, i64* @errno
%77 = trunc i64 %76 to i32
store i32 %77, i32* %3
br label %91
78: ; preds = %70, %67
%79 = load i32, i32* %5
%80 = icmp sgt i32 %79, 0
br i1 %80, label %81, label %90
81: ; preds = %78
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%83 = load i8*, i8** %9
%84 = call i32 @snprintf(i8* %82, i32 1023, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %83)
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%86 = call i32 @system(i8* %85)
%87 = icmp ne i32 %86, -1
br i1 %87, label %88, label %89
88: ; preds = %81
store i32 0, i32* %3
br label %91
89: ; preds = %81
store i32 -1, i32* %3
br label %91
90: ; preds = %78
store i32 0, i32* %3
br label %91
91: ; preds = %90, %89, %88, %74, %66, %55, %39, %28, %20
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [30 x i8] c"check_path:blkfile_check:stat\00"
@stderr = global i32 0
@.str.1 = [32 x i8] c"failed to get directory for %s.\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [2 x i8] c"/\00"
@.str.4 = [3 x i8] c"./\00"
@.str.5 = [26 x i8] c"check_path:dir_check:stat\00"
@.str.6 = [19 x i8] c"mkdir -p -m 755 %s\00"
define i32 @check_path(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISBLK(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %63
13: ; preds = %2
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%19 = load i64, i64* @errno
%20 = trunc i64 %19 to i32
br label %63
21: ; preds = %8, %13
%22 = tail call i8* @strdup(i8* %0)
%23 = tail call i8* @dirname(i8* %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %63
28: ; preds = %21
%29 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %63, label %31
31: ; preds = %28
%32 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %63, label %34
34: ; preds = %31
%35 = tail call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%36 = icmp eq i64 %35, 0
br i1 %36, label %63, label %37
37: ; preds = %34
%38 = call i32 @stat(i8* %23, %struct.stat* %3)
%39 = icmp eq i32 %38, -1
br i1 %39, label %47, label %40
40: ; preds = %37
%41 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%42 = load i32, i32* %41
%43 = tail call i64 @S_ISDIR(i32 %42)
%44 = icmp eq i64 %43, 0
%45 = icmp sgt i32 %1, 0
%46 = and i1 %45, %44
br i1 %46, label %57, label %63
47: ; preds = %37
%48 = load i64, i64* @errno
%49 = load i64, i64* @ENOENT
%50 = icmp eq i64 %48, %49
br i1 %50, label %55, label %51
51: ; preds = %47
%52 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
%53 = load i64, i64* @errno
%54 = trunc i64 %53 to i32
br label %63
55: ; preds = %47
%56 = icmp sgt i32 %1, 0
br i1 %56, label %57, label %63
57: ; preds = %40, %55
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%59 = call i32 @snprintf(i8* %58, i32 1023, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %23)
%60 = call i32 @system(i8* %58)
%61 = icmp eq i32 %60, -1
%62 = sext i1 %61 to i32
br label %63
63: ; preds = %25, %51, %57, %34, %31, %28, %40, %55, %8, %17
%64 = phi i32 [ %20, %17 ], [ 0, %8 ], [ -1, %25 ], [ %54, %51 ], [ %62, %57 ], [ 0, %34 ], [ 0, %31 ], [ 0, %28 ], [ 0, %40 ], [ 0, %55 ]
ret i32 %64
}