; LLVM IR from aap/pdp6 :: tools (ld6)

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

@mem = external dso_local global [262144 x i64], align 16
@blocksz = external dso_local global i32, align 4
@itemsz = external dso_local global i32, align 4
@block = external dso_local global [18 x i64], align 16
@loc = external dso_local global i32, align 4
@locmin = external dso_local global i32, align 4
@locmax = external dso_local global i32, align 4

; 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 @right(i64 noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @readblock(i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @handlecode() #2 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @readblock(i32 noundef 1)
  %2 = load i64, ptr @block, align 16, !tbaa !5
  %3 = call i32 @right(i64 noundef %2)
  store i32 %3, ptr @loc, align 4, !tbaa !9
  store i32 1, ptr %1, align 4, !tbaa !9
  %4 = load i32, ptr %1, align 4, !tbaa !9
  %5 = load i32, ptr @blocksz, align 4, !tbaa !9
  %6 = icmp ult i32 %4, %5
  br i1 %6, label %7, label %8

7:                                                ; preds = %0
  br label %18

8:                                                ; preds = %0
  br label %9

9:                                                ; preds = %43, %8
  %10 = load i32, ptr @itemsz, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %12, label %44

12:                                               ; preds = %9
  call void @readblock(i32 noundef 1)
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %40, %12
  %14 = load i32, ptr %1, align 4, !tbaa !9
  %15 = load i32, ptr @blocksz, align 4, !tbaa !9
  %16 = icmp ult i32 %14, %15
  br i1 %16, label %17, label %43

17:                                               ; preds = %13
  br label %18

18:                                               ; preds = %17, %7
  %19 = load i32, ptr %1, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [18 x i64], ptr @block, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = load i32, ptr @loc, align 4, !tbaa !9
  %24 = zext i32 %23 to i64
  %25 = getelementptr inbounds [262144 x i64], ptr @mem, i64 0, i64 %24
  store i64 %22, ptr %25, align 8, !tbaa !5
  %26 = load i32, ptr @loc, align 4, !tbaa !9
  %27 = load i32, ptr @locmin, align 4, !tbaa !9
  %28 = icmp ult i32 %26, %27
  br i1 %28, label %29, label %31

29:                                               ; preds = %18
  %30 = load i32, ptr @loc, align 4, !tbaa !9
  store i32 %30, ptr @locmin, align 4, !tbaa !9
  br label %31

31:                                               ; preds = %29, %18
  %32 = load i32, ptr @loc, align 4, !tbaa !9
  %33 = load i32, ptr @locmax, align 4, !tbaa !9
  %34 = icmp ugt i32 %32, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %31
  %36 = load i32, ptr @loc, align 4, !tbaa !9
  store i32 %36, ptr @locmax, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %35, %31
  %38 = load i32, ptr @loc, align 4, !tbaa !9
  %39 = add i32 %38, 1
  store i32 %39, ptr @loc, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %37
  %41 = load i32, ptr %1, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %1, align 4, !tbaa !9
  br label %13, !llvm.loop !11

43:                                               ; preds = %13
  br label %9, !llvm.loop !13

44:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret void
}

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 = !{!"long", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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

@mem = external dso_local local_unnamed_addr global [262144 x i64], align 16
@blocksz = external dso_local local_unnamed_addr global i32, align 4
@itemsz = external dso_local local_unnamed_addr global i32, align 4
@block = external dso_local local_unnamed_addr global [18 x i64], align 16
@loc = external dso_local local_unnamed_addr global i32, align 4
@locmin = external dso_local local_unnamed_addr global i32, align 4
@locmax = external dso_local local_unnamed_addr global i32, align 4

declare i32 @right(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @readblock(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @handlecode() local_unnamed_addr #1 {
  tail call void @readblock(i32 noundef 1)
  %1 = load i64, ptr @block, align 16, !tbaa !5
  %2 = tail call i32 @right(i64 noundef %1) #2
  store i32 %2, ptr @loc, align 4, !tbaa !9
  %3 = load i32, ptr @blocksz, align 4, !tbaa !9
  %4 = icmp ugt i32 %3, 1
  br i1 %4, label %11, label %5

5:                                                ; preds = %8, %0
  %6 = load i32, ptr @itemsz, align 4, !tbaa !9
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %29, label %7

7:                                                ; preds = %5
  tail call void @readblock(i32 noundef 1)
  %.pre6 = load i32, ptr @blocksz, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %26, %7
  %9 = phi i32 [ %12, %26 ], [ %.pre6, %7 ]
  %.0 = phi i32 [ %28, %26 ], [ 0, %7 ]
  %10 = icmp ult i32 %.0, %9
  br i1 %10, label %._crit_edge, label %5, !llvm.loop !11

._crit_edge:                                      ; preds = %8
  %.pre = load i32, ptr @loc, align 4, !tbaa !9
  br label %11

11:                                               ; preds = %._crit_edge, %0
  %12 = phi i32 [ %3, %0 ], [ %9, %._crit_edge ]
  %13 = phi i32 [ %2, %0 ], [ %.pre, %._crit_edge ]
  %.1 = phi i32 [ 1, %0 ], [ %.0, %._crit_edge ]
  %14 = sext i32 %.1 to i64
  %15 = getelementptr inbounds [18 x i64], ptr @block, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !5
  %17 = zext i32 %13 to i64
  %18 = getelementptr inbounds [262144 x i64], ptr @mem, i64 0, i64 %17
  store i64 %16, ptr %18, align 8, !tbaa !5
  %19 = load i32, ptr @locmin, align 4, !tbaa !9
  %20 = icmp ult i32 %13, %19
  br i1 %20, label %21, label %22

21:                                               ; preds = %11
  store i32 %13, ptr @locmin, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %21, %11
  %23 = load i32, ptr @locmax, align 4, !tbaa !9
  %24 = icmp ugt i32 %13, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  store i32 %13, ptr @locmax, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %25, %22
  %27 = add i32 %13, 1
  store i32 %27, ptr @loc, align 4, !tbaa !9
  %28 = add nuw nsw i32 %.1, 1
  br label %8, !llvm.loop !13

29:                                               ; preds = %5
  ret void
}

attributes #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 = { 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 = !{!"long", !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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass