#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 */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *absolute_path(char const *);
int /*<<< orphan*/ free(char *);
scalar_t__ mkpath(char *);
scalar_t__ stat(char *, struct stat *);
char *verify_working_directory(const char *basedir) {
  struct stat sb;
  char *abs_basedir = absolute_path(basedir);
  if ((stat(abs_basedir, &sb) == 0 && !S_ISDIR(sb.st_mode)) ||
      mkpath(abs_basedir) != 0) {
    free(abs_basedir);
    abs_basedir = NULL;
  }
  return abs_basedir;
}


x86 assembly (clang, -O0):
<verify_working_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <absolute_path@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jne    1191 <verify_working_directory+0x41>
mov    -0x10(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
je     11a4 <verify_working_directory+0x54>
mov    -0x18(%rbp),%rdi
call   1060 <mkpath@plt>
cmp    $0x0,%rax
je     11b5 <verify_working_directory+0x65>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_working_directory>:
push   %rbx
sub    $0x10,%rsp
call   1070 <absolute_path@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1050 <stat@plt>
test   %rax,%rax
jne    117c <verify_working_directory+0x2c>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     1189 <verify_working_directory+0x39>
mov    %rbx,%rdi
call   1060 <mkpath@plt>
test   %rax,%rax
je     1193 <verify_working_directory+0x43>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i8* @verify_working_directory(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i8*
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @absolute_path(i8* %5)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = call i64 @stat(i8* %7, %struct.stat* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @S_ISDIR(i32 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10, %1
%16 = load i8*, i8** %4
%17 = call i64 @mkpath(i8* %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15, %10
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
store i8* null, i8** %4
br label %22
22: ; preds = %19, %15
%23 = load i8*, i8** %4
ret i8* %23
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i8* @verify_working_directory(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i8* @absolute_path(i8* %0)
%5 = call i64 @stat(i8* %4, %struct.stat* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @S_ISDIR(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7, %1
%13 = call i64 @mkpath(i8* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12, %7
%16 = call i32 @free(i8* %4)
br label %17
17: ; preds = %15, %12
%18 = phi i8* [ null, %15 ], [ %4, %12 ]
ret i8* %18
}