; LLVM IR from chkn/logdb :: src (logdb_buffer)

; 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.logdb_buffer_t = type { ptr, i32, ptr, ptr, 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: nounwind allocsize(0)
declare noalias ptr @malloc(i64 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: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @logdb_buffer_length(ptr noundef) #4

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

17:                                               ; preds = %1
  %18 = load ptr, ptr %4, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.logdb_buffer_t, ptr %18, i32 0, i32 3
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = call ptr @logdb_buffer_data(ptr noundef %20)
  store ptr %21, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %122

22:                                               ; preds = %1
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.logdb_buffer_t, ptr %23, i32 0, i32 4
  %25 = load ptr, ptr %24, align 8, !tbaa !12
  %26 = icmp ne ptr %25, null
  br i1 %26, label %27, label %118

27:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = call i32 @logdb_buffer_length(ptr noundef %28)
  store i32 %29, ptr %6, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %30 = load i32, ptr %6, align 4, !tbaa !13
  %31 = zext i32 %30 to i64
  %32 = call noalias ptr @malloc(i64 noundef %31) #6
  store ptr %32, ptr %7, align 8, !tbaa !5
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = icmp ne ptr %33, null
  br i1 %34, label %36, label %35

35:                                               ; preds = %27
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %115

36:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %37, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %38, ptr %9, align 8, !tbaa !5
  br label %39

39:                                               ; preds = %88, %36
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.logdb_buffer_t, ptr %40, i32 0, i32 3
  %42 = load ptr, ptr %41, align 8, !tbaa !9
  %43 = icmp ne ptr %42, null
  br i1 %43, label %44, label %69

44:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.logdb_buffer_t, ptr %45, i32 0, i32 3
  %47 = load ptr, ptr %46, align 8, !tbaa !9
  %48 = call i32 @logdb_buffer_length(ptr noundef %47)
  store i32 %48, ptr %10, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %49 = load ptr, ptr %9, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.logdb_buffer_t, ptr %49, i32 0, i32 3
  %51 = load ptr, ptr %50, align 8, !tbaa !9
  %52 = call ptr @logdb_buffer_data(ptr noundef %51)
  store ptr %52, ptr %11, align 8, !tbaa !5
  %53 = load ptr, ptr %11, align 8, !tbaa !5
  %54 = icmp ne ptr %53, null
  br i1 %54, label %57, label %55

55:                                               ; preds = %44
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  call void @free(ptr noundef %56) #5
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %66

57:                                               ; preds = %44
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load ptr, ptr %11, align 8, !tbaa !5
  %60 = load i32, ptr %10, align 4, !tbaa !13
  %61 = zext i32 %60 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %58, ptr align 1 %59, i64 %61, i1 false)
  %62 = load i32, ptr %10, align 4, !tbaa !13
  %63 = load ptr, ptr %8, align 8, !tbaa !5
  %64 = zext i32 %62 to i64
  %65 = getelementptr inbounds i8, ptr %63, i64 %64
  store ptr %65, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %5, align 4
  br label %66

66:                                               ; preds = %57, %55
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %67 = load i32, ptr %5, align 4
  switch i32 %67, label %114 [
    i32 0, label %68
  ]

68:                                               ; preds = %66
  br label %84

69:                                               ; preds = %39
  %70 = load ptr, ptr %8, align 8, !tbaa !5
  %71 = load ptr, ptr %9, align 8, !tbaa !5
  %72 = getelementptr inbounds %struct.logdb_buffer_t, ptr %71, i32 0, i32 0
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.logdb_buffer_t, ptr %74, i32 0, i32 1
  %76 = load i32, ptr %75, align 8, !tbaa !15
  %77 = zext i32 %76 to i64
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %70, ptr align 1 %73, i64 %77, i1 false)
  %78 = load ptr, ptr %9, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.logdb_buffer_t, ptr %78, i32 0, i32 1
  %80 = load i32, ptr %79, align 8, !tbaa !15
  %81 = load ptr, ptr %8, align 8, !tbaa !5
  %82 = zext i32 %80 to i64
  %83 = getelementptr inbounds i8, ptr %81, i64 %82
  store ptr %83, ptr %8, align 8, !tbaa !5
  br label %84

