#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 st_mode;
};
typedef int mode_t;

/* Variables and functions */
int SQLITE_IOERR_FSTAT;
int SQLITE_OK;
int SQLITE_OPEN_DELETEONCLOSE;
int SQLITE_OPEN_MAIN_JOURNAL;
int /*<<< orphan*/ memcpy(char *, char const *, int);
int sqlite3Strlen30(char const *);
scalar_t__ stat(char *, struct stat *);
int findCreateFileMode(const char *zPath, int flags, mode_t *pMode) {
  int rc = SQLITE_OK;
  if (flags & (0x00080000 | SQLITE_OPEN_MAIN_JOURNAL)) {
    char zDb[512 + 1];
    int nDb;
    struct stat sStat;
    nDb = sqlite3Strlen30(zPath) - 1;
    while (nDb > 0 && zPath[nDb] != 'l') {
      nDb--;
    }
    nDb -= ((flags & 0x00080000) ? 3 : 7);
    memcpy(zDb, zPath, nDb);
    zDb[nDb] = '\0';
    if (0 == stat(zDb, &sStat)) {
      *pMode = sStat.st_mode & 0777;
    } else {
      rc = SQLITE_IOERR_FSTAT;
    }
  } else if (flags & SQLITE_OPEN_DELETEONCLOSE) {
    *pMode = 0600;
  } else {
    *pMode = 0644;
  }
  return rc;
}


x86 assembly (clang, -O0):
<findCreateFileMode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%ecx
or     $0x80000,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     126d <findCreateFileMode+0x13d>
mov    -0x8(%rbp),%rdi
call   1030 <sqlite3Strlen30@plt>
sub    $0x1,%eax
mov    %eax,-0x224(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x224(%rbp)
mov    %al,-0x229(%rbp)
jle    11b1 <findCreateFileMode+0x81>
mov    -0x8(%rbp),%rax
movslq -0x224(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x6c,%eax
setne  %al
mov    %al,-0x229(%rbp)
mov    -0x229(%rbp),%al
test   $0x1,%al
jne    11c4 <findCreateFileMode+0x94>
jmp    11d8 <findCreateFileMode+0xa8>
mov    -0x224(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x224(%rbp)
jmp    1181 <findCreateFileMode+0x51>
mov    -0xc(%rbp),%edx
and    $0x80000,%edx
mov    $0x7,%ecx
mov    $0x3,%eax
cmp    $0x0,%edx
cmovne %eax,%ecx
mov    -0x224(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x224(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1050 <memcpy@plt>
movslq -0x224(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
lea    -0x228(%rbp),%rsi
call   1040 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    125c <findCreateFileMode+0x12c>
mov    -0x228(%rbp),%ecx
and    $0x1ff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    1268 <findCreateFileMode+0x138>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    12a0 <findCreateFileMode+0x170>
mov    -0xc(%rbp),%eax
mov    0x2d61(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1291 <findCreateFileMode+0x161>
mov    -0x18(%rbp),%rax
movl   $0x180,(%rax)
jmp    129b <findCreateFileMode+0x16b>
mov    -0x18(%rbp),%rax
movl   $0x1a4,(%rax)
jmp    12a0 <findCreateFileMode+0x170>
mov    -0x1c(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findCreateFileMode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x220,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    0x2e85(%rip),%rax
mov    (%rax),%r15d
mov    0x2e8b(%rip),%rax
mov    $0x80000,%ecx
or     (%rax),%ecx
test   %esi,%ecx
je     11d6 <findCreateFileMode+0xa6>
mov    %rdi,%rbx
call   1030 <sqlite3Strlen30@plt>
mov    %eax,%edx
test   %eax,%eax
mov    $0x1,%ecx
cmovle %eax,%ecx
add    $0xffffffff,%ecx
nopw   0x0(%rax,%rax,1)
cmp    $0x2,%edx
jl     1193 <findCreateFileMode+0x63>
add    $0xffffffffffffffff,%rdx
mov    %edx,%eax
cmpb   $0x6c,(%rbx,%rax,1)
jne    1180 <findCreateFileMode+0x50>
mov    %edx,%ecx
shr    $0x11,%ebp
and    $0x4,%ebp
add    %ecx,%ebp
add    $0xfffffff9,%ebp
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1040 <stat@plt>
test   %rax,%rax
je     11ea <findCreateFileMode+0xba>
mov    0x2e27(%rip),%rax
mov    (%rax),%r15d
jmp    11ff <findCreateFileMode+0xcf>
mov    0x2dfb(%rip),%rax
test   %ebp,(%rax)
je     11f8 <findCreateFileMode+0xc8>
movl   $0x180,(%r14)
jmp    11ff <findCreateFileMode+0xcf>
mov    $0x1ff,%eax
and    0x8(%rsp),%eax
mov    %eax,(%r14)
jmp    11ff <findCreateFileMode+0xcf>
movl   $0x1a4,(%r14)
mov    %r15d,%eax
add    $0x220,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@SQLITE_OK = global i32 0
@SQLITE_OPEN_MAIN_JOURNAL = global i32 0
@SQLITE_IOERR_FSTAT = global i32 0
@SQLITE_OPEN_DELETEONCLOSE = global i32 0
define i32 @findCreateFileMode(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [513 x i8]
%9 = alloca i32
%10 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%11 = load i32, i32* @SQLITE_OK
store i32 %11, i32* %7
%12 = load i32, i32* %5
%13 = load i32, i32* @SQLITE_OPEN_MAIN_JOURNAL
%14 = or i32 524288, %13
%15 = and i32 %12, %14
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %63
17: ; preds = %3
%18 = load i8*, i8** %4
%19 = call i32 @sqlite3Strlen30(i8* %18)
%20 = sub nsw i32 %19, 1
store i32 %20, i32* %9
br label %21
21: ; preds = %34, %17
%22 = load i32, i32* %9
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load i8*, i8** %4
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp ne i32 %30, 108
br label %32
32: ; preds = %24, %21
%33 = phi i1 [ false, %21 ], [ %31, %24 ]
br i1 %33, label %34, label %37
34: ; preds = %32
%35 = load i32, i32* %9
%36 = add nsw i32 %35, -1
store i32 %36, i32* %9
br label %21
37: ; preds = %32
%38 = load i32, i32* %5
%39 = and i32 %38, 524288
%40 = icmp ne i32 %39, 0
%41 = zext i1 %40 to i64
%42 = select i1 %40, i32 3, i32 7
%43 = load i32, i32* %9
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %9
%45 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%46 = load i8*, i8** %4
%47 = load i32, i32* %9
%48 = call i32 @memcpy(i8* %45, i8* %46, i32 %47)
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 %50
store i8 0, i8* %51
%52 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%53 = call i64 @stat(i8* %52, %struct.stat* %10)
%54 = icmp eq i64 0, %53
br i1 %54, label %55, label %60
55: ; preds = %37
%56 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%57 = load i32, i32* %56
%58 = and i32 %57, 511
%59 = load i32*, i32** %6
store i32 %58, i32* %59
br label %62
60: ; preds = %37
%61 = load i32, i32* @SQLITE_IOERR_FSTAT
store i32 %61, i32* %7
br label %62
62: ; preds = %60, %55
br label %73
63: ; preds = %3
%64 = load i32, i32* %5
%65 = load i32, i32* @SQLITE_OPEN_DELETEONCLOSE
%66 = and i32 %64, %65
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = load i32*, i32** %6
store i32 384, i32* %69
br label %72
70: ; preds = %63
%71 = load i32*, i32** %6
store i32 420, i32* %71
br label %72
72: ; preds = %70, %68
br label %73
73: ; preds = %72, %62
%74 = load i32, i32* %7
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@SQLITE_OK = global i32 0
@SQLITE_OPEN_MAIN_JOURNAL = global i32 0
@SQLITE_IOERR_FSTAT = global i32 0
@SQLITE_OPEN_DELETEONCLOSE = global i32 0
define i32 @findCreateFileMode(i8* %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca [513 x i8]
%5 = alloca %struct.stat
%6 = load i32, i32* @SQLITE_OK
%7 = load i32, i32* @SQLITE_OPEN_MAIN_JOURNAL
%8 = or i32 %7, 524288
%9 = and i32 %8, %1
%10 = icmp eq i32 %9, 0
br i1 %10, label %49, label %11
11: ; preds = %3
%12 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 0
%13 = bitcast %struct.stat* %5 to i8*
%14 = tail call i32 @sqlite3Strlen30(i8* %0)
%15 = zext i32 %14 to i64
%16 = call i32 @llvm.smin.i32(i32 %14, i32 1)
%17 = add i32 %16, -1
br label %18
18: ; preds = %23, %11
%19 = phi i64 [ %20, %23 ], [ %15, %11 ]
%20 = add nsw i64 %19, -1
%21 = trunc i64 %19 to i32
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = and i64 %20, 4294967295
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 108
br i1 %27, label %28, label %18
28: ; preds = %23
%29 = trunc i64 %20 to i32
br label %30
30: ; preds = %18, %28
%31 = phi i32 [ %29, %28 ], [ %17, %18 ]
%32 = and i32 %1, 524288
%33 = icmp eq i32 %32, 0
%34 = select i1 %33, i32 -7, i32 -3
%35 = add i32 %31, %34
%36 = call i32 @memcpy(i8* %12, i8* %0, i32 %35)
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds [513 x i8], [513 x i8]* %4, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i64 @stat(i8* %12, %struct.stat* %5)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %30
%42 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%43 = load i32, i32* %42
%44 = and i32 %43, 511
store i32 %44, i32* %2
br label %47
45: ; preds = %30
%46 = load i32, i32* @SQLITE_IOERR_FSTAT
br label %47
47: ; preds = %45, %41
%48 = phi i32 [ %6, %41 ], [ %46, %45 ]
br label %55
49: ; preds = %3
%50 = load i32, i32* @SQLITE_OPEN_DELETEONCLOSE
%51 = and i32 %50, %1
%52 = icmp eq i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %49
store i32 384, i32* %2
br label %55
54: ; preds = %49
store i32 420, i32* %2
br label %55
55: ; preds = %53, %54, %47
%56 = phi i32 [ %48, %47 ], [ %6, %53 ], [ %6, %54 ]
ret i32 %56
}