#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 */

/* Variables and functions */
int O_RDONLY;
int SQLITE_OK;
int robust_open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sqlite3CantopenError(int);
int /*<<< orphan*/ sqlite3_snprintf(int, char *, char *, char const *);
scalar_t__ strlen(char *);
int unixLogErrorAtLine(int /*<<< orphan*/, char *, char *, int);
int openDirectory(const char *zFilename, int *pFd) {
  int ii;
  int fd = -1;
  char zDirname[512 + 1];
  sqlite3_snprintf(512, zDirname, "%s", zFilename);
  for (ii = (int)strlen(zDirname); ii > 1 && zDirname[ii] != '/'; ii--) {
    ;
  }
  if (ii > 0) {
    zDirname[ii] = '\0';
    fd = robust_open(zDirname, O_RDONLY | 0, 0);
  }
  *pFd = fd;
  return (fd >= 0 ? SQLITE_OK
                  : unixLogErrorAtLine(sqlite3CantopenError(569), "open",
                                       zDirname, 569));
}


x86 assembly (clang, -O0):
<openDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
lea    -0x220(%rbp),%rsi
mov    -0x8(%rbp),%rcx
mov    $0x200,%edi
lea    0xe7f(%rip),%rdx
call   1060 <sqlite3_snprintf@plt>
lea    -0x220(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x14(%rbp)
mov    %al,-0x221(%rbp)
jle    11bf <openDirectory+0x6f>
movslq -0x14(%rbp),%rax
movsbl -0x220(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x221(%rbp)
mov    -0x221(%rbp),%al
test   $0x1,%al
jne    11d2 <openDirectory+0x82>
jmp    11e5 <openDirectory+0x95>
jmp    11d7 <openDirectory+0x87>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1195 <openDirectory+0x45>
cmpl   $0x0,-0x14(%rbp)
jle    1218 <openDirectory+0xc8>
movslq -0x14(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
xor    %edx,%edx
call   1070 <robust_open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x18(%rbp)
jl     123f <openDirectory+0xef>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x228(%rbp)
jmp    1269 <openDirectory+0x119>
mov    $0x239,%edi
call   1050 <sqlite3CantopenError@plt>
mov    %eax,%edi
lea    -0x220(%rbp),%rdx
lea    0xdaa(%rip),%rsi
mov    $0x239,%ecx
call   1040 <unixLogErrorAtLine@plt>
mov    %eax,-0x228(%rbp)
mov    -0x228(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openDirectory>:
push   %r14
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %rdi,%rcx
lea    0xe99(%rip),%rdx
mov    %rsp,%rbx
mov    $0x200,%edi
mov    %rbx,%rsi
call   1060 <sqlite3_snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x2,%eax
jl     11a9 <openDirectory+0x59>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%ecx
cmpb   $0x2f,(%rsp,%rcx,1)
je     11b3 <openDirectory+0x63>
lea    -0x1(%rax),%ecx
cmp    $0x2,%eax
mov    %ecx,%eax
jg     1190 <openDirectory+0x40>
mov    $0x1,%eax
jmp    11b3 <openDirectory+0x63>
cmp    $0x1,%eax
mov    $0x1,%eax
jne    11de <openDirectory+0x8e>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
xor    %edx,%edx
call   1070 <robust_open@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     11e5 <openDirectory+0x95>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
jmp    1205 <openDirectory+0xb5>
movl   $0xffffffff,(%r14)
mov    $0x239,%edi
call   1050 <sqlite3CantopenError@plt>
lea    0xe0d(%rip),%rsi
mov    %rsp,%rdx
mov    %eax,%edi
mov    $0x239,%ecx
call   1040 <unixLogErrorAtLine@plt>
add    $0x208,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@SQLITE_OK = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @openDirectory(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [513 x i8]
store i8* %0, i8** %3
store i32* %1, i32** %4
store i32 -1, i32* %6
%8 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 @sqlite3_snprintf(i32 512, i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%12 = call i64 @strlen(i8* %11)
%13 = trunc i64 %12 to i32
store i32 %13, i32* %5
br label %14
14: ; preds = %27, %2
%15 = load i32, i32* %5
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 47
br label %24
24: ; preds = %17, %14
%25 = phi i1 [ false, %14 ], [ %23, %17 ]
br i1 %25, label %26, label %30
26: ; preds = %24
br label %27
27: ; preds = %26
%28 = load i32, i32* %5
%29 = add nsw i32 %28, -1
store i32 %29, i32* %5
br label %14
30: ; preds = %24
%31 = load i32, i32* %5
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %30
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 %35
store i8 0, i8* %36
%37 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%38 = load i32, i32* @O_RDONLY
%39 = or i32 %38, 0
%40 = call i32 @robust_open(i8* %37, i32 %39, i32 0)
store i32 %40, i32* %6
br label %41
41: ; preds = %33, %30
%42 = load i32, i32* %6
%43 = load i32*, i32** %4
store i32 %42, i32* %43
%44 = load i32, i32* %6
%45 = icmp sge i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = load i32, i32* @SQLITE_OK
br label %52
48: ; preds = %41
%49 = call i32 @sqlite3CantopenError(i32 569)
%50 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%51 = call i32 @unixLogErrorAtLine(i32 %49, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %50, i32 569)
br label %52
52: ; preds = %48, %46
%53 = phi i32 [ %47, %46 ], [ %51, %48 ]
ret i32 %53
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@O_RDONLY = global i32 0
@SQLITE_OK = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @openDirectory(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [513 x i8]
%4 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 0
%5 = call i32 @sqlite3_snprintf(i32 512, i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = call i64 @strlen(i8* %4)
%7 = trunc i64 %6 to i32
%8 = icmp sgt i32 %7, 1
br i1 %8, label %9, label %18
9: ; preds = %2, %15
%10 = phi i32 [ %16, %15 ], [ %7, %2 ]
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 47
br i1 %14, label %21, label %15
15: ; preds = %9
%16 = add nsw i32 %10, -1
%17 = icmp sgt i32 %10, 2
br i1 %17, label %9, label %21
18: ; preds = %2
%19 = icmp eq i32 %7, 1
br i1 %19, label %21, label %20
20: ; preds = %18
store i32 -1, i32* %1
br label %30
21: ; preds = %15, %9, %18
%22 = phi i32 [ 1, %18 ], [ 1, %15 ], [ %10, %9 ]
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 %23
store i8 0, i8* %24
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @robust_open(i8* %4, i32 %25, i32 0)
store i32 %26, i32* %1
%27 = icmp sgt i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32, i32* @SQLITE_OK
br label %33
30: ; preds = %20, %21
%31 = call i32 @sqlite3CantopenError(i32 569)
%32 = call i32 @unixLogErrorAtLine(i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 569)
br label %33
33: ; preds = %30, %28
%34 = phi i32 [ %29, %28 ], [ %32, %30 ]
ret i32 %34
}