84:                                               ; preds = %69, %68
  %85 = load ptr, ptr %9, align 8, !tbaa !5
  %86 = getelementptr inbounds %struct.logdb_buffer_t, ptr %85, i32 0, i32 4
  %87 = load ptr, ptr %86, align 8, !tbaa !12
  store ptr %87, ptr %9, align 8, !tbaa !5
  br label %88

88:                                               ; preds = %84
  %89 = load ptr, ptr %9, align 8, !tbaa !5
  %90 = icmp ne ptr %89, null
  br i1 %90, label %39, label %91, !llvm.loop !16

91:                                               ; preds = %88
  %92 = load ptr, ptr %4, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.logdb_buffer_t, ptr %92, i32 0, i32 2
  %94 = load ptr, ptr %93, align 8, !tbaa !18
  %95 = icmp ne ptr %94, null
  br i1 %95, label %96, label %103

96:                                               ; preds = %91
  %97 = load ptr, ptr %4, align 8, !tbaa !5
  %98 = getelementptr inbounds %struct.logdb_buffer_t, ptr %97, i32 0, i32 2
  %99 = load ptr, ptr %98, align 8, !tbaa !18
  %100 = load ptr, ptr %4, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.logdb_buffer_t, ptr %100, i32 0, i32 0
  %102 = load ptr, ptr %101, align 8, !tbaa !14
  call void %99(ptr noundef %102)
  br label %103

103:                                              ; preds = %96, %91
  %104 = load ptr, ptr %7, align 8, !tbaa !5
  %105 = load ptr, ptr %4, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.logdb_buffer_t, ptr %105, i32 0, i32 0
  store ptr %104, ptr %106, align 8, !tbaa !14
  %107 = load i32, ptr %6, align 4, !tbaa !13
  %108 = load ptr, ptr %4, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.logdb_buffer_t, ptr %108, i32 0, i32 1
  store i32 %107, ptr %109, align 8, !tbaa !15
  %110 = load ptr, ptr %4, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.logdb_buffer_t, ptr %110, i32 0, i32 4
  store ptr null, ptr %111, align 8, !tbaa !12
  %112 = load ptr, ptr %4, align 8, !tbaa !5
  %113 = getelementptr inbounds %struct.logdb_buffer_t, ptr %112, i32 0, i32 2
  store ptr @free, ptr %113, align 8, !tbaa !18
  store i32 0, ptr %5, align 4
  br label %114

114:                                              ; preds = %103, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  br label %115

115:                                              ; preds = %114, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  %116 = load i32, ptr %5, align 4
  switch i32 %116, label %122 [
    i32 0, label %117
  ]

117:                                              ; preds = %115
  br label %118

118:                                              ; preds = %117, %22
  %119 = load ptr, ptr %4, align 8, !tbaa !5
  %120 = getelementptr inbounds %struct.logdb_buffer_t, ptr %119, i32 0, i32 0
  %121 = load ptr, ptr %120, align 8, !tbaa !14
  store ptr %121, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %122

122:                                              ; preds = %118, %115, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %123 = load ptr, ptr %2, align 8
  ret ptr %123
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #4 = { 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 #5 = { nounwind }
attributes #6 = { nounwind allocsize(0) }

!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, !6, i64 24}
!10 = !{!"logdb_buffer_t", !6, i64 0, !11, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !7, i64 40}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 32}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !6, i64 0}
!15 = !{!10, !11, i64 8}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !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"

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @logdb_buffer_length(ptr noundef) local_unnamed_addr #3

; Function Attrs: nounwind uwtable
define dso_local ptr @logdb_buffer_data(ptr noundef %0) local_unnamed_addr #3 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse, %1
  %.tr = phi ptr [ %0, %1 ], [ %3, %tailrecurse ]
  %2 = getelementptr inbounds i8, ptr %.tr, i64 24
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %tailrecurse

