; LLVM IR from AlgoGenesis/C :: Recursion/UlamSequence (ulamsequence)

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

@ulamCount = external dso_local global i32, align 4
@ulam = external dso_local global [1000 x i32], align 16

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @isUlam(i32 noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %8

8:                                                ; preds = %47, %1
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = load i32, ptr @ulamCount, align 4, !tbaa !5
  %11 = icmp slt i32 %9, %10
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  store i32 2, ptr %6, align 4
  br label %50

13:                                               ; preds = %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  store i32 %15, ptr %7, align 4, !tbaa !5
  br label %16

16:                                               ; preds = %41, %13
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = load i32, ptr @ulamCount, align 4, !tbaa !5
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 5, ptr %6, align 4
  br label %44

21:                                               ; preds = %16
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [1000 x i32], ptr @ulam, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1000 x i32], ptr @ulam, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %25, %29
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = icmp eq i32 %30, %31
  br i1 %32, label %33, label %40

33:                                               ; preds = %21
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %4, align 4, !tbaa !5
  %36 = load i32, ptr %4, align 4, !tbaa !5
  %37 = icmp sgt i32 %36, 1
  br i1 %37, label %38, label %39

38:                                               ; preds = %33
  store i1 false, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %44

39:                                               ; preds = %33
  br label %40

40:                                               ; preds = %39, %21
  br label %41

41:                                               ; preds = %40
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %7, align 4, !tbaa !5
  br label %16, !llvm.loop !9

44:                                               ; preds = %38, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  %45 = load i32, ptr %6, align 4
  switch i32 %45, label %50 [
    i32 5, label %46
  ]

46:                                               ; preds = %44
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !5
  br label %8, !llvm.loop !11

50:                                               ; preds = %44, %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  %51 = load i32, ptr %6, align 4
  switch i32 %51, label %55 [
    i32 2, label %52
  ]

52:                                               ; preds = %50
  %53 = load i32, ptr %4, align 4, !tbaa !5
  %54 = icmp eq i32 %53, 1
  store i1 %54, ptr %2, align 1
  store i32 1, ptr %6, align 4
  br label %55

55:                                               ; preds = %52, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  %56 = load i1, ptr %2, align 1
  ret i1 %56
}

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

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

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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

@ulamCount = external dso_local local_unnamed_addr global i32, align 4
@ulam = external dso_local local_unnamed_addr global [1000 x i32], align 16

; Function Attrs: nofree norecurse nosync nounwind memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local zeroext i1 @isUlam(i32 noundef %0) local_unnamed_addr #0 {
  %2 = load i32, ptr @ulamCount, align 4, !tbaa !5
  %.not2434 = icmp slt i32 %2, 1
  br i1 %.not2434, label %.thread, label %.lr.ph38.preheader

.lr.ph38.preheader:                               ; preds = %1
  %3 = zext nneg i32 %2 to i64
  %wide.trip.count48 = zext nneg i32 %2 to i64
  br label %.lr.ph38

.loopexit:                                        ; preds = %14, %.lr.ph38
  %.117.lcssa = phi i32 [ %.01635, %.lr.ph38 ], [ %.2, %14 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond49 = icmp eq i64 %indvars.iv.next47, %wide.trip.count48
  br i1 %exitcond49, label %.thread.loopexit41, label %.lr.ph38

.lr.ph38:                                         ; preds = %.lr.ph38.preheader, %.loopexit
  %indvars.iv46 = phi i64 [ 0, %.lr.ph38.preheader ], [ %indvars.iv.next47, %.loopexit ]
  %indvars.iv = phi i64 [ 1, %.lr.ph38.preheader ], [ %indvars.iv.next, %.loopexit ]
  %.01635 = phi i32 [ 0, %.lr.ph38.preheader ], [ %.117.lcssa, %.loopexit ]
  %indvars.iv.next47 = add nuw nsw i64 %indvars.iv46, 1
  %.not31 = icmp ult i64 %indvars.iv.next47, %3
  br i1 %.not31, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph38
  %4 = getelementptr inbounds [1000 x i32], ptr @ulam, i64 0, i64 %indvars.iv46
  %5 = load i32, ptr %4, align 4, !tbaa !5
  br label %6

6:                                                ; preds = %.lr.ph, %14
  %indvars.iv43 = phi i64 [ %indvars.iv, %.lr.ph ], [ %indvars.iv.next44, %14 ]
  %.11732 = phi i32 [ %.01635, %.lr.ph ], [ %.2, %14 ]
  %7 = getelementptr inbounds [1000 x i32], ptr @ulam, i64 0, i64 %indvars.iv43
  %8 = load i32, ptr %7, align 4, !tbaa !5
  %9 = add nsw i32 %8, %5
  %10 = icmp eq i32 %9, %0
  br i1 %10, label %11, label %14

11:                                               ; preds = %6
  %12 = add nsw i32 %.11732, 1
  %13 = icmp sgt i32 %.11732, 0
  br i1 %13, label %.thread, label %14

14:                                               ; preds = %6, %11
  %.2 = phi i32 [ %12, %11 ], [ %.11732, %6 ]
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next44, %wide.trip.count48
  br i1 %exitcond.not, label %.loopexit, label %6, !llvm.loop !9

.thread.loopexit41:                               ; preds = %.loopexit
  %.not24 = icmp uge i64 %indvars.iv.next47, %3
  %15 = icmp eq i32 %.117.lcssa, 1
  %16 = select i1 %.not24, i1 %15, i1 false
  br label %.thread

.thread:                                          ; preds = %11, %.thread.loopexit41, %1
  %.not2429 = phi i1 [ false, %1 ], [ %16, %.thread.loopexit41 ], [ false, %11 ]
  ret i1 %.not2429
}

attributes #0 = { nofree norecurse nosync nounwind memory(read, argmem: none, inaccessiblemem: none) 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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