; LLVM IR from Robert-van-Engelen/lisp :: src (lisp-pr)

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

@CONS = external dso_local global i32, align 4
@MACR = external dso_local global i32, align 4
@sp = external dso_local global i32, align 4
@used = external dso_local global [128 x i32], align 16
@cell = external dso_local global [10240 x double], align 16
@env = external dso_local global double, align 8

; 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 @ord(double noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local double @err(i32 noundef) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @sweep() #1

; Function Attrs: nounwind uwtable
declare dso_local void @compact() #1

; Function Attrs: nounwind uwtable
define dso_local i32 @gc() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  call void @llvm.memset.p0.i64(ptr align 16 @used, i8 0, i64 512, i1 false)
  %2 = load i64, ptr @env, align 8, !tbaa !5
  %3 = lshr i64 %2, 48
  %4 = load i32, ptr @CONS, align 4, !tbaa !9
  %5 = zext i32 %4 to i64
  %6 = icmp eq i64 %3, %5
  br i1 %6, label %7, label %10

7:                                                ; preds = %0
  %8 = load double, ptr @env, align 8, !tbaa !11
  %9 = call i32 @ord(double noundef %8)
  call void @mark(i32 noundef %9)
  br label %10

10:                                               ; preds = %7, %0
  %11 = load i32, ptr @sp, align 4, !tbaa !9
  store i32 %11, ptr %1, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %37, %10
  %13 = load i32, ptr %1, align 4, !tbaa !9
  %14 = icmp ult i32 %13, 10240
  br i1 %14, label %15, label %40

15:                                               ; preds = %12
  %16 = load i32, ptr %1, align 4, !tbaa !9
  %17 = zext i32 %16 to i64
  %18 = getelementptr inbounds [10240 x double], ptr @cell, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = lshr i64 %19, 48
  %21 = load i32, ptr @CONS, align 4, !tbaa !9
  %22 = load i32, ptr @MACR, align 4, !tbaa !9
  %23 = xor i32 %21, %22
  %24 = xor i32 %23, -1
  %25 = zext i32 %24 to i64
  %26 = and i64 %20, %25
  %27 = load i32, ptr @CONS, align 4, !tbaa !9
  %28 = zext i32 %27 to i64
  %29 = icmp eq i64 %26, %28
  br i1 %29, label %30, label %36

30:                                               ; preds = %15
  %31 = load i32, ptr %1, align 4, !tbaa !9
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds [10240 x double], ptr @cell, i64 0, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !11
  %35 = call i32 @ord(double noundef %34)
  call void @mark(i32 noundef %35)
  br label %36

36:                                               ; preds = %30, %15
  br label %37

37:                                               ; preds = %36
  %38 = load i32, ptr %1, align 4, !tbaa !9
  %39 = add i32 %38, 1
  store i32 %39, ptr %1, align 4, !tbaa !9
  br label %12, !llvm.loop !13

40:                                               ; preds = %12
  %41 = call i32 @sweep()
  store i32 %41, ptr %1, align 4, !tbaa !9
  call void @compact()
  %42 = load i32, ptr %1, align 4, !tbaa !9
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %47

44:                                               ; preds = %40
  %45 = load i32, ptr %1, align 4, !tbaa !9
  %46 = uitofp i32 %45 to double
  br label %49

47:                                               ; preds = %40
  %48 = call double @err(i32 noundef 7)
  br label %49

49:                                               ; preds = %47, %44
  %50 = phi double [ %46, %44 ], [ %48, %47 ]
  %51 = fptoui double %50 to i32
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  ret i32 %51
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

@CONS = external dso_local local_unnamed_addr global i32, align 4
@MACR = external dso_local local_unnamed_addr global i32, align 4
@sp = external dso_local local_unnamed_addr global i32, align 4
@used = external dso_local local_unnamed_addr global [128 x i32], align 16
@cell = external dso_local local_unnamed_addr global [10240 x double], align 16
@env = external dso_local local_unnamed_addr global double, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @ord(double noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local double @err(i32 noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @sweep() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @compact() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i32 @gc() local_unnamed_addr #0 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(512) @used, i8 0, i64 512, i1 false)
  %1 = load i64, ptr @env, align 8
  %2 = lshr i64 %1, 48
  %3 = load i32, ptr @CONS, align 4, !tbaa !5
  %4 = zext i32 %3 to i64
  %5 = icmp eq i64 %2, %4
  br i1 %5, label %6, label %9

6:                                                ; preds = %0
  %7 = bitcast i64 %1 to double
  %8 = tail call i32 @ord(double noundef %7)
  tail call void @mark(i32 noundef %8)
  br label %9

9:                                                ; preds = %6, %0
  %10 = load i32, ptr @sp, align 4, !tbaa !5
  %11 = icmp ult i32 %10, 10240
  br i1 %11, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %9
  %12 = zext nneg i32 %10 to i64
  %.pre9 = load i32, ptr @CONS, align 4, !tbaa !5
  %.pre11 = load i32, ptr @MACR, align 4, !tbaa !5
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %27
  %13 = phi i32 [ %.pre11, %.lr.ph.preheader ], [ %28, %27 ]
  %14 = phi i32 [ %.pre9, %.lr.ph.preheader ], [ %29, %27 ]
  %indvars.iv = phi i64 [ %12, %.lr.ph.preheader ], [ %indvars.iv.next, %27 ]
  %15 = getelementptr inbounds [10240 x double], ptr @cell, i64 0, i64 %indvars.iv
  %16 = load i64, ptr %15, align 8
  %17 = lshr i64 %16, 48
  %18 = xor i32 %14, %13
  %19 = xor i32 %18, -1
  %20 = zext i32 %19 to i64
  %21 = and i64 %17, %20
  %22 = zext i32 %14 to i64
  %23 = icmp eq i64 %21, %22
  br i1 %23, label %24, label %27

24:                                               ; preds = %.lr.ph
  %25 = bitcast i64 %16 to double
  %26 = tail call i32 @ord(double noundef %25)
  tail call void @mark(i32 noundef %26)
  %.pre = load i32, ptr @CONS, align 4, !tbaa !5
  %.pre10 = load i32, ptr @MACR, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %.lr.ph, %24
  %28 = phi i32 [ %13, %.lr.ph ], [ %.pre10, %24 ]
  %29 = phi i32 [ %14, %.lr.ph ], [ %.pre, %24 ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %30 = and i64 %indvars.iv.next, 4294967295
  %exitcond.not = icmp eq i64 %30, 10240
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %27, %9
  %31 = tail call i32 @sweep()
  tail call void @compact()
  %.not = icmp eq i32 %31, 0
  br i1 %.not, label %34, label %32

32:                                               ; preds = %._crit_edge
  %33 = uitofp i32 %31 to double
  br label %36

34:                                               ; preds = %._crit_edge
  %35 = tail call double @err(i32 noundef 7)
  br label %36

36:                                               ; preds = %34, %32
  %37 = phi double [ %33, %32 ], [ %35, %34 ]
  %38 = fptoui double %37 to i32
  ret i32 %38
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }

!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: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass