; LLVM IR from AbdulrahmanXSO25/mkdb_cclient :: mkdb (mkdb_result_free)

; 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.mkdb_result_t = type { i32, %union.anon }
%union.anon = type { %struct.anon.0 }
%struct.anon.0 = type { i16, ptr, i32, ptr }
%struct.mkdb_column_t = type { ptr, i32, i32 }
%struct.mkdb_value_t = type { i32, %union.anon.1 }
%union.anon.1 = type { i64 }

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

; 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 void @mkdb_result_free(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %1
  br label %126

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.mkdb_result_t, ptr %11, i32 0, i32 0
  %13 = load i32, ptr %12, align 8, !tbaa !9
  switch i32 %13, label %123 [
    i32 1, label %14
    i32 2, label %119
  ]

14:                                               ; preds = %10
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.mkdb_result_t, ptr %15, i32 0, i32 1
  %17 = getelementptr inbounds %struct.anon.0, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !12
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %47

20:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store i64 0, ptr %3, align 8, !tbaa !13
  br label %21

21:                                               ; preds = %39, %20
  %22 = load i64, ptr %3, align 8, !tbaa !13
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.mkdb_result_t, ptr %23, i32 0, i32 1
  %25 = getelementptr inbounds %struct.anon.0, ptr %24, i32 0, i32 0
  %26 = load i16, ptr %25, align 8, !tbaa !12
  %27 = zext i16 %26 to i64
  %28 = icmp ult i64 %22, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %42

30:                                               ; preds = %21
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.mkdb_result_t, ptr %31, i32 0, i32 1
  %33 = getelementptr inbounds %struct.anon.0, ptr %32, i32 0, i32 1
  %34 = load ptr, ptr %33, align 8, !tbaa !12
  %35 = load i64, ptr %3, align 8, !tbaa !13
  %36 = getelementptr inbounds %struct.mkdb_column_t, ptr %34, i64 %35
  %37 = getelementptr inbounds %struct.mkdb_column_t, ptr %36, i32 0, i32 0
  %38 = load ptr, ptr %37, align 8, !tbaa !15
  call void @free(ptr noundef %38) #3
  br label %39

39:                                               ; preds = %30
  %40 = load i64, ptr %3, align 8, !tbaa !13
  %41 = add i64 %40, 1
  store i64 %41, ptr %3, align 8, !tbaa !13
  br label %21, !llvm.loop !17

42:                                               ; preds = %29
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.mkdb_result_t, ptr %43, i32 0, i32 1
  %45 = getelementptr inbounds %struct.anon.0, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !12
  call void @free(ptr noundef %46) #3
  br label %47

47:                                               ; preds = %42, %14
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.mkdb_result_t, ptr %48, i32 0, i32 1
  %50 = getelementptr inbounds %struct.anon.0, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !12
  %52 = icmp ne ptr %51, null
  br i1 %52, label %53, label %118

53:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store i64 0, ptr %4, align 8, !tbaa !13
  br label %54

54:                                               ; preds = %110, %53
  %55 = load i64, ptr %4, align 8, !tbaa !13
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.mkdb_result_t, ptr %56, i32 0, i32 1
  %58 = getelementptr inbounds %struct.anon.0, ptr %57, i32 0, i32 2
  %59 = load i32, ptr %58, align 8, !tbaa !12
  %60 = zext i32 %59 to i64
  %61 = icmp ult i64 %55, %60
  br i1 %61, label %63, label %62

62:                                               ; preds = %54
  store i32 6, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %113

63:                                               ; preds = %54
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 0, ptr %6, align 8, !tbaa !13
  br label %64

64:                                               ; preds = %99, %63
  %65 = load i64, ptr %6, align 8, !tbaa !13
  %66 = load ptr, ptr %2, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.mkdb_result_t, ptr %66, i32 0, i32 1
  %68 = getelementptr inbounds %struct.anon.0, ptr %67, i32 0, i32 0
  %69 = load i16, ptr %68, align 8, !tbaa !12
  %70 = zext i16 %69 to i64
  %71 = icmp ult i64 %65, %70
  br i1 %71, label %73, label %72

72:                                               ; preds = %64
  store i32 9, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %102

73:                                               ; preds = %64
  %74 = load ptr, ptr %2, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.mkdb_result_t, ptr %74, i32 0, i32 1
  %76 = getelementptr inbounds %struct.anon.0, ptr %75, i32 0, i32 3
  %77 = load ptr, ptr %76, align 8, !tbaa !12
  %78 = load i64, ptr %4, align 8, !tbaa !13
  %79 = getelementptr inbounds ptr, ptr %77, i64 %78
  %80 = load ptr, ptr %79, align 8, !tbaa !5
  %81 = load i64, ptr %6, align 8, !tbaa !13
  %82 = getelementptr inbounds %struct.mkdb_value_t, ptr %80, i64 %81
  %83 = getelementptr inbounds %struct.mkdb_value_t, ptr %82, i32 0, i32 0
  %84 = load i32, ptr %83, align 8, !tbaa !9
  %85 = icmp eq i32 %84, 5
  br i1 %85, label %86, label %98

86:                                               ; preds = %73
  %87 = load ptr, ptr %2, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.mkdb_result_t, ptr %87, i32 0, i32 1
  %89 = getelementptr inbounds %struct.anon.0, ptr %88, i32 0, i32 3
  %90 = load ptr, ptr %89, align 8, !tbaa !12
  %91 = load i64, ptr %4, align 8, !tbaa !13
  %92 = getelementptr inbounds ptr, ptr %90, i64 %91
  %93 = load ptr, ptr %92, align 8, !tbaa !5
  %94 = load i64, ptr %6, align 8, !tbaa !13
  %95 = getelementptr inbounds %struct.mkdb_value_t, ptr %93, i64 %94
  %96 = getelementptr inbounds %struct.mkdb_value_t, ptr %95, i32 0, i32 1
  %97 = load ptr, ptr %96, align 8, !tbaa !12
  call void @free(ptr noundef %97) #3
  br label %98

98:                                               ; preds = %86, %73
  br label %99

99:                                               ; preds = %98
  %100 = load i64, ptr %6, align 8, !tbaa !13
  %101 = add i64 %100, 1
  store i64 %101, ptr %6, align 8, !tbaa !13
  br label %64, !llvm.loop !19

102:                                              ; preds = %72
  %103 = load ptr, ptr %2, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.mkdb_result_t, ptr %103, i32 0, i32 1
  %105 = getelementptr inbounds %struct.anon.0, ptr %104, i32 0, i32 3
  %106 = load ptr, ptr %105, align 8, !tbaa !12
  %107 = load i64, ptr %4, align 8, !tbaa !13
  %108 = getelementptr inbounds ptr, ptr %106, i64 %107
  %109 = load ptr, ptr %108, align 8, !tbaa !5
  call void @free(ptr noundef %109) #3
  br label %110

110:                                              ; preds = %102
  %111 = load i64, ptr %4, align 8, !tbaa !13
  %112 = add i64 %111, 1
  store i64 %112, ptr %4, align 8, !tbaa !13
  br label %54, !llvm.loop !20

113:                                              ; preds = %62
  %114 = load ptr, ptr %2, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.mkdb_result_t, ptr %114, i32 0, i32 1
  %116 = getelementptr inbounds %struct.anon.0, ptr %115, i32 0, i32 3
  %117 = load ptr, ptr %116, align 8, !tbaa !12
  call void @free(ptr noundef %117) #3
  br label %118

118:                                              ; preds = %113, %47
  br label %124

119:                                              ; preds = %10
  %120 = load ptr, ptr %2, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct.mkdb_result_t, ptr %120, i32 0, i32 1
  %122 = load ptr, ptr %121, align 8, !tbaa !12
  call void @free(ptr noundef %122) #3
  br label %124

123:                                              ; preds = %10
  br label %124

124:                                              ; preds = %123, %119, %118
  %125 = load ptr, ptr %2, align 8, !tbaa !5
  call void @free(ptr noundef %125) #3
  br label %126

126:                                              ; preds = %124, %9
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 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 #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 0}
!10 = !{!"", !11, i64 0, !7, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !11, i64 8, !11, i64 12}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}
!20 = distinct !{!20, !18}


