; LLVM IR from seppo0010/rlite :: src (page_list)

; 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.rl_list = type { i64, i64, ptr, i64, i64 }
%struct.rl_list_type = type { ptr, ptr, i32, ptr }
%struct.rl_list_node = type { i64, i64, i64, ptr }

; 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

declare i32 @rl_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @rl_flatten_list(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.rl_list, ptr %13, i32 0, i32 3
  %15 = load i64, ptr %14, align 8, !tbaa !9
  store i64 %15, ptr %8, align 8, !tbaa !12
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store i64 0, ptr %9, align 8, !tbaa !12
  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 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !13
  br label %16

16:                                               ; preds = %54, %3
  %17 = load i64, ptr %8, align 8, !tbaa !12
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %19, label %58

19:                                               ; preds = %16
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.rl_list, ptr %21, i32 0, i32 2
  %23 = load ptr, ptr %22, align 8, !tbaa !15
  %24 = getelementptr inbounds %struct.rl_list_type, ptr %23, i32 0, i32 1
  %25 = load ptr, ptr %24, align 8, !tbaa !16
  %26 = load i64, ptr %8, align 8, !tbaa !12
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call i32 @rl_read(ptr noundef %20, ptr noundef %25, i64 noundef %26, ptr noundef %27, ptr noundef %11, i32 noundef 1)
  store i32 %28, ptr %12, align 4, !tbaa !13
  %29 = load i32, ptr %12, align 4, !tbaa !13
  %30 = icmp ne i32 2, %29
  br i1 %30, label %31, label %32

31:                                               ; preds = %19
  br label %59

32:                                               ; preds = %19
  %33 = load ptr, ptr %11, align 8, !tbaa !5
  store ptr %33, ptr %10, align 8, !tbaa !5
  store i64 0, ptr %7, align 8, !tbaa !12
  br label %34

34:                                               ; preds = %51, %32
  %35 = load i64, ptr %7, align 8, !tbaa !12
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.rl_list_node, ptr %36, i32 0, i32 0
  %38 = load i64, ptr %37, align 8, !tbaa !18
  %39 = icmp slt i64 %35, %38
  br i1 %39, label %40, label %54

40:                                               ; preds = %34
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.rl_list_node, ptr %41, i32 0, i32 3
  %43 = load ptr, ptr %42, align 8, !tbaa !20
  %44 = load i64, ptr %7, align 8, !tbaa !12
  %45 = getelementptr inbounds ptr, ptr %43, i64 %44
  %46 = load ptr, ptr %45, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i64, ptr %9, align 8, !tbaa !12
  %49 = add nsw i64 %48, 1
  store i64 %49, ptr %9, align 8, !tbaa !12
  %50 = getelementptr inbounds ptr, ptr %47, i64 %48
  store ptr %46, ptr %50, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %40
  %52 = load i64, ptr %7, align 8, !tbaa !12
  %53 = add nsw i64 %52, 1
  store i64 %53, ptr %7, align 8, !tbaa !12
  br label %34, !llvm.loop !21

54:                                               ; preds = %34
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.rl_list_node, ptr %55, i32 0, i32 2
  %57 = load i64, ptr %56, align 8, !tbaa !23
  store i64 %57, ptr %8, align 8, !tbaa !12
  br label %16, !llvm.loop !24

58:                                               ; preds = %16
  store i32 0, ptr %12, align 4, !tbaa !13
  br label %59

59:                                               ; preds = %58, %31
  %60 = load i32, ptr %12, align 4, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, 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
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i32 %60
}

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


; 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 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

declare i32 @rl_read(ptr noundef, ptr noundef, i64 noundef, ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 3, 2) i32 @rl_flatten_list(ptr noundef %0, ptr noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #2 {
  %4 = alloca ptr, align 8
  %5 = getelementptr inbounds i8, ptr %1, i64 24
  %6 = load i64, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %7 = getelementptr inbounds i8, ptr %1, i64 16
  %.not22 = icmp eq i64 %6, 0
  br i1 %.not22, label %._crit_edge27, label %.lr.ph26.preheader

.lr.ph26.preheader:                               ; preds = %3
  %invariant.gep = getelementptr i8, ptr %2, i64 8
  %invariant.gep35 = getelementptr i8, ptr %2, i64 16
  %invariant.gep37 = getelementptr i8, ptr %2, i64 24
  br label %.lr.ph26

.lr.ph26:                                         ; preds = %.lr.ph26.preheader, %._crit_edge
  %.01624 = phi i64 [ %.1.lcssa, %._crit_edge ], [ 0, %.lr.ph26.preheader ]
  %.01723 = phi i64 [ %44, %._crit_edge ], [ %6, %.lr.ph26.preheader ]
  %8 = load ptr, ptr %7, align 8, !tbaa !11
  %9 = getelementptr inbounds i8, ptr %8, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = call i32 @rl_read(ptr noundef %0, ptr noundef %10, i64 noundef %.01723, ptr noundef %1, ptr noundef nonnull %4, i32 noundef 1) #3
  %.not19 = icmp eq i32 %11, 2
  br i1 %.not19, label %12, label %._crit_edge27

12:                                               ; preds = %.lr.ph26
  %13 = load ptr, ptr %4, align 8, !tbaa !15
  %14 = load i64, ptr %13, align 8, !tbaa !16
  %15 = icmp sgt i64 %14, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %12
  %16 = getelementptr inbounds i8, ptr %13, i64 24
  %xtraiter = and i64 %14, 3
  %17 = icmp ult i64 %14, 4
  br i1 %17, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %14, 9223372036854775804
  br label %18

18:                                               ; preds = %18, %.lr.ph.new
  %.01521 = phi i64 [ 0, %.lr.ph.new ], [ %36, %18 ]
  %.120 = phi i64 [ %.01624, %.lr.ph.new ], [ %35, %18 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.3, %18 ]
  %19 = load ptr, ptr %16, align 8, !tbaa !18
  %20 = getelementptr inbounds ptr, ptr %19, i64 %.01521
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %22 = getelementptr inbounds ptr, ptr %2, i64 %.120
  store ptr %21, ptr %22, align 8, !tbaa !15
  %23 = or disjoint i64 %.01521, 1
  %24 = load ptr, ptr %16, align 8, !tbaa !18
  %25 = getelementptr inbounds ptr, ptr %24, i64 %23
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  %gep = getelementptr ptr, ptr %invariant.gep, i64 %.120
  store ptr %26, ptr %gep, align 8, !tbaa !15
  %27 = or disjoint i64 %.01521, 2
  %28 = load ptr, ptr %16, align 8, !tbaa !18
  %29 = getelementptr inbounds ptr, ptr %28, i64 %27
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %gep36 = getelementptr ptr, ptr %invariant.gep35, i64 %.120
  store ptr %30, ptr %gep36, align 8, !tbaa !15
  %31 = or disjoint i64 %.01521, 3
  %32 = load ptr, ptr %16, align 8, !tbaa !18
  %33 = getelementptr inbounds ptr, ptr %32, i64 %31
  %34 = load ptr, ptr %33, align 8, !tbaa !15
  %35 = add nsw i64 %.120, 4
  %gep38 = getelementptr ptr, ptr %invariant.gep37, i64 %.120
  store ptr %34, ptr %gep38, align 8, !tbaa !15
  %36 = add nuw nsw i64 %.01521, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %18, !llvm.loop !19

._crit_edge.loopexit.unr-lcssa:                   ; preds = %18, %.lr.ph
  %.lcssa.ph = phi i64 [ poison, %.lr.ph ], [ %35, %18 ]
  %.01521.unr = phi i64 [ 0, %.lr.ph ], [ %36, %18 ]
  %.120.unr = phi i64 [ %.01624, %.lr.ph ], [ %35, %18 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader
  %.01521.epil = phi i64 [ %42, %.epil.preheader ], [ %.01521.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.120.epil = phi i64 [ %40, %.epil.preheader ], [ %.120.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %37 = load ptr, ptr %16, align 8, !tbaa !18
  %38 = getelementptr inbounds ptr, ptr %37, i64 %.01521.epil
  %39 = load ptr, ptr %38, align 8, !tbaa !15
  %40 = add nsw i64 %.120.epil, 1
  %41 = getelementptr inbounds ptr, ptr %2, i64 %.120.epil
  store ptr %39, ptr %41, align 8, !tbaa !15
  %42 = add nuw nsw i64 %.01521.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.epil.preheader, !llvm.loop !21

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.epil.preheader, %12
  %.1.lcssa = phi i64 [ %.01624, %12 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %40, %.epil.preheader ]
  %43 = getelementptr inbounds i8, ptr %13, i64 16
  %44 = load i64, ptr %43, align 8, !tbaa !23
  %.not = icmp eq i64 %44, 0
  br i1 %.not, label %._crit_edge27, label %.lr.ph26, !llvm.loop !24

._crit_edge27:                                    ; preds = %.lr.ph26, %._crit_edge, %3
  %.0 = phi i32 [ 0, %3 ], [ 0, %._crit_edge ], [ %11, %.lr.ph26 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret i32 %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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, !7, i64 24}
!6 = !{!"rl_list", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !7, i64 32}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!13, !10, i64 8}
!13 = !{!"", !10, i64 0, !10, i64 8, !14, i64 16, !10, i64 24}
!14 = !{!"int", !8, i64 0}
!15 = !{!10, !10, i64 0}
!16 = !{!17, !7, i64 0}
!17 = !{!"rl_list_node", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24}
!18 = !{!17, !10, i64 24}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.unroll.disable"}
!23 = !{!17, !7, i64 16}
!24 = distinct !{!24, !20}


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