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

/* Variables and functions */
int /*<<< orphan*/ DPRINTF(int, int, char *, char *);
int PATH_MAX;
int db_upgrade_v19_directory_id(int /*<<< orphan*/ *, char *);
int db_upgrade_v19_insert_directory(int /*<<< orphan*/ *, char *, int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
int db_upgrade_v19_insert_parent_directories(sqlite3 *hdl, char *virtual_path) {
  char *ptr;
  int dir_id;
  int parent_id;
  char buf[PATH_MAX];
  parent_id = 1;
  ptr = virtual_path + 1;
  while (ptr && (ptr = strchr(ptr, '/'))) {
    strncpy(buf, virtual_path, (ptr - virtual_path));
    buf[(ptr - virtual_path)] = '\0';
    dir_id = db_upgrade_v19_directory_id(hdl, buf);
    if (dir_id < 0) {
      DPRINTF(1, 9, "Select of directory failed '%s'\n", buf);
      return -1;
    } else if (dir_id == 0) {
      dir_id = db_upgrade_v19_insert_directory(hdl, buf, parent_id);
      if (dir_id < 0) {
        DPRINTF(1, 9, "Insert of directory failed '%s'\n", buf);
        return -1;
      }
    }
    parent_id = dir_id;
    ptr++;
  }
  return parent_id;
}


x86 assembly (clang, -O0):
<db_upgrade_v19_insert_parent_directories>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x49(%rbp)
je     11cb <db_upgrade_v19_insert_parent_directories+0x7b>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11db <db_upgrade_v19_insert_parent_directories+0x8b>
jmp    12c0 <db_upgrade_v19_insert_parent_directories+0x170>
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,(%rsi,%rax,1)
mov    -0x10(%rbp),%rdi
call   1060 <db_upgrade_v19_directory_id@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    124b <db_upgrade_v19_insert_parent_directories+0xfb>
mov    -0x48(%rbp),%rcx
mov    $0x1,%edi
mov    $0x9,%esi
lea    0xdcd(%rip),%rdx
call   1070 <DPRINTF@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12cd <db_upgrade_v19_insert_parent_directories+0x17d>
cmpl   $0x0,-0x24(%rbp)
jne    12a4 <db_upgrade_v19_insert_parent_directories+0x154>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%edx
call   1040 <db_upgrade_v19_insert_directory@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    129f <db_upgrade_v19_insert_parent_directories+0x14f>
mov    -0x48(%rbp),%rcx
mov    $0x1,%edi
mov    $0x9,%esi
lea    0xd9a(%rip),%rdx
call   1070 <DPRINTF@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12cd <db_upgrade_v19_insert_parent_directories+0x17d>
jmp    12a4 <db_upgrade_v19_insert_parent_directories+0x154>
jmp    12a9 <db_upgrade_v19_insert_parent_directories+0x159>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119f <db_upgrade_v19_insert_parent_directories+0x4f>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_upgrade_v19_insert_parent_directories>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,-0x30(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %rsi,-0x38(%rbp)
lea    0x1(%rsi),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     121a <db_upgrade_v19_insert_parent_directories+0xca>
mov    %rax,%r14
mov    $0x1,%r15d
jmp    11cc <db_upgrade_v19_insert_parent_directories+0x7c>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%r14
mov    %r12d,%r15d
test   %rax,%rax
je     1241 <db_upgrade_v19_insert_parent_directories+0xf1>
mov    %r14,%rbx
mov    -0x38(%rbp),%rsi
sub    %rsi,%rbx
mov    %r13,%rdi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x0(%r13,%rbx,1)
mov    -0x30(%rbp),%rdi
mov    %r13,%rsi
call   1060 <db_upgrade_v19_directory_id@plt>
test   %eax,%eax
js     1222 <db_upgrade_v19_insert_parent_directories+0xd2>
mov    %eax,%r12d
jne    11b0 <db_upgrade_v19_insert_parent_directories+0x60>
mov    -0x30(%rbp),%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1040 <db_upgrade_v19_insert_directory@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    11b0 <db_upgrade_v19_insert_parent_directories+0x60>
lea    0xe09(%rip),%rdx
jmp    1229 <db_upgrade_v19_insert_parent_directories+0xd9>
mov    $0x1,%r12d
jmp    1241 <db_upgrade_v19_insert_parent_directories+0xf1>
lea    0xdd7(%rip),%rdx
mov    $0x1,%edi
mov    $0x9,%esi
mov    %r13,%rcx
call   1070 <DPRINTF@plt>
mov    $0xffffffff,%r12d
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [33 x i8] c"Select of directory failed '%s'\0A\00"
@.str.1 = [33 x i8] c"Insert of directory failed '%s'\0A\00"
define i32 @db_upgrade_v19_insert_parent_directories(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %9
%15 = alloca i8, i64 %13
store i64 %13, i64* %10
store i32 1, i32* %8
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %6
br label %18
18: ; preds = %61, %2
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** %6
%23 = call i8* @strchr(i8* %22, i8 signext 47)
store i8* %23, i8** %6
%24 = icmp ne i8* %23, null
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %27, label %65
27: ; preds = %25
%28 = load i8*, i8** %5
%29 = load i8*, i8** %6
%30 = load i8*, i8** %5
%31 = ptrtoint i8* %29 to i64
%32 = ptrtoint i8* %30 to i64
%33 = sub i64 %31, %32
%34 = trunc i64 %33 to i32
%35 = call i32 @strncpy(i8* %15, i8* %28, i32 %34)
%36 = load i8*, i8** %6
%37 = load i8*, i8** %5
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = getelementptr inbounds i8, i8* %15, i64 %40
store i8 0, i8* %41
%42 = load i32*, i32** %4
%43 = call i32 @db_upgrade_v19_directory_id(i32* %42, i8* %15)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %27
%47 = call i32 @DPRINTF(i32 1, i32 9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
store i32 1, i32* %11
br label %67
48: ; preds = %27
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = load i32*, i32** %4
%53 = load i32, i32* %8
%54 = call i32 @db_upgrade_v19_insert_directory(i32* %52, i8* %15, i32 %53)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @DPRINTF(i32 1, i32 9, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
store i32 1, i32* %11
br label %67
59: ; preds = %51
br label %60
60: ; preds = %59, %48
br label %61
61: ; preds = %60
%62 = load i32, i32* %7
store i32 %62, i32* %8
%63 = load i8*, i8** %6
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %6
br label %18
65: ; preds = %25
%66 = load i32, i32* %8
store i32 %66, i32* %3
store i32 1, i32* %11
br label %67
67: ; preds = %65, %57, %46
%68 = load i8*, i8** %9
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [33 x i8] c"Select of directory failed '%s'\0A\00"
@.str.1 = [33 x i8] c"Insert of directory failed '%s'\0A\00"
define i32 @db_upgrade_v19_insert_parent_directories(i32* %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = call i8* @strchr(i8* %6, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %34, label %9
9: ; preds = %2
%10 = ptrtoint i8* %1 to i64
br label %11
11: ; preds = %9, %26
%12 = phi i8* [ %7, %9 ], [ %29, %26 ]
%13 = phi i32 [ 1, %9 ], [ %27, %26 ]
%14 = ptrtoint i8* %12 to i64
%15 = sub i64 %14, %10
%16 = trunc i64 %15 to i32
%17 = call i32 @strncpy(i8* %5, i8* %1, i32 %16)
%18 = getelementptr inbounds i8, i8* %5, i64 %15
store i8 0, i8* %18
%19 = call i32 @db_upgrade_v19_directory_id(i32* %0, i8* %5)
%20 = icmp slt i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %11
%22 = icmp eq i32 %19, 0
br i1 %22, label %23, label %26
23: ; preds = %21
%24 = call i32 @db_upgrade_v19_insert_directory(i32* %0, i8* %5, i32 %13)
%25 = icmp slt i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %21, %23
%27 = phi i32 [ %24, %23 ], [ %19, %21 ]
%28 = getelementptr inbounds i8, i8* %12, i64 1
%29 = call i8* @strchr(i8* %28, i8 signext 47)
%30 = icmp eq i8* %29, null
br i1 %30, label %34, label %11
31: ; preds = %23, %11
%32 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %23 ]
%33 = call i32 @DPRINTF(i32 1, i32 9, i8* %32, i8* %5)
br label %34
34: ; preds = %26, %31, %2
%35 = phi i32 [ 1, %2 ], [ -1, %31 ], [ %27, %26 ]
ret i32 %35
}