; LLVM IR from flomar/sqlmemdb :: sqlite (sqlite3)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.sqlite3_index_info = type { i32, ptr, i32, ptr, ptr, i32, ptr, i32, i32, double, i64, i32, i64 }
%struct.sqlite3_index_constraint = type { i32, i8, i8, i32 }
%struct.HiddenIndexInfo = type { ptr, ptr }
%struct.WhereClause = type { ptr, ptr, i8, i8, i32, i32, ptr, [8 x %struct.WhereTerm] }
%struct.WhereTerm = type { ptr, ptr, i16, i16, i16, i8, i8, i32, i32, i32, %union.anon.2, i64, i64 }
%union.anon.2 = type { ptr }
%struct.Expr = type { i8, i8, i32, %union.anon.3, ptr, ptr, %union.anon.4, i32, i32, i16, i16, i16, i8, ptr, %union.anon.5 }
%union.anon.3 = type { ptr }
%union.anon.4 = type { ptr }
%union.anon.5 = type { ptr }
%struct.CollSeq = type { ptr, i8, ptr, ptr, ptr }

@sqlite3StrBINARY = external hidden constant [7 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: nounwind uwtable
define dso_local ptr @sqlite3_vtab_collation(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.sqlite3_index_info, ptr %10, i64 1
  store ptr %11, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store ptr null, ptr %6, align 8, !tbaa !5
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = icmp sge i32 %12, 0
  br i1 %13, label %14, label %64

14:                                               ; preds = %2
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.sqlite3_index_info, ptr %16, i32 0, i32 0
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp slt i32 %15, %18
  br i1 %19, label %20, label %64

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store ptr null, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.sqlite3_index_info, ptr %21, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = load i32, ptr %4, align 4, !tbaa !9
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds %struct.sqlite3_index_constraint, ptr %23, i64 %25
  %27 = getelementptr inbounds %struct.sqlite3_index_constraint, ptr %26, i32 0, i32 3
  %28 = load i32, ptr %27, align 4, !tbaa !16
  store i32 %28, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.HiddenIndexInfo, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !18
  %32 = getelementptr inbounds %struct.WhereClause, ptr %31, i32 0, i32 6
  %33 = load ptr, ptr %32, align 8, !tbaa !20
  %34 = load i32, ptr %8, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds %struct.WhereTerm, ptr %33, i64 %35
  %37 = getelementptr inbounds %struct.WhereTerm, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !22
  store ptr %38, ptr %9, align 8, !tbaa !5
  %39 = load ptr, ptr %9, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Expr, ptr %39, i32 0, i32 4
  %41 = load ptr, ptr %40, align 8, !tbaa !25
  %42 = icmp ne ptr %41, null
  br i1 %42, label %43, label %54

43:                                               ; preds = %20
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.HiddenIndexInfo, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !27
  %47 = load ptr, ptr %9, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.Expr, ptr %47, i32 0, i32 4
  %49 = load ptr, ptr %48, align 8, !tbaa !25
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.Expr, ptr %50, i32 0, i32 5
  %52 = load ptr, ptr %51, align 8, !tbaa !28
  %53 = call ptr @sqlite3BinaryCompareCollSeq(ptr noundef %46, ptr noundef %49, ptr noundef %52)
  store ptr %53, ptr %7, align 8, !tbaa !5
  br label %54

54:                                               ; preds = %43, %20
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %61

57:                                               ; preds = %54
  %58 = load ptr, ptr %7, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.CollSeq, ptr %58, i32 0, i32 0
  %60 = load ptr, ptr %59, align 8, !tbaa !29
  br label %62

61:                                               ; preds = %54
  br label %62

62:                                               ; preds = %61, %57
  %63 = phi ptr [ %60, %57 ], [ @sqlite3StrBINARY, %61 ]
  store ptr %63, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  br label %64

64:                                               ; preds = %62, %14, %2
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret ptr %65
}

; Function Attrs: nounwind uwtable
declare hidden ptr @sqlite3BinaryCompareCollSeq(ptr noundef, ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"sqlite3_index_info", !10, i64 0, !6, i64 8, !10, i64 16, !6, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !10, i64 56, !10, i64 60, !13, i64 64, !14, i64 72, !10, i64 80, !14, i64 88}
!13 = !{!"double", !7, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = !{!12, !6, i64 8}
!16 = !{!17, !10, i64 8}
!17 = !{!"sqlite3_index_constraint", !10, i64 0, !7, i64 4, !7, i64 5, !10, i64 8}
!18 = !{!19, !6, i64 0}
!19 = !{!"HiddenIndexInfo", !6, i64 0, !6, i64 8}
!20 = !{!21, !6, i64 32}
!21 = !{!"WhereClause", !6, i64 0, !6, i64 8, !7, i64 16, !7, i64 17, !10, i64 20, !10, i64 24, !6, i64 32, !7, i64 40}
!22 = !{!23, !6, i64 0}
!23 = !{!"WhereTerm", !6, i64 0, !6, i64 8, !24, i64 16, !24, i64 18, !24, i64 20, !7, i64 22, !7, i64 23, !10, i64 24, !10, i64 28, !10, i64 32, !7, i64 40, !14, i64 48, !14, i64 56}
!24 = !{!"short", !7, i64 0}
!25 = !{!26, !6, i64 16}
!26 = !{!"Expr", !7, i64 0, !7, i64 1, !10, i64 4, !7, i64 8, !6, i64 16, !6, i64 24, !7, i64 32, !10, i64 40, !10, i64 44, !24, i64 48, !24, i64 50, !24, i64 52, !7, i64 54, !6, i64 56, !7, i64 64}
!27 = !{!19, !6, i64 8}
!28 = !{!26, !6, i64 24}
!29 = !{!30, !6, i64 0}
!30 = !{!"CollSeq", !6, i64 0, !7, i64 8, !6, i64 16, !6, i64 24, !6, i64 32}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

%struct.sqlite3_index_constraint = type { i32, i8, i8, i32 }
%struct.WhereTerm = type { ptr, ptr, i16, i16, i16, i8, i8, i32, i32, i32, %union.anon.2, i64, i64 }
%union.anon.2 = type { ptr }

@sqlite3StrBINARY = external hidden constant [7 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @sqlite3_vtab_collation(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 96
  %4 = icmp sgt i32 %1, -1
  br i1 %4, label %5, label %.thread

5:                                                ; preds = %2
  %6 = load i32, ptr %0, align 8, !tbaa !5
  %7 = icmp sgt i32 %6, %1
  br i1 %7, label %8, label %.thread

8:                                                ; preds = %5
  %9 = getelementptr inbounds i8, ptr %0, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !13
  %11 = zext nneg i32 %1 to i64
  %12 = getelementptr inbounds %struct.sqlite3_index_constraint, ptr %10, i64 %11, i32 3
  %13 = load i32, ptr %12, align 4, !tbaa !14
  %14 = load ptr, ptr %3, align 8, !tbaa !16
  %15 = getelementptr inbounds i8, ptr %14, i64 32
  %16 = load ptr, ptr %15, align 8, !tbaa !18
  %17 = sext i32 %13 to i64
  %18 = getelementptr inbounds %struct.WhereTerm, ptr %16, i64 %17
  %19 = load ptr, ptr %18, align 8, !tbaa !20
  %20 = getelementptr inbounds i8, ptr %19, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !23
  %.not = icmp eq ptr %21, null
  br i1 %.not, label %.thread, label %22

22:                                               ; preds = %8
  %23 = getelementptr inbounds i8, ptr %0, i64 104
  %24 = load ptr, ptr %23, align 8, !tbaa !25
  %25 = getelementptr inbounds i8, ptr %19, i64 24
  %26 = load ptr, ptr %25, align 8, !tbaa !26
  %27 = tail call ptr @sqlite3BinaryCompareCollSeq(ptr noundef %24, ptr noundef nonnull %21, ptr noundef %26)
  %.not17 = icmp eq ptr %27, null
  br i1 %.not17, label %.thread, label %28

28:                                               ; preds = %22
  %29 = load ptr, ptr %27, align 8, !tbaa !27
  br label %.thread

.thread:                                          ; preds = %8, %28, %22, %5, %2
  %.0 = phi ptr [ null, %5 ], [ null, %2 ], [ %29, %28 ], [ @sqlite3StrBINARY, %22 ], [ @sqlite3StrBINARY, %8 ]
  ret ptr %.0
}

; Function Attrs: nounwind uwtable
declare hidden ptr @sqlite3BinaryCompareCollSeq(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !7, i64 0}
!6 = !{!"sqlite3_index_info", !7, i64 0, !10, i64 8, !7, i64 16, !10, i64 24, !10, i64 32, !7, i64 40, !10, i64 48, !7, i64 56, !7, i64 60, !11, i64 64, !12, i64 72, !7, i64 80, !12, i64 88}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"double", !8, i64 0}
!12 = !{!"long long", !8, i64 0}
!13 = !{!6, !10, i64 8}
!14 = !{!15, !7, i64 8}
!15 = !{!"sqlite3_index_constraint", !7, i64 0, !8, i64 4, !8, i64 5, !7, i64 8}
!16 = !{!17, !10, i64 0}
!17 = !{!"HiddenIndexInfo", !10, i64 0, !10, i64 8}
!18 = !{!19, !10, i64 32}
!19 = !{!"WhereClause", !10, i64 0, !10, i64 8, !8, i64 16, !8, i64 17, !7, i64 20, !7, i64 24, !10, i64 32, !8, i64 40}
!20 = !{!21, !10, i64 0}
!21 = !{!"WhereTerm", !10, i64 0, !10, i64 8, !22, i64 16, !22, i64 18, !22, i64 20, !8, i64 22, !8, i64 23, !7, i64 24, !7, i64 28, !7, i64 32, !8, i64 40, !12, i64 48, !12, i64 56}
!22 = !{!"short", !8, i64 0}
!23 = !{!24, !10, i64 16}
!24 = !{!"Expr", !8, i64 0, !8, i64 1, !7, i64 4, !8, i64 8, !10, i64 16, !10, i64 24, !8, i64 32, !7, i64 40, !7, i64 44, !22, i64 48, !22, i64 50, !22, i64 52, !8, i64 54, !10, i64 56, !8, i64 64}
!25 = !{!17, !10, i64 8}
!26 = !{!24, !10, i64 24}
!27 = !{!28, !10, i64 0}
!28 = !{!"CollSeq", !10, i64 0, !8, i64 8, !10, i64 16, !10, i64 24, !10, i64 32}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass