; LLVM IR from kala13x/libxutils :: src/data (buf)

; 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.XDataBuffer = type { ptr, ptr, i64, i64, i32, 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 i32 @XDataBuffer_Realloc(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local ptr @XDataBuffer_Get(ptr noundef, i32 noundef) #1

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

14:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  store i32 1, ptr %7, align 4
  br label %60

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  store i64 %17, ptr %8, align 8, !tbaa !11
  br label %18

18:                                               ; preds = %45, %15
  %19 = load i64, ptr %8, align 8, !tbaa !11
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.XDataBuffer, ptr %20, i32 0, i32 3
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %23 = icmp ult i64 %19, %22
  br i1 %23, label %24, label %48

24:                                               ; preds = %18
  %25 = load i64, ptr %8, align 8, !tbaa !11
  %26 = add i64 %25, 1
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.XDataBuffer, ptr %27, i32 0, i32 3
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = icmp uge i64 %26, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %24
  br label %48

32:                                               ; preds = %24
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.XDataBuffer, ptr %33, i32 0, i32 1
  %35 = load ptr, ptr %34, align 8, !tbaa !15
  %36 = load i64, ptr %8, align 8, !tbaa !11
  %37 = add i64 %36, 1
  %38 = getelementptr inbounds ptr, ptr %35, i64 %37
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.XDataBuffer, ptr %40, i32 0, i32 1
  %42 = load ptr, ptr %41, align 8, !tbaa !15
  %43 = load i64, ptr %8, align 8, !tbaa !11
  %44 = getelementptr inbounds ptr, ptr %42, i64 %43
  store ptr %39, ptr %44, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %32
  %46 = load i64, ptr %8, align 8, !tbaa !11
  %47 = add i64 %46, 1
  store i64 %47, ptr %8, align 8, !tbaa !11
  br label %18, !llvm.loop !16

48:                                               ; preds = %31, %18
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.XDataBuffer, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = load ptr, ptr %4, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.XDataBuffer, ptr %52, i32 0, i32 3
  %54 = load i64, ptr %53, align 8, !tbaa !13
  %55 = add i64 %54, -1
  store i64 %55, ptr %53, align 8, !tbaa !13
  %56 = getelementptr inbounds ptr, ptr %51, i64 %55
  store ptr null, ptr %56, align 8, !tbaa !5
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = call i32 @XDataBuffer_Realloc(ptr noundef %57)
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %59, ptr %3, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %60

60:                                               ; preds = %48, %14
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  %61 = load ptr, ptr %3, align 8
  ret ptr %61
}

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


; 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: nounwind uwtable
declare dso_local i32 @XDataBuffer_Realloc(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local ptr @XDataBuffer_Pop(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call ptr @XDataBuffer_Get(ptr noundef %0, i32 noundef %1)
  %4 = icmp eq ptr %3, null
  br i1 %4, label %46, label %5

5:                                                ; preds = %2
  %6 = zext i32 %1 to i64
  %7 = getelementptr inbounds i8, ptr %0, i64 24
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = icmp ugt i64 %8, %6
  br i1 %9, label %.lr.ph, label %.._crit_edge_crit_edge

.._crit_edge_crit_edge:                           ; preds = %5
  %.pre = add nsw i64 %8, -1
  br label %._crit_edge

.lr.ph:                                           ; preds = %5
  %10 = getelementptr inbounds i8, ptr %0, i64 8
  %11 = add i64 %8, -1
  %exitcond.not25 = icmp eq i64 %11, %6
  br i1 %exitcond.not25, label %._crit_edge, label %.lr.ph27.preheader

.lr.ph27.preheader:                               ; preds = %.lr.ph
  %12 = xor i64 %6, -1
  %13 = add i64 %8, %12
  %14 = add i64 %8, -2
  %15 = sub i64 %14, %6
  %xtraiter = and i64 %13, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph27.prol.loopexit, label %.lr.ph27.prol

.lr.ph27.prol:                                    ; preds = %.lr.ph27.preheader, %.lr.ph27.prol
  %.02226.prol = phi i64 [ %16, %.lr.ph27.prol ], [ %6, %.lr.ph27.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %.lr.ph27.prol ], [ 0, %.lr.ph27.preheader ]
  %16 = add nuw i64 %.02226.prol, 1
  %17 = load ptr, ptr %10, align 8, !tbaa !12
  %18 = getelementptr inbounds ptr, ptr %17, i64 %16
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = getelementptr inbounds ptr, ptr %17, i64 %.02226.prol
  store ptr %19, ptr %20, align 8, !tbaa !13
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph27.prol.loopexit, label %.lr.ph27.prol, !llvm.loop !14

.lr.ph27.prol.loopexit:                           ; preds = %.lr.ph27.prol, %.lr.ph27.preheader
  %.02226.unr = phi i64 [ %6, %.lr.ph27.preheader ], [ %16, %.lr.ph27.prol ]
  %21 = icmp ult i64 %15, 3
  br i1 %21, label %._crit_edge, label %.lr.ph27

.lr.ph27:                                         ; preds = %.lr.ph27.prol.loopexit, %.lr.ph27
  %.02226 = phi i64 [ %37, %.lr.ph27 ], [ %.02226.unr, %.lr.ph27.prol.loopexit ]
  %22 = add nuw i64 %.02226, 1
  %23 = load ptr, ptr %10, align 8, !tbaa !12
  %24 = getelementptr inbounds ptr, ptr %23, i64 %22
  %25 = load ptr, ptr %24, align 8, !tbaa !13
  %26 = getelementptr inbounds ptr, ptr %23, i64 %.02226
  store ptr %25, ptr %26, align 8, !tbaa !13
  %27 = add nuw i64 %.02226, 2
  %28 = load ptr, ptr %10, align 8, !tbaa !12
  %29 = getelementptr inbounds ptr, ptr %28, i64 %27
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = getelementptr inbounds ptr, ptr %28, i64 %22
  store ptr %30, ptr %31, align 8, !tbaa !13
  %32 = add nuw i64 %.02226, 3
  %33 = load ptr, ptr %10, align 8, !tbaa !12
  %34 = getelementptr inbounds ptr, ptr %33, i64 %32
  %35 = load ptr, ptr %34, align 8, !tbaa !13
  %36 = getelementptr inbounds ptr, ptr %33, i64 %27
  store ptr %35, ptr %36, align 8, !tbaa !13
  %37 = add nuw i64 %.02226, 4
  %38 = load ptr, ptr %10, align 8, !tbaa !12
  %39 = getelementptr inbounds ptr, ptr %38, i64 %37
  %40 = load ptr, ptr %39, align 8, !tbaa !13
  %41 = getelementptr inbounds ptr, ptr %38, i64 %32
  store ptr %40, ptr %41, align 8, !tbaa !13
  %exitcond.not.3 = icmp eq i64 %37, %11
  br i1 %exitcond.not.3, label %._crit_edge, label %.lr.ph27, !llvm.loop !16

._crit_edge:                                      ; preds = %.lr.ph27.prol.loopexit, %.lr.ph27, %.lr.ph, %.._crit_edge_crit_edge
  %.pre-phi = phi i64 [ %.pre, %.._crit_edge_crit_edge ], [ %11, %.lr.ph ], [ %11, %.lr.ph27 ], [ %11, %.lr.ph27.prol.loopexit ]
  %42 = getelementptr inbounds i8, ptr %0, i64 8
  %43 = load ptr, ptr %42, align 8, !tbaa !12
  store i64 %.pre-phi, ptr %7, align 8, !tbaa !5
  %44 = getelementptr inbounds ptr, ptr %43, i64 %.pre-phi
  store ptr null, ptr %44, align 8, !tbaa !13
  %45 = tail call i32 @XDataBuffer_Realloc(ptr noundef nonnull %0)
  br label %46

46:                                               ; preds = %2, %._crit_edge
  ret ptr %3
}

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, !10, i64 24}
!6 = !{!"XDataBuffer", !7, i64 0, !7, i64 8, !10, i64 16, !10, i64 24, !11, i64 32, !11, i64 36}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !7, i64 8}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.unroll.disable"}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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