#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int gl_pathc;
  char **gl_pathv;
};
typedef TYPE_1__ glob_t;

/* Variables and functions */
scalar_t__ _sqlite_exec(char *);
scalar_t__ _sqlite_init();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ glob(char *, int /*<<< orphan*/, int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ globfree(TYPE_1__ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ tdpkg_cache_write_filename(char const *);
int tdpkg_cache_rebuild(void) {
  if (_sqlite_init())
    return -1;
  glob_t glob_list;
  if (glob("/var/lib/dpkg/info/*.list", 0, NULL, &glob_list)) {
    fprintf(stderr, "tdpkg sqlite: can't glob /var/lib/dpkg/info/*.list\n");
    return -1;
  }
  if (_sqlite_exec("DELETE FROM files; BEGIN;")) {
    globfree(&glob_list);
    return -1;
  }
  int i;
  for (i = 0; i < glob_list.gl_pathc; i++) {
    const char *filename = glob_list.gl_pathv[i];
    printf("tdpkg: (Indexing list file %d...)\r", i + 1);
    if (tdpkg_cache_write_filename(filename)) {
      _sqlite_exec("ROLLBACK;");
      globfree(&glob_list);
      return -1;
    }
  }
  globfree(&glob_list);
  if (_sqlite_exec("COMMIT;"))
    return -1;
  printf("tdpkg: %d list files cached succefully\n", i);
  return 0;
}


x86 assembly (clang, -O0):
<tdpkg_cache_rebuild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1040 <_sqlite_init@plt>
cmp    $0x0,%rax
je     1195 <tdpkg_cache_rebuild+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <tdpkg_cache_rebuild+0x14e>
lea    0xe64(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1090 <glob@plt>
cmp    $0x0,%rax
je     11d6 <tdpkg_cache_rebuild+0x66>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <tdpkg_cache_rebuild+0x14e>
lea    0xe71(%rip),%rdi
call   1070 <_sqlite_exec@plt>
cmp    $0x0,%rax
je     1201 <tdpkg_cache_rebuild+0x91>
lea    -0x18(%rbp),%rdi
call   1030 <globfree@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <tdpkg_cache_rebuild+0x14e>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    127d <tdpkg_cache_rebuild+0x10d>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1080 <tdpkg_cache_write_filename@plt>
cmp    $0x0,%rax
je     126a <tdpkg_cache_rebuild+0xfa>
lea    0xe3b(%rip),%rdi
call   1070 <_sqlite_exec@plt>
lea    -0x18(%rbp),%rdi
call   1030 <globfree@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <tdpkg_cache_rebuild+0x14e>
jmp    126f <tdpkg_cache_rebuild+0xff>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1208 <tdpkg_cache_rebuild+0x98>
lea    -0x18(%rbp),%rdi
call   1030 <globfree@plt>
lea    0xe08(%rip),%rdi
call   1070 <_sqlite_exec@plt>
cmp    $0x0,%rax
je     12a8 <tdpkg_cache_rebuild+0x138>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <tdpkg_cache_rebuild+0x14e>
mov    -0x1c(%rbp),%esi
lea    0xdeb(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tdpkg_cache_rebuild>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <_sqlite_init@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1226 <tdpkg_cache_rebuild+0xb6>
lea    0xe68(%rip),%rdi
mov    %rsp,%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1090 <glob@plt>
test   %rax,%rax
je     11c0 <tdpkg_cache_rebuild+0x50>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1221 <tdpkg_cache_rebuild+0xb1>
lea    0xe87(%rip),%rdi
call   1070 <_sqlite_exec@plt>
test   %rax,%rax
jne    1219 <tdpkg_cache_rebuild+0xa9>
xor    %ebx,%ebx
lea    0xe8e(%rip),%r14
nopw   0x0(%rax,%rax,1)
movslq (%rsp),%rax
cmp    %rax,%rbx
jge    1230 <tdpkg_cache_rebuild+0xc0>
mov    0x8(%rsp),%rax
mov    (%rax,%rbx,8),%r15
add    $0x1,%rbx
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1080 <tdpkg_cache_write_filename@plt>
test   %rax,%rax
je     11e0 <tdpkg_cache_rebuild+0x70>
lea    0xe77(%rip),%rdi
call   1070 <_sqlite_exec@plt>
mov    %rsp,%rdi
call   1030 <globfree@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rsp,%rdi
call   1030 <globfree@plt>
lea    0xe56(%rip),%rdi
call   1070 <_sqlite_exec@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1226 <tdpkg_cache_rebuild+0xb6>
lea    0xe45(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
jmp    1226 <tdpkg_cache_rebuild+0xb6>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i8** }
@.str = [26 x i8] c"/var/lib/dpkg/info/*.list\00"
@stderr = global i32 0
@.str.1 = [52 x i8] c"tdpkg sqlite: can't glob /var/lib/dpkg/info/*.list\0A\00"
@.str.2 = [26 x i8] c"DELETE FROM files; BEGIN;\00"
@.str.3 = [35 x i8] c"tdpkg: (Indexing list file %d...)\0D\00"
@.str.4 = [10 x i8] c"ROLLBACK;\00"
@.str.5 = [8 x i8] c"COMMIT;\00"
@.str.6 = [40 x i8] c"tdpkg: %d list files cached succefully\0A\00"
define i32 @tdpkg_cache_rebuild() {
%1 = alloca i32
%2 = alloca %struct.TYPE_4__
%3 = alloca i32
%4 = alloca i8*
%5 = call i64 (...) @_sqlite_init()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %0
store i32 -1, i32* %1
br label %53
8: ; preds = %0
%9 = call i64 @glob(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 0, i32* null, %struct.TYPE_4__* %2)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %53
14: ; preds = %8
%15 = call i64 @_sqlite_exec(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @globfree(%struct.TYPE_4__* %2)
store i32 -1, i32* %1
br label %53
19: ; preds = %14
store i32 0, i32* %3
br label %20
20: ; preds = %42, %19
%21 = load i32, i32* %3
%22 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %45
25: ; preds = %20
%26 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %2, i32 0, i32 1
%27 = load i8**, i8*** %26
%28 = load i32, i32* %3
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %27, i64 %29
%31 = load i8*, i8** %30
store i8* %31, i8** %4
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
%34 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %4
%36 = call i64 @tdpkg_cache_write_filename(i8* %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %25
%39 = call i64 @_sqlite_exec(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%40 = call i32 @globfree(%struct.TYPE_4__* %2)
store i32 -1, i32* %1
br label %53
41: ; preds = %25
br label %42
42: ; preds = %41
%43 = load i32, i32* %3
%44 = add nsw i32 %43, 1
store i32 %44, i32* %3
br label %20
45: ; preds = %20
%46 = call i32 @globfree(%struct.TYPE_4__* %2)
%47 = call i64 @_sqlite_exec(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 -1, i32* %1
br label %53
50: ; preds = %45
%51 = load i32, i32* %3
%52 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %51)
store i32 0, i32* %1
br label %53
53: ; preds = %50, %49, %38, %17, %11, %7
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i8** }
@.str = [26 x i8] c"/var/lib/dpkg/info/*.list\00"
@stderr = global i32 0
@.str.1 = [52 x i8] c"tdpkg sqlite: can't glob /var/lib/dpkg/info/*.list\0A\00"
@.str.2 = [26 x i8] c"DELETE FROM files; BEGIN;\00"
@.str.3 = [35 x i8] c"tdpkg: (Indexing list file %d...)\0D\00"
@.str.4 = [10 x i8] c"ROLLBACK;\00"
@.str.5 = [8 x i8] c"COMMIT;\00"
@.str.6 = [40 x i8] c"tdpkg: %d list files cached succefully\0A\00"
define i32 @tdpkg_cache_rebuild() {
%1 = alloca %struct.TYPE_4__
%2 = tail call i64 (...) @_sqlite_init()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %45
4: ; preds = %0
%5 = bitcast %struct.TYPE_4__* %1 to i8*
%6 = call i64 @glob(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 0, i32* null, %struct.TYPE_4__* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %43
11: ; preds = %4
%12 = call i64 @_sqlite_exec(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %1, i64 0, i32 1
br label %19
17: ; preds = %11
%18 = call i32 @globfree(%struct.TYPE_4__* %1)
br label %43
19: ; preds = %14, %24
%20 = phi i64 [ 0, %14 ], [ %28, %24 ]
%21 = load i32, i32* %15
%22 = sext i32 %21 to i64
%23 = icmp slt i64 %20, %22
br i1 %23, label %24, label %36
24: ; preds = %19
%25 = load i8**, i8*** %16
%26 = getelementptr inbounds i8*, i8** %25, i64 %20
%27 = load i8*, i8** %26
%28 = add nuw nsw i64 %20, 1
%29 = trunc i64 %28 to i32
%30 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = call i64 @tdpkg_cache_write_filename(i8* %27)
%32 = icmp eq i64 %31, 0
br i1 %32, label %19, label %33
33: ; preds = %24
%34 = call i64 @_sqlite_exec(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @globfree(%struct.TYPE_4__* %1)
br label %43
36: ; preds = %19
%37 = call i32 @globfree(%struct.TYPE_4__* %1)
%38 = call i64 @_sqlite_exec(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = trunc i64 %20 to i32
%42 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %33, %40, %36, %17, %8
%44 = phi i32 [ -1, %8 ], [ -1, %17 ], [ -1, %33 ], [ 0, %40 ], [ -1, %36 ]
br label %45
45: ; preds = %0, %43
%46 = phi i32 [ %44, %43 ], [ -1, %0 ]
ret i32 %46
}