; 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.mkdb_column_t = type { ptr, i32, i32 }
%struct.mkdb_value_t = type { i32, %union.anon.1 }
%union.anon.1 = type { i64 }

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #0

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

2:                                                ; preds = %1
  %3 = load i32, ptr %0, align 8, !tbaa !5
  switch i32 %3, label %47 [
    i32 1, label %4
    i32 2, label %45
  ]

4:                                                ; preds = %2
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = getelementptr inbounds i8, ptr %0, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !10
  %.not29 = icmp eq ptr %7, null
  br i1 %.not29, label %17, label %.preheader32

.preheader32:                                     ; preds = %4
  %8 = load i16, ptr %5, align 8, !tbaa !10
  %.not39 = icmp eq i16 %8, 0
  br i1 %.not39, label %._crit_edge, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %6, align 8, !tbaa !10
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader32
  %9 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %7, %.preheader32 ]
  tail call void @free(ptr noundef %9) #2
  br label %17

.lr.ph:                                           ; preds = %.preheader32, %.lr.ph
  %.02733 = phi i64 [ %13, %.lr.ph ], [ 0, %.preheader32 ]
  %10 = load ptr, ptr %6, align 8, !tbaa !10
  %11 = getelementptr inbounds %struct.mkdb_column_t, ptr %10, i64 %.02733
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  tail call void @free(ptr noundef %12) #2
  %13 = add nuw nsw i64 %.02733, 1
  %14 = load i16, ptr %5, align 8, !tbaa !10
  %15 = zext i16 %14 to i64
  %16 = icmp ult i64 %13, %15
  br i1 %16, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !14

17:                                               ; preds = %._crit_edge, %4
  %18 = getelementptr inbounds i8, ptr %0, i64 32
  %19 = load ptr, ptr %18, align 8, !tbaa !10
  %.not30 = icmp eq ptr %19, null
  br i1 %.not30, label %47, label %.preheader31

.preheader31:                                     ; preds = %17
  %20 = getelementptr inbounds i8, ptr %0, i64 24
  %21 = load i32, ptr %20, align 8, !tbaa !10
  %.not40 = icmp eq i32 %21, 0
  br i1 %.not40, label %.sink.split, label %.preheader

.preheader:                                       ; preds = %.preheader31, %._crit_edge36
  %.02637 = phi i64 [ %26, %._crit_edge36 ], [ 0, %.preheader31 ]
  %22 = load i16, ptr %5, align 8, !tbaa !10
  %.not41 = icmp eq i16 %22, 0
  br i1 %.not41, label %._crit_edge36, label %.lr.ph35

._crit_edge36:                                    ; preds = %40, %.preheader
  %23 = load ptr, ptr %18, align 8, !tbaa !10
  %24 = getelementptr inbounds ptr, ptr %23, i64 %.02637
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  tail call void @free(ptr noundef %25) #2
  %26 = add nuw nsw i64 %.02637, 1
  %27 = load i32, ptr %20, align 8, !tbaa !10
  %28 = zext i32 %27 to i64
  %29 = icmp ult i64 %26, %28
  br i1 %29, label %.preheader, label %.sink.split.sink.split, !llvm.loop !17

.lr.ph35:                                         ; preds = %.preheader, %40
  %30 = phi i16 [ %41, %40 ], [ %22, %.preheader ]
  %.034 = phi i64 [ %42, %40 ], [ 0, %.preheader ]
  %31 = load ptr, ptr %18, align 8, !tbaa !10
  %32 = getelementptr inbounds ptr, ptr %31, i64 %.02637
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = getelementptr inbounds %struct.mkdb_value_t, ptr %33, i64 %.034
  %35 = load i32, ptr %34, align 8, !tbaa !5
  %36 = icmp eq i32 %35, 5
  br i1 %36, label %37, label %40

37:                                               ; preds = %.lr.ph35
  %38 = getelementptr inbounds i8, ptr %34, i64 8
  %39 = load ptr, ptr %38, align 8, !tbaa !10
  tail call void @free(ptr noundef %39) #2
  %.pre42 = load i16, ptr %5, align 8, !tbaa !10
  br label %40

40:                                               ; preds = %.lr.ph35, %37
  %41 = phi i16 [ %30, %.lr.ph35 ], [ %.pre42, %37 ]
  %42 = add nuw nsw i64 %.034, 1
  %43 = zext i16 %41 to i64
  %44 = icmp ult i64 %42, %43
  br i1 %44, label %.lr.ph35, label %._crit_edge36, !llvm.loop !18

45:                                               ; preds = %2
  %46 = getelementptr inbounds i8, ptr %0, i64 8
  br label %.sink.split.sink.split

.sink.split.sink.split:                           ; preds = %._crit_edge36, %45
  %.sink44 = phi ptr [ %46, %45 ], [ %18, %._crit_edge36 ]
  %.pre43 = load ptr, ptr %.sink44, align 8, !tbaa !10
  br label %.sink.split

.sink.split:                                      ; preds = %.sink.split.sink.split, %.preheader31
  %.sink = phi ptr [ %19, %.preheader31 ], [ %.pre43, %.sink.split.sink.split ]
  tail call void @free(ptr noundef %.sink) #2
  br label %47

47:                                               ; preds = %.sink.split, %2, %17
  tail call void @free(ptr noundef nonnull %0) #2
  br label %48

48:                                               ; preds = %1, %47
  ret void
}

attributes #0 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { 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, !7, i64 0}
!6 = !{!"", !7, i64 0, !8, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !7, i64 8, !7, i64 12}
!13 = !{!"any pointer", !8, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!13, !13, i64 0}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !15}


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