#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*/ mode_t;
typedef int /*<<< orphan*/ dirpath;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ eslib_file_mkdirpath(char *, int /*<<< orphan*/);
scalar_t__ eslib_file_path_check(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, unsigned int);
int eslib_file_mkfile(char *path, mode_t dirmode) {
  unsigned int i;
  unsigned int slashidx = 0;
  char dirpath[2048];
  int fd;
  if (eslib_file_path_check(path))
    return -1;
  for (i = 0; i < 2048; ++i) {
    if (path[i] == '\0')
      break;
    if (path[i] == '/')
      slashidx = i;
  }
  if (i >= 2048)
    return -1;
  if (i == 0 || slashidx >= i - 1)
    return -1;
  if (slashidx != 0) {
    memset(dirpath, 0, sizeof(dirpath));
    strncpy(dirpath, path, slashidx);
    dirpath[2048 - 1] = '\0';
    if (eslib_file_mkdirpath(dirpath, dirmode)) {
      printf("mkfile error creating directory path\n");
      return -1;
    }
  }
  fd = open(path, O_WRONLY | O_CREAT, 0700);
  if (fd == -1) {
    printf("mkfile(%s) error, open: %s\n", path, strerror(errno));
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<eslib_file_mkfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <eslib_file_path_check@plt>
cmp    $0x0,%rax
je     11b8 <eslib_file_mkfile+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <eslib_file_mkfile+0x1c0>
movl   $0x0,-0x18(%rbp)
cmpl   $0x800,-0x18(%rbp)
jae    1212 <eslib_file_mkfile+0x92>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    11e5 <eslib_file_mkfile+0x65>
jmp    1212 <eslib_file_mkfile+0x92>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11ff <eslib_file_mkfile+0x7f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1204 <eslib_file_mkfile+0x84>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11bf <eslib_file_mkfile+0x3f>
cmpl   $0x800,-0x18(%rbp)
jb     122b <eslib_file_mkfile+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <eslib_file_mkfile+0x1c0>
cmpl   $0x0,-0x18(%rbp)
je     1246 <eslib_file_mkfile+0xc6>
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jb     1252 <eslib_file_mkfile+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <eslib_file_mkfile+0x1c0>
cmpl   $0x0,-0x1c(%rbp)
je     12be <eslib_file_mkfile+0x13e>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1060 <memset@plt>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <eslib_file_mkdirpath@plt>
cmp    $0x0,%rax
je     12b9 <eslib_file_mkfile+0x139>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <eslib_file_mkfile+0x1c0>
jmp    12be <eslib_file_mkfile+0x13e>
mov    -0x10(%rbp),%rdi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
mov    0x2d26(%rip),%rax
or     (%rax),%esi
mov    $0x1c0,%edx
call   1090 <open@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0xffffffff,-0x824(%rbp)
jne    132e <eslib_file_mkfile+0x1ae>
mov    -0x10(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    0x2cd5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x830(%rbp),%rsi
mov    %rax,%rdx
lea    0xd0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1340 <eslib_file_mkfile+0x1c0>
mov    -0x824(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_file_mkfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1040 <eslib_file_path_check@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     11c8 <eslib_file_mkfile+0x38>
mov    %r15d,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
xor    %ebp,%ebp
jmp    11df <eslib_file_mkfile+0x4f>
xchg   %ax,%ax
lea    0x3(%rax),%ebp
add    $0x4,%rax
cmp    $0x800,%rax
je     11b5 <eslib_file_mkfile+0x25>
movzbl (%rbx,%rax,1),%ecx
cmp    $0x2f,%cl
je     11f0 <eslib_file_mkfile+0x60>
test   %cl,%cl
jne    11f2 <eslib_file_mkfile+0x62>
jmp    1251 <eslib_file_mkfile+0xc1>
xchg   %ax,%ax
mov    %eax,%ebp
movzbl 0x1(%rbx,%rax,1),%ecx
cmp    $0x2f,%cl
je     1210 <eslib_file_mkfile+0x80>
test   %cl,%cl
jne    1213 <eslib_file_mkfile+0x83>
jmp    1247 <eslib_file_mkfile+0xb7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0x1(%rax),%ebp
movzbl 0x2(%rbx,%rax,1),%ecx
cmp    $0x2f,%cl
je     1230 <eslib_file_mkfile+0xa0>
test   %cl,%cl
jne    1233 <eslib_file_mkfile+0xa3>
jmp    124d <eslib_file_mkfile+0xbd>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x2(%rax),%ebp
movzbl 0x3(%rbx,%rax,1),%ecx
cmp    $0x2f,%cl
je     11d0 <eslib_file_mkfile+0x40>
test   %cl,%cl
jne    11d3 <eslib_file_mkfile+0x43>
add    $0x3,%rax
jmp    1251 <eslib_file_mkfile+0xc1>
add    $0x1,%rax
jmp    1251 <eslib_file_mkfile+0xc1>
add    $0x2,%rax
add    $0xffffffff,%eax
jae    11b5 <eslib_file_mkfile+0x25>
cmp    %eax,%ebp
jae    11b5 <eslib_file_mkfile+0x25>
test   %ebp,%ebp
je     12ae <eslib_file_mkfile+0x11e>
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x7ff(%rsp)
mov    %r12,%rdi
mov    %r14d,%esi
call   1090 <eslib_file_mkdirpath@plt>
test   %rax,%rax
je     12ae <eslib_file_mkfile+0x11e>
lea    0xd78(%rip),%rdi
call   1050 <puts@plt>
jmp    11b5 <eslib_file_mkfile+0x25>
mov    0x2d13(%rip),%rax
mov    0x2d3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1c0,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12e1 <eslib_file_mkfile+0x151>
mov    %eax,%edi
call   1080 <close@plt>
xor    %r15d,%r15d
jmp    11b5 <eslib_file_mkfile+0x25>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd0a(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11b5 <eslib_file_mkfile+0x25>

LLVM IR (-O0):
@.str = [38 x i8] c"mkfile error creating directory path\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [28 x i8] c"mkfile(%s) error, open: %s\0A\00"
@errno = global i32 0
define i32 @eslib_file_mkfile(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [2048 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %7
%10 = load i8*, i8** %4
%11 = call i64 @eslib_file_path_check(i8* %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %88
14: ; preds = %2
store i32 0, i32* %6
br label %15
15: ; preds = %38, %14
%16 = load i32, i32* %6
%17 = icmp ult i32 %16, 2048
br i1 %17, label %18, label %41
18: ; preds = %15
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %18
br label %41
27: ; preds = %18
%28 = load i8*, i8** %4
%29 = load i32, i32* %6
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 47
br i1 %34, label %35, label %37
35: ; preds = %27
%36 = load i32, i32* %6
store i32 %36, i32* %7
br label %37
37: ; preds = %35, %27
br label %38
38: ; preds = %37
%39 = load i32, i32* %6
%40 = add i32 %39, 1
store i32 %40, i32* %6
br label %15
41: ; preds = %26, %15
%42 = load i32, i32* %6
%43 = icmp uge i32 %42, 2048
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 -1, i32* %3
br label %88
45: ; preds = %41
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %45
%49 = load i32, i32* %7
%50 = load i32, i32* %6
%51 = sub i32 %50, 1
%52 = icmp uge i32 %49, %51
br i1 %52, label %53, label %54
53: ; preds = %48, %45
store i32 -1, i32* %3
br label %88
54: ; preds = %48
%55 = load i32, i32* %7
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %72
57: ; preds = %54
%58 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%59 = call i32 @memset(i8* %58, i32 0, i32 2048)
%60 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%61 = load i8*, i8** %4
%62 = load i32, i32* %7
%63 = call i32 @strncpy(i8* %60, i8* %61, i32 %62)
%64 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 2047
store i8 0, i8* %64
%65 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%66 = load i32, i32* %5
%67 = call i64 @eslib_file_mkdirpath(i8* %65, i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %57
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
71: ; preds = %57
br label %72
72: ; preds = %71, %54
%73 = load i8*, i8** %4
%74 = load i32, i32* @O_WRONLY
%75 = load i32, i32* @O_CREAT
%76 = or i32 %74, %75
%77 = call i32 @open(i8* %73, i32 %76, i32 448)
store i32 %77, i32* %9
%78 = load i32, i32* %9
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %85
80: ; preds = %72
%81 = load i8*, i8** %4
%82 = load i32, i32* @errno
%83 = call i8* @strerror(i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %81, i8* %83)
store i32 -1, i32* %3
br label %88
85: ; preds = %72
%86 = load i32, i32* %9
%87 = call i32 @close(i32 %86)
store i32 0, i32* %3
br label %88
88: ; preds = %85, %80, %69, %53, %44, %13
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [28 x i8] c"mkfile(%s) error, open: %s\0A\00"
@errno = global i32 0
@str = [37 x i8] c"mkfile error creating directory path\00"
define i32 @eslib_file_mkfile(i8* %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @eslib_file_path_check(i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %69
7: ; preds = %2, %35
%8 = phi i64 [ %37, %35 ], [ 0, %2 ]
%9 = phi i32 [ %36, %35 ], [ 0, %2 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %8
%11 = load i8, i8* %10
switch i8 %11, label %14 [
i8 0, label %39
i8 47, label %12
]
12: ; preds = %7
%13 = trunc i64 %8 to i32
br label %14
14: ; preds = %7, %12
%15 = phi i32 [ %13, %12 ], [ %9, %7 ]
%16 = or i64 %8, 1
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
switch i8 %18, label %21 [
i8 0, label %39
i8 47, label %19
]
19: ; preds = %14
%20 = trunc i64 %16 to i32
br label %21
21: ; preds = %19, %14
%22 = phi i32 [ %20, %19 ], [ %15, %14 ]
%23 = or i64 %8, 2
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
switch i8 %25, label %28 [
i8 0, label %39
i8 47, label %26
]
26: ; preds = %21
%27 = trunc i64 %23 to i32
br label %28
28: ; preds = %26, %21
%29 = phi i32 [ %27, %26 ], [ %22, %21 ]
%30 = or i64 %8, 3
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = load i8, i8* %31
switch i8 %32, label %35 [
i8 0, label %39
i8 47, label %33
]
33: ; preds = %28
%34 = trunc i64 %30 to i32
br label %35
35: ; preds = %33, %28
%36 = phi i32 [ %34, %33 ], [ %29, %28 ]
%37 = add nuw nsw i64 %8, 4
%38 = icmp eq i64 %37, 2048
br i1 %38, label %69, label %7
39: ; preds = %28, %21, %14, %7
%40 = phi i64 [ %8, %7 ], [ %16, %14 ], [ %23, %21 ], [ %30, %28 ]
%41 = phi i32 [ %9, %7 ], [ %15, %14 ], [ %22, %21 ], [ %29, %28 ]
%42 = trunc i64 %40 to i32
%43 = icmp ne i32 %42, 0
%44 = add nsw i32 %42, -1
%45 = icmp ult i32 %41, %44
%46 = select i1 %43, i1 %45, i1 false
br i1 %46, label %47, label %69
47: ; preds = %39
%48 = icmp eq i32 %41, 0
br i1 %48, label %57, label %49
49: ; preds = %47
%50 = call i32 @memset(i8* %4, i32 0, i32 2048)
%51 = call i32 @strncpy(i8* %4, i8* %0, i32 %41)
%52 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 2047
store i8 0, i8* %52
%53 = call i64 @eslib_file_mkdirpath(i8* %4, i32 %1)
%54 = icmp eq i64 %53, 0
br i1 %54, label %57, label %55
55: ; preds = %49
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
br label %69
57: ; preds = %49, %47
%58 = load i32, i32* @O_WRONLY
%59 = load i32, i32* @O_CREAT
%60 = or i32 %59, %58
%61 = call i32 @open(i8* %0, i32 %60, i32 448)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %57
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %65)
br label %69
67: ; preds = %57
%68 = call i32 @close(i32 %61)
br label %69
69: ; preds = %35, %39, %2, %67, %63, %55
%70 = phi i32 [ -1, %55 ], [ -1, %63 ], [ 0, %67 ], [ -1, %2 ], [ -1, %39 ], [ -1, %35 ]
ret i32 %70
}