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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ NAME_MAX;
scalar_t__ PATH_MAX;
scalar_t__ errno;
int mkdir(char *, int);
int stat(char *, struct stat *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
void create_download_dir(char *pathname) {
  char *p, *pdir;
  char dir[PATH_MAX + NAME_MAX + 1];
  struct stat sb;
  strcpy(dir, pathname);
  pdir = dir;
  while ((p = strchr(pdir, '/')) != NULL) {
    *p = 0;
    if (stat(dir, &sb) == -1 && errno == ENOENT) {
      if (mkdir(dir, 0700) == -1)
        ;
    }
    *p++ = '/';
    pdir = p;
  }
}


x86 assembly (clang, -O0):
<create_download_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%rcx
mov    0x2e63(%rip),%rax
mov    (%rax),%rdx
lea    0x1(%rcx,%rdx,1),%rax
mov    %rsp,%rsi
mov    %rsi,-0x20(%rbp)
lea    0x10(%rcx,%rdx,1),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     122a <create_download_dir+0xea>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    120b <create_download_dir+0xcb>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    120b <create_download_dir+0xcb>
mov    -0x38(%rbp),%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1206 <create_download_dir+0xc6>
jmp    1206 <create_download_dir+0xc6>
jmp    120b <create_download_dir+0xcb>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x2f,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1197 <create_download_dir+0x57>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_download_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rsi
mov    0x2e98(%rip),%rax
mov    (%rax),%rax
mov    0x2e5e(%rip),%rcx
mov    (%rcx),%rcx
add    %rcx,%rax
add    $0x10,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11f8 <create_download_dir+0xb8>
mov    %rax,%rbx
lea    -0x30(%rbp),%r15
mov    0x2e37(%rip),%r12
mov    0x2e20(%rip),%r13
jmp    11cc <create_download_dir+0x8c>
nopw   0x0(%rax,%rax,1)
movb   $0x2f,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11f8 <create_download_dir+0xb8>
movb   $0x0,(%rbx)
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11b0 <create_download_dir+0x70>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11b0 <create_download_dir+0x70>
mov    %r14,%rdi
mov    $0x1c0,%esi
call   1040 <mkdir@plt>
jmp    11b0 <create_download_dir+0x70>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i64 0
@NAME_MAX = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define void @create_download_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.stat
store i8* %0, i8** %2
%8 = load i64, i64* @PATH_MAX
%9 = load i64, i64* @NAME_MAX
%10 = add nsw i64 %8, %9
%11 = add nsw i64 %10, 1
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = load i8*, i8** %2
%15 = call i32 @strcpy(i8* %13, i8* %14)
store i8* %13, i8** %4
br label %16
16: ; preds = %33, %1
%17 = load i8*, i8** %4
%18 = call i8* @strchr(i8* %17, i8 signext 47)
store i8* %18, i8** %3
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %37
20: ; preds = %16
%21 = load i8*, i8** %3
store i8 0, i8* %21
%22 = call i32 @stat(i8* %13, %struct.stat* %7)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = call i32 @mkdir(i8* %13, i32 448)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %28
br label %32
32: ; preds = %31, %28
br label %33
33: ; preds = %32, %24, %20
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %3
store i8 47, i8* %34
%36 = load i8*, i8** %3
store i8* %36, i8** %4
br label %16
37: ; preds = %16
%38 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i64 0
@NAME_MAX = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define void @create_download_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = load i64, i64* @PATH_MAX
%4 = load i64, i64* @NAME_MAX
%5 = add i64 %3, 1
%6 = add i64 %5, %4
%7 = alloca i8, i64 %6
%8 = bitcast %struct.stat* %2 to i8*
%9 = call i32 @strcpy(i8* %7, i8* %0)
%10 = call i8* @strchr(i8* %7, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %26, label %12
12: ; preds = %1, %22
%13 = phi i8* [ %24, %22 ], [ %10, %1 ]
store i8 0, i8* %13
%14 = call i32 @stat(i8* %7, %struct.stat* %2)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @mkdir(i8* %7, i32 448)
br label %22
22: ; preds = %20, %16, %12
%23 = getelementptr inbounds i8, i8* %13, i64 1
store i8 47, i8* %13
%24 = call i8* @strchr(i8* %23, i8 signext 47)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %12
26: ; preds = %22, %1
ret void
}