; LLVM IR from ibr-ds/STANlite :: Enclave/sqlite (vdbesort)

; 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.VdbeCursor = type { i8, i8, i8, i8, i8, i8, ptr, i64, ptr, i32, i32, ptr, %union.anon.0, ptr, i32, i32, i16, i16, i64, ptr, ptr, i32, i32, [1 x i32] }
%union.anon.0 = type { ptr }
%struct.VdbeSorter = type { i32, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, %struct.SorterList, i32, i32, i8, i8, i8, i8, i8, [1 x %struct.SortSubtask] }
%struct.SorterList = type { ptr, ptr, i32 }
%struct.SortSubtask = type { ptr, i32, ptr, ptr, %struct.SorterList, i32, ptr, %struct.SorterFile, %struct.SorterFile }
%struct.SorterFile = type { ptr, i64 }
%struct.UnpackedRecord = type { ptr, ptr, i16, i8, i8, i8, i8, i8 }
%struct.Mem = type { %union.MemValue, i16, i8, i8, i32, ptr, ptr, i32, i32, ptr, ptr }
%union.MemValue = type { double }

; 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
declare hidden ptr @vdbeSorterRowkey(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @sqlite3VdbeSorterCompare(ptr noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #1 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store ptr %3, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.VdbeCursor, ptr %17, i32 0, i32 12
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  store ptr %19, ptr %10, align 8, !tbaa !5
  %20 = load ptr, ptr %10, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.VdbeSorter, ptr %20, i32 0, i32 8
  %22 = load ptr, ptr %21, align 8, !tbaa !12
  store ptr %22, ptr %11, align 8, !tbaa !5
  %23 = load ptr, ptr %6, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.VdbeCursor, ptr %23, i32 0, i32 13
  %25 = load ptr, ptr %24, align 8, !tbaa !15
  store ptr %25, ptr %12, align 8, !tbaa !5
  %26 = load ptr, ptr %11, align 8, !tbaa !5
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %41

28:                                               ; preds = %4
  %29 = load ptr, ptr %12, align 8, !tbaa !5
  %30 = call ptr @sqlite3VdbeAllocUnpackedRecord(ptr noundef %29)
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.VdbeSorter, ptr %31, i32 0, i32 8
  store ptr %30, ptr %32, align 8, !tbaa !12
  store ptr %30, ptr %11, align 8, !tbaa !5
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  %34 = icmp eq ptr %33, null
  br i1 %34, label %35, label %36

35:                                               ; preds = %28
  store i32 7, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %80

36:                                               ; preds = %28
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = trunc i32 %37 to i16
  %39 = load ptr, ptr %11, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.UnpackedRecord, ptr %39, i32 0, i32 2
  store i16 %38, ptr %40, align 8, !tbaa !19
  br label %41

41:                                               ; preds = %36, %4
  %42 = load ptr, ptr %10, align 8, !tbaa !5
  %43 = call ptr @vdbeSorterRowkey(ptr noundef %42, ptr noundef %15)
  store ptr %43, ptr %14, align 8, !tbaa !5
  %44 = load ptr, ptr %12, align 8, !tbaa !5
  %45 = load i32, ptr %15, align 4, !tbaa !9
  %46 = load ptr, ptr %14, align 8, !tbaa !5
  %47 = load ptr, ptr %11, align 8, !tbaa !5
  call void @sqlite3VdbeRecordUnpack(ptr noundef %44, i32 noundef %45, ptr noundef %46, ptr noundef %47)
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %67, %41
  %49 = load i32, ptr %13, align 4, !tbaa !9
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = icmp slt i32 %49, %50
  br i1 %51, label %52, label %70

52:                                               ; preds = %48
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.UnpackedRecord, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !21
  %56 = load i32, ptr %13, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds %struct.Mem, ptr %55, i64 %57
  %59 = getelementptr inbounds %struct.Mem, ptr %58, i32 0, i32 1
  %60 = load i16, ptr %59, align 8, !tbaa !22
  %61 = zext i16 %60 to i32
  %62 = and i32 %61, 1
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %66

64:                                               ; preds = %52
  %65 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 -1, ptr %65, align 4, !tbaa !9
  store i32 0, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %80

66:                                               ; preds = %52
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %13, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %13, align 4, !tbaa !9
  br label %48, !llvm.loop !24

70:                                               ; preds = %48
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.Mem, ptr %71, i32 0, i32 4
  %73 = load i32, ptr %72, align 4, !tbaa !26
  %74 = load ptr, ptr %7, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.Mem, ptr %74, i32 0, i32 5
  %76 = load ptr, ptr %75, align 8, !tbaa !27
  %77 = load ptr, ptr %11, align 8, !tbaa !5
  %78 = call i32 @sqlite3VdbeRecordCompare(i32 noundef %73, ptr noundef %76, ptr noundef %77)
  %79 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %78, ptr %79, align 4, !tbaa !9
  store i32 0, ptr %5, align 4
  store i32 1, ptr %16, align 4
  br label %80

80:                                               ; preds = %70, %64, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  %81 = load i32, ptr %5, align 4
  ret i32 %81
}

declare ptr @sqlite3VdbeAllocUnpackedRecord(ptr noundef) #2

declare void @sqlite3VdbeRecordUnpack(ptr noundef, i32 noundef, ptr noundef, ptr noundef) #2

declare i32 @sqlite3VdbeRecordCompare(i32 noundef, ptr noundef, ptr noundef) #2

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 = { "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 #3 = { 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 = !{!7, !7, i64 0}
!12 = !{!13, !6, i64 48}
!13 = !{!"VdbeSorter", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !14, i64 56, !10, i64 80, !10, i64 84, !7, i64 88, !7, i64 89, !7, i64 90, !7, i64 91, !7, i64 92, !7, i64 96}
!14 = !{!"SorterList", !6, i64 0, !6, i64 8, !10, i64 16}
!15 = !{!16, !6, i64 56}
!16 = !{!"VdbeCursor", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !10, i64 5, !10, i64 5, !10, i64 5, !6, i64 8, !17, i64 16, !6, i64 24, !10, i64 32, !10, i64 36, !6, i64 40, !7, i64 48, !6, i64 56, !10, i64 64, !10, i64 68, !18, i64 72, !18, i64 74, !17, i64 80, !6, i64 88, !6, i64 96, !10, i64 104, !10, i64 108, !7, i64 112}
!17 = !{!"long long", !7, i64 0}
!18 = !{!"short", !7, i64 0}
!19 = !{!20, !18, i64 16}
!20 = !{!"UnpackedRecord", !6, i64 0, !6, i64 8, !18, i64 16, !7, i64 18, !7, i64 19, !7, i64 20, !7, i64 21, !7, i64 22}
!21 = !{!20, !6, i64 8}
!22 = !{!23, !18, i64 8}
!23 = !{!"Mem", !7, i64 0, !18, i64 8, !7, i64 10, !7, i64 11, !10, i64 12, !6, i64 16, !6, i64 24, !10, i64 32, !10, i64 36, !6, i64 40, !6, i64 48}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!23, !10, i64 12}
!27 = !{!23, !6, i64 16}


; 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.Mem = type { %union.MemValue, i16, i8, i8, i32, ptr, ptr, i32, i32, ptr, ptr }
%union.MemValue = type { double }

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

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

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

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 8) i32 @sqlite3VdbeSorterCompare(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #1 {
  %5 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %5) #3
  %6 = getelementptr inbounds i8, ptr %0, i64 48
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %7, i64 48
  %9 = load ptr, ptr %8, align 8, !tbaa !8
  %10 = getelementptr inbounds i8, ptr %0, i64 56
  %11 = load ptr, ptr %10, align 8, !tbaa !13
  %12 = icmp eq ptr %9, null
  br i1 %12, label %13, label %19

13:                                               ; preds = %4
  %14 = tail call ptr @sqlite3VdbeAllocUnpackedRecord(ptr noundef %11) #3
  store ptr %14, ptr %8, align 8, !tbaa !8
  %15 = icmp eq ptr %14, null
  br i1 %15, label %35, label %16

16:                                               ; preds = %13
  %17 = trunc i32 %2 to i16
  %18 = getelementptr inbounds i8, ptr %14, i64 16
  store i16 %17, ptr %18, align 8, !tbaa !17
  br label %19

19:                                               ; preds = %16, %4
  %.024 = phi ptr [ %14, %16 ], [ %9, %4 ]
  %20 = call ptr @vdbeSorterRowkey(ptr noundef nonnull %7, ptr noundef nonnull %5)
  %21 = load i32, ptr %5, align 4, !tbaa !19
  call void @sqlite3VdbeRecordUnpack(ptr noundef %11, i32 noundef %21, ptr noundef %20, ptr noundef nonnull %.024) #3
  %22 = icmp sgt i32 %2, 0
  br i1 %22, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %19
  %23 = getelementptr inbounds i8, ptr %.024, i64 8
  %24 = load ptr, ptr %23, align 8, !tbaa !20
  %wide.trip.count = zext nneg i32 %2 to i64
  br label %26

25:                                               ; preds = %26
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %26, !llvm.loop !21

26:                                               ; preds = %.lr.ph, %25
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %25 ]
  %27 = getelementptr inbounds %struct.Mem, ptr %24, i64 %indvars.iv, i32 1
  %28 = load i16, ptr %27, align 8, !tbaa !23
  %29 = and i16 %28, 1
  %.not = icmp eq i16 %29, 0
  br i1 %.not, label %25, label %.sink.split

._crit_edge:                                      ; preds = %25, %19
  %30 = getelementptr inbounds i8, ptr %1, i64 12
  %31 = load i32, ptr %30, align 4, !tbaa !25
  %32 = getelementptr inbounds i8, ptr %1, i64 16
  %33 = load ptr, ptr %32, align 8, !tbaa !26
  %34 = call i32 @sqlite3VdbeRecordCompare(i32 noundef %31, ptr noundef %33, ptr noundef nonnull %.024) #3
  br label %.sink.split

.sink.split:                                      ; preds = %26, %._crit_edge
  %.sink = phi i32 [ %34, %._crit_edge ], [ -1, %26 ]
  store i32 %.sink, ptr %3, align 4, !tbaa !19
  br label %35

35:                                               ; preds = %.sink.split, %13
  %.0 = phi i32 [ 7, %13 ], [ 0, %.sink.split ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %5) #3
  ret i32 %.0
}

declare ptr @sqlite3VdbeAllocUnpackedRecord(ptr noundef) local_unnamed_addr #2

declare void @sqlite3VdbeRecordUnpack(ptr noundef, i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

declare i32 @sqlite3VdbeRecordCompare(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { "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 #3 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !11, i64 48}
!9 = !{!"VdbeSorter", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !12, i64 56, !10, i64 80, !10, i64 84, !6, i64 88, !6, i64 89, !6, i64 90, !6, i64 91, !6, i64 92, !6, i64 96}
!10 = !{!"int", !6, i64 0}
!11 = !{!"any pointer", !6, i64 0}
!12 = !{!"SorterList", !11, i64 0, !11, i64 8, !10, i64 16}
!13 = !{!14, !11, i64 56}
!14 = !{!"VdbeCursor", !6, i64 0, !6, i64 1, !6, i64 2, !6, i64 3, !6, i64 4, !10, i64 5, !10, i64 5, !10, i64 5, !11, i64 8, !15, i64 16, !11, i64 24, !10, i64 32, !10, i64 36, !11, i64 40, !6, i64 48, !11, i64 56, !10, i64 64, !10, i64 68, !16, i64 72, !16, i64 74, !15, i64 80, !11, i64 88, !11, i64 96, !10, i64 104, !10, i64 108, !6, i64 112}
!15 = !{!"long long", !6, i64 0}
!16 = !{!"short", !6, i64 0}
!17 = !{!18, !16, i64 16}
!18 = !{!"UnpackedRecord", !11, i64 0, !11, i64 8, !16, i64 16, !6, i64 18, !6, i64 19, !6, i64 20, !6, i64 21, !6, i64 22}
!19 = !{!10, !10, i64 0}
!20 = !{!18, !11, i64 8}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}
!23 = !{!24, !16, i64 8}
!24 = !{!"Mem", !6, i64 0, !16, i64 8, !6, i64 10, !6, i64 11, !10, i64 12, !11, i64 16, !11, i64 24, !10, i64 32, !10, i64 36, !11, i64 40, !11, i64 48}
!25 = !{!24, !10, i64 12}
!26 = !{!24, !11, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass