; LLVM IR from Robert-van-Engelen/tinylisp :: src (tinylisp-opt)

; 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.anon = type { ptr, ptr, i16 }

@hp = external dso_local global i32, align 4
@sp = external dso_local global i32, align 4
@PRIM = external dso_local global i32, align 4
@NIL = external dso_local global i32, align 4
@err = external dso_local global double, align 8
@env = external dso_local global double, align 8
@nil = external dso_local global double, align 8
@tru = external dso_local global double, align 8
@prim = external dso_local global [21 x %struct.anon], align 16
@.str.27 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [5 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
declare dso_local double @atom(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local double @pair(double noundef, double noundef, double noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local double @eval(double noundef, double noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local double @Read() #0

; Function Attrs: nounwind uwtable
declare dso_local void @print(double noundef) #0

declare i32 @printf(ptr noundef, ...) #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %3 = call i32 (ptr, ...) @printf(ptr noundef @.str.27)
  %4 = load i32, ptr @NIL, align 4, !tbaa !5
  %5 = call double @box(i32 noundef %4, i32 noundef 0)
  store double %5, ptr @nil, align 8, !tbaa !9
  %6 = call double @atom(ptr noundef @.str.28)
  store double %6, ptr @err, align 8, !tbaa !9
  %7 = call double @atom(ptr noundef @.str.29)
  store double %7, ptr @tru, align 8, !tbaa !9
  %8 = load double, ptr @tru, align 8, !tbaa !9
  %9 = load double, ptr @tru, align 8, !tbaa !9
  %10 = load double, ptr @nil, align 8, !tbaa !9
  %11 = call double @pair(double noundef %8, double noundef %9, double noundef %10)
  store double %11, ptr @env, align 8, !tbaa !9
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %31, %0
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [21 x %struct.anon], ptr @prim, i64 0, i64 %14
  %16 = getelementptr inbounds %struct.anon, ptr %15, i32 0, i32 0
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %18 = icmp ne ptr %17, null
  br i1 %18, label %19, label %34

19:                                               ; preds = %12
  %20 = load i32, ptr %2, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [21 x %struct.anon], ptr @prim, i64 0, i64 %21
  %23 = getelementptr inbounds %struct.anon, ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = call double @atom(ptr noundef %24)
  %26 = load i32, ptr @PRIM, align 4, !tbaa !5
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = call double @box(i32 noundef %26, i32 noundef %27)
  %29 = load double, ptr @env, align 8, !tbaa !9
  %30 = call double @pair(double noundef %25, double noundef %28, double noundef %29)
  store double %30, ptr @env, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %19
  %32 = load i32, ptr %2, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %2, align 4, !tbaa !5
  br label %12, !llvm.loop !15

34:                                               ; preds = %12
  br label %35

35:                                               ; preds = %36, %34
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr @sp, align 4, !tbaa !5
  %38 = load i32, ptr @hp, align 4, !tbaa !5
  %39 = udiv i32 %38, 8
  %40 = sub i32 %37, %39
  %41 = call i32 (ptr, ...) @printf(ptr noundef @.str.30, i32 noundef %40)
  %42 = call double @Read()
  %43 = load double, ptr @env, align 8, !tbaa !9
  %44 = call double @eval(double noundef %42, double noundef %43)
  call void @print(double noundef %44)
  call void @gc()
  br label %35
}

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 = { "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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 8, !14, i64 16}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%struct.anon = type { ptr, ptr, i16 }

@hp = external dso_local local_unnamed_addr global i32, align 4
@sp = external dso_local local_unnamed_addr global i32, align 4
@PRIM = external dso_local local_unnamed_addr global i32, align 4
@NIL = external dso_local local_unnamed_addr global i32, align 4
@err = external dso_local local_unnamed_addr global double, align 8
@env = external dso_local local_unnamed_addr global double, align 8
@nil = external dso_local local_unnamed_addr global double, align 8
@tru = external dso_local local_unnamed_addr global double, align 8
@prim = external dso_local local_unnamed_addr global [21 x %struct.anon], align 16
@.str.27 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.28 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.29 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [5 x i8], align 1

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local double @Read() local_unnamed_addr #0

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

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: noreturn nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.27)
  %2 = load i32, ptr @NIL, align 4, !tbaa !5
  %3 = tail call double @box(i32 noundef %2, i32 noundef 0)
  store double %3, ptr @nil, align 8, !tbaa !9
  %4 = tail call double @atom(ptr noundef nonnull @.str.28)
  store double %4, ptr @err, align 8, !tbaa !9
  %5 = tail call double @atom(ptr noundef nonnull @.str.29)
  store double %5, ptr @tru, align 8, !tbaa !9
  %6 = load double, ptr @nil, align 8, !tbaa !9
  %7 = tail call double @pair(double noundef %5, double noundef %5, double noundef %6)
  store double %7, ptr @env, align 8, !tbaa !9
  %8 = load ptr, ptr @prim, align 16, !tbaa !11
  %.not5 = icmp eq ptr %8, null
  br i1 %.not5, label %.preheader.preheader, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %9 = phi ptr [ %17, %.lr.ph ], [ %8, %0 ]
  %10 = tail call double @atom(ptr noundef nonnull %9)
  %11 = load i32, ptr @PRIM, align 4, !tbaa !5
  %12 = trunc nuw nsw i64 %indvars.iv to i32
  %13 = tail call double @box(i32 noundef %11, i32 noundef %12)
  %14 = load double, ptr @env, align 8, !tbaa !9
  %15 = tail call double @pair(double noundef %10, double noundef %13, double noundef %14)
  store double %15, ptr @env, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %16 = getelementptr inbounds [21 x %struct.anon], ptr @prim, i64 0, i64 %indvars.iv.next
  %17 = load ptr, ptr %16, align 8, !tbaa !11
  %.not = icmp eq ptr %17, null
  br i1 %.not, label %.preheader.preheader, label %.lr.ph, !llvm.loop !15

.preheader.preheader:                             ; preds = %.lr.ph, %0
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.preheader
  %18 = load i32, ptr @sp, align 4, !tbaa !5
  %19 = load i32, ptr @hp, align 4, !tbaa !5
  %20 = lshr i32 %19, 3
  %21 = sub i32 %18, %20
  %22 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.30, i32 noundef %21)
  %23 = tail call double @Read()
  %24 = load double, ptr @env, align 8, !tbaa !9
  %25 = tail call double @eval(double noundef %23, double noundef %24)
  tail call void @print(double noundef %25)
  tail call void @gc()
  br label %.preheader
}

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 = { nofree nounwind "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 = { noreturn 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 8, !14, i64 16}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass