; LLVM IR from Yaty/yamldb :: src/code/sql (order)

; 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.SortParameters = type { i32, i32 }
%struct.QueryResult = type { ptr, ptr, i32, i32, i32, ptr, ptr, i32, i32, i32 }
%struct.Orders = type { ptr, i32 }
%struct.Order = type { ptr, i32 }

; 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 dso_local void @mergeSort(ptr noundef, i32 noundef, i32 noundef, i64) #1

; Function Attrs: nounwind uwtable
define dso_local void @makeOrder(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca %struct.SortParameters, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = icmp ne ptr %9, null
  br i1 %10, label %11, label %85

11:                                               ; preds = %2
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.QueryResult, ptr %12, i32 0, i32 3
  %14 = load i32, ptr %13, align 4, !tbaa !9
  %15 = icmp sgt i32 %14, 1
  br i1 %15, label %16, label %85

16:                                               ; preds = %11
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %85

19:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store i32 0, ptr %5, align 4, !tbaa !12
  br label %20

20:                                               ; preds = %81, %19
  %21 = load i32, ptr %5, align 4, !tbaa !12
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Orders, ptr %22, i32 0, i32 1
  %24 = load i32, ptr %23, align 8, !tbaa !13
  %25 = icmp slt i32 %21, %24
  br i1 %25, label %26, label %84

26:                                               ; preds = %20
  store i32 -1, ptr %7, align 4, !tbaa !12
  store i32 0, ptr %6, align 4, !tbaa !12
  br label %27

27:                                               ; preds = %54, %26
  %28 = load i32, ptr %6, align 4, !tbaa !12
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.QueryResult, ptr %29, i32 0, i32 2
  %31 = load i32, ptr %30, align 8, !tbaa !15
  %32 = icmp slt i32 %28, %31
  br i1 %32, label %33, label %57

33:                                               ; preds = %27
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.QueryResult, ptr %34, i32 0, i32 1
  %36 = load ptr, ptr %35, align 8, !tbaa !16
  %37 = load i32, ptr %6, align 4, !tbaa !12
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds ptr, ptr %36, i64 %38
  %40 = load ptr, ptr %39, align 8, !tbaa !5
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.Orders, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !17
  %44 = load i32, ptr %5, align 4, !tbaa !12
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.Order, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.Order, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !18
  %49 = call i32 @areStringsEquals(ptr noundef %40, ptr noundef %48, i32 noundef 1)
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %53

51:                                               ; preds = %33
  %52 = load i32, ptr %6, align 4, !tbaa !12
  store i32 %52, ptr %7, align 4, !tbaa !12
  br label %57

53:                                               ; preds = %33
  br label %54

54:                                               ; preds = %53
  %55 = load i32, ptr %6, align 4, !tbaa !12
  %56 = add nsw i32 %55, 1
  store i32 %56, ptr %6, align 4, !tbaa !12
  br label %27, !llvm.loop !20

57:                                               ; preds = %51, %27
  %58 = load i32, ptr %7, align 4, !tbaa !12
  %59 = icmp eq i32 %58, -1
  br i1 %59, label %60, label %61

60:                                               ; preds = %57
  br label %84

61:                                               ; preds = %57
  %62 = load i32, ptr %7, align 4, !tbaa !12
  %63 = getelementptr inbounds %struct.SortParameters, ptr %8, i32 0, i32 0
  store i32 %62, ptr %63, align 4, !tbaa !22
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.Orders, ptr %64, i32 0, i32 0
  %66 = load ptr, ptr %65, align 8, !tbaa !17
  %67 = load i32, ptr %5, align 4, !tbaa !12
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds %struct.Order, ptr %66, i64 %68
  %70 = getelementptr inbounds %struct.Order, ptr %69, i32 0, i32 1
  %71 = load i32, ptr %70, align 8, !tbaa !24
  %72 = getelementptr inbounds %struct.SortParameters, ptr %8, i32 0, i32 1
  store i32 %71, ptr %72, align 4, !tbaa !25
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.QueryResult, ptr %73, i32 0, i32 0
  %75 = load ptr, ptr %74, align 8, !tbaa !26
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.QueryResult, ptr %76, i32 0, i32 3
  %78 = load i32, ptr %77, align 4, !tbaa !9
  %79 = sub nsw i32 %78, 1
  %80 = load i64, ptr %8, align 4
  call void @mergeSort(ptr noundef %75, i32 noundef 0, i32 noundef %79, i64 %80)
  br label %81

81:                                               ; preds = %61
  %82 = load i32, ptr %5, align 4, !tbaa !12
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %5, align 4, !tbaa !12
  br label %20, !llvm.loop !27

84:                                               ; preds = %60, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %85

85:                                               ; preds = %84, %16, %11, %2
  ret void
}

declare i32 @areStringsEquals(ptr noundef, ptr noundef, i32 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, !11, i64 20}
!10 = !{!"QueryResult", !6, i64 0, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !6, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 8}
!14 = !{!"Orders", !6, i64 0, !11, i64 8}
!15 = !{!10, !11, i64 16}
!16 = !{!10, !6, i64 8}
!17 = !{!14, !6, i64 0}
!18 = !{!19, !6, i64 0}
!19 = !{!"Order", !6, i64 0, !11, i64 8}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!23, !11, i64 0}
!23 = !{!"SortParameters", !11, i64 0, !11, i64 4}
!24 = !{!19, !11, i64 8}
!25 = !{!23, !11, i64 4}
!26 = !{!10, !6, i64 0}
!27 = distinct !{!27, !21}


; 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.Order = type { ptr, i32 }

; Function Attrs: nounwind uwtable
declare dso_local void @mergeSort(ptr noundef, i32 noundef, i32 noundef, i64) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @makeOrder(ptr noundef readonly %0, ptr noundef readonly %1) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %.thread, label %3

3:                                                ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 20
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 1
  %7 = icmp ne ptr %1, null
  %or.cond = and i1 %7, %6
  br i1 %or.cond, label %.preheader26, label %.thread

.preheader26:                                     ; preds = %3
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !11
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.preheader.lr.ph, label %.thread

.preheader.lr.ph:                                 ; preds = %.preheader26
  %11 = getelementptr inbounds i8, ptr %0, i64 16
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %26
  %indvars.iv34 = phi i64 [ 0, %.preheader.lr.ph ], [ %indvars.iv.next35, %26 ]
  %13 = load i32, ptr %11, align 8, !tbaa !13
  %14 = icmp sgt i32 %13, 0
  br i1 %14, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %.preheader, %22
  %indvars.iv = phi i64 [ %indvars.iv.next, %22 ], [ 0, %.preheader ]
  %15 = load ptr, ptr %12, align 8, !tbaa !14
  %16 = getelementptr inbounds ptr, ptr %15, i64 %indvars.iv
  %17 = load ptr, ptr %16, align 8, !tbaa !15
  %18 = load ptr, ptr %1, align 8, !tbaa !16
  %19 = getelementptr inbounds %struct.Order, ptr %18, i64 %indvars.iv34
  %20 = load ptr, ptr %19, align 8, !tbaa !17
  %21 = tail call i32 @areStringsEquals(ptr noundef %17, ptr noundef %20, i32 noundef 1) #2
  %.not24 = icmp eq i32 %21, 0
  br i1 %.not24, label %22, label %26

22:                                               ; preds = %.lr.ph
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = load i32, ptr %11, align 8, !tbaa !13
  %24 = sext i32 %23 to i64
  %25 = icmp slt i64 %indvars.iv.next, %24
  br i1 %25, label %.lr.ph, label %.thread, !llvm.loop !19

26:                                               ; preds = %.lr.ph
  %27 = load ptr, ptr %1, align 8, !tbaa !16
  %28 = getelementptr inbounds %struct.Order, ptr %27, i64 %indvars.iv34, i32 1
  %29 = load i32, ptr %28, align 8, !tbaa !21
  %30 = load ptr, ptr %0, align 8, !tbaa !22
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = add nsw i32 %31, -1
  %.sroa.4.0.insert.ext = zext i32 %29 to i64
  %.sroa.4.0.insert.shift = shl nuw i64 %.sroa.4.0.insert.ext, 32
  %.sroa.0.0.insert.ext = and i64 %indvars.iv, 4294967295
  %.sroa.0.0.insert.insert = or disjoint i64 %.sroa.4.0.insert.shift, %.sroa.0.0.insert.ext
  tail call void @mergeSort(ptr noundef %30, i32 noundef 0, i32 noundef %32, i64 %.sroa.0.0.insert.insert)
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %33 = load i32, ptr %8, align 8, !tbaa !11
  %34 = sext i32 %33 to i64
  %35 = icmp slt i64 %indvars.iv.next35, %34
  br i1 %35, label %.preheader, label %.thread, !llvm.loop !23

.thread:                                          ; preds = %26, %.preheader, %22, %.preheader26, %3, %2
  ret void
}

declare i32 @areStringsEquals(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

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" }
attributes #1 = { "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, !10, i64 20}
!6 = !{!"QueryResult", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 20, !10, i64 24, !7, i64 32, !7, i64 40, !10, i64 48, !10, i64 52, !10, i64 56}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"Orders", !7, i64 0, !10, i64 8}
!13 = !{!6, !10, i64 16}
!14 = !{!6, !7, i64 8}
!15 = !{!7, !7, i64 0}
!16 = !{!12, !7, i64 0}
!17 = !{!18, !7, i64 0}
!18 = !{!"Order", !7, i64 0, !10, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!18, !10, i64 8}
!22 = !{!6, !7, i64 0}
!23 = distinct !{!23, !20}


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