4:                                                ; preds = %tailrecurse
  %5 = getelementptr inbounds i8, ptr %.tr, i64 32
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %.not52 = icmp eq ptr %6, null
  br i1 %.not52, label %._crit_edge, label %7

._crit_edge:                                      ; preds = %4
  %.pre = load ptr, ptr %.tr, align 8, !tbaa !12
  br label %.critedge

7:                                                ; preds = %4
  %8 = tail call i32 @logdb_buffer_length(ptr noundef nonnull %.tr)
  %9 = zext i32 %8 to i64
  %10 = tail call noalias ptr @malloc(i64 noundef %9) #4
  %.not53 = icmp eq ptr %10, null
  br i1 %.not53, label %.critedge, label %.preheader

.preheader:                                       ; preds = %7, %26
  %.043 = phi ptr [ %.245, %26 ], [ %10, %7 ]
  %.042 = phi ptr [ %28, %26 ], [ %.tr, %7 ]
  %11 = getelementptr inbounds i8, ptr %.042, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %.not54 = icmp eq ptr %12, null
  br i1 %.not54, label %19, label %13

13:                                               ; preds = %.preheader
  %14 = tail call i32 @logdb_buffer_length(ptr noundef nonnull %12)
  %15 = load ptr, ptr %11, align 8, !tbaa !5
  %16 = tail call ptr @logdb_buffer_data(ptr noundef %15)
  %.not55.not = icmp eq ptr %16, null
  br i1 %.not55.not, label %.thread, label %17

.thread:                                          ; preds = %13
  tail call void @free(ptr noundef %10) #5
  br label %.critedge

17:                                               ; preds = %13
  %18 = zext i32 %14 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.043, ptr nonnull align 1 %16, i64 %18, i1 false)
  br label %26

19:                                               ; preds = %.preheader
  %20 = load ptr, ptr %.042, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %.042, i64 8
  %22 = load i32, ptr %21, align 8, !tbaa !13
  %23 = zext i32 %22 to i64
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.043, ptr align 1 %20, i64 %23, i1 false)
  %24 = load i32, ptr %21, align 8, !tbaa !13
  %25 = zext i32 %24 to i64
  br label %26

26:                                               ; preds = %17, %19
  %.pn = phi i64 [ %18, %17 ], [ %25, %19 ]
  %.245 = getelementptr inbounds i8, ptr %.043, i64 %.pn
  %27 = getelementptr inbounds i8, ptr %.042, i64 32
  %28 = load ptr, ptr %27, align 8, !tbaa !11
  %.not56 = icmp eq ptr %28, null
  br i1 %.not56, label %29, label %.preheader, !llvm.loop !14

29:                                               ; preds = %26
  %30 = getelementptr inbounds i8, ptr %.tr, i64 16
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  %.not57 = icmp eq ptr %31, null
  br i1 %.not57, label %34, label %32

32:                                               ; preds = %29
  %33 = load ptr, ptr %.tr, align 8, !tbaa !12
  tail call void %31(ptr noundef %33) #5
  br label %34

34:                                               ; preds = %32, %29
  store ptr %10, ptr %.tr, align 8, !tbaa !12
  %35 = getelementptr inbounds i8, ptr %.tr, i64 8
  store i32 %8, ptr %35, align 8, !tbaa !13
  store ptr null, ptr %5, align 8, !tbaa !11
  store ptr @free, ptr %30, align 8, !tbaa !16
  br label %.critedge

.critedge:                                        ; preds = %34, %._crit_edge, %.thread, %7
  %.5 = phi ptr [ null, %7 ], [ null, %.thread ], [ %.pre, %._crit_edge ], [ %10, %34 ]
  ret ptr %.5
}

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { 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 #4 = { nounwind allocsize(0) }
attributes #5 = { 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 24}
!6 = !{!"logdb_buffer_t", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !8, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 32}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !7, i64 16}


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