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

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

@buf = external dso_local global [40 x i8], align 16

; 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 void @look() #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @seeing(i8 noundef signext) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @get() #1

; Function Attrs: nounwind uwtable
define dso_local signext i8 @scan() #1 {
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !5
  br label %2

2:                                                ; preds = %5, %0
  %3 = call i32 @seeing(i8 noundef signext 32)
  %4 = icmp ne i32 %3, 0
  br i1 %4, label %5, label %6

5:                                                ; preds = %2
  call void @look()
  br label %2, !llvm.loop !9

6:                                                ; preds = %2
  %7 = call i32 @seeing(i8 noundef signext 40)
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %15, label %9

9:                                                ; preds = %6
  %10 = call i32 @seeing(i8 noundef signext 41)
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %15, label %12

12:                                               ; preds = %9
  %13 = call i32 @seeing(i8 noundef signext 39)
  %14 = icmp ne i32 %13, 0
  br i1 %14, label %15, label %21

15:                                               ; preds = %12, %9, %6
  %16 = call signext i8 @get()
  %17 = load i32, ptr %1, align 4, !tbaa !5
  %18 = add i32 %17, 1
  store i32 %18, ptr %1, align 4, !tbaa !5
  %19 = zext i32 %17 to i64
  %20 = getelementptr inbounds [40 x i8], ptr @buf, i64 0, i64 %19
  store i8 %16, ptr %20, align 1, !tbaa !11
  br label %44

21:                                               ; preds = %12
  br label %22

22:                                               ; preds = %41, %21
  %23 = call signext i8 @get()
  %24 = load i32, ptr %1, align 4, !tbaa !5
  %25 = add i32 %24, 1
  store i32 %25, ptr %1, align 4, !tbaa !5
  %26 = zext i32 %24 to i64
  %27 = getelementptr inbounds [40 x i8], ptr @buf, i64 0, i64 %26
  store i8 %23, ptr %27, align 1, !tbaa !11
  br label %28

28:                                               ; preds = %22
  %29 = load i32, ptr %1, align 4, !tbaa !5
  %30 = icmp ult i32 %29, 39
  br i1 %30, label %31, label %41

31:                                               ; preds = %28
  %32 = call i32 @seeing(i8 noundef signext 40)
  %33 = icmp ne i32 %32, 0
  br i1 %33, label %41, label %34

34:                                               ; preds = %31
  %35 = call i32 @seeing(i8 noundef signext 41)
  %36 = icmp ne i32 %35, 0
  br i1 %36, label %41, label %37

37:                                               ; preds = %34
  %38 = call i32 @seeing(i8 noundef signext 32)
  %39 = icmp ne i32 %38, 0
  %40 = xor i1 %39, true
  br label %41

41:                                               ; preds = %37, %34, %31, %28
  %42 = phi i1 [ false, %34 ], [ false, %31 ], [ false, %28 ], [ %40, %37 ]
  br i1 %42, label %22, label %43, !llvm.loop !12

43:                                               ; preds = %41
  br label %44

44:                                               ; preds = %43, %15
  %45 = load i32, ptr %1, align 4, !tbaa !5
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds [40 x i8], ptr @buf, i64 0, i64 %46
  store i8 0, ptr %47, align 1, !tbaa !11
  %48 = load i8, ptr @buf, align 16, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  ret i8 %48
}

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 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !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"

@buf = external dso_local local_unnamed_addr global [40 x i8], align 16

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @seeing(i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @get() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local signext i8 @scan() local_unnamed_addr #0 {
  %1 = tail call i32 @seeing(i8 noundef signext 32)
  %.not12 = icmp eq i32 %1, 0
  br i1 %.not12, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %0, %.lr.ph
  tail call void @look()
  %2 = tail call i32 @seeing(i8 noundef signext 32)
  %.not = icmp eq i32 %2, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

._crit_edge:                                      ; preds = %.lr.ph, %0
  %3 = tail call i32 @seeing(i8 noundef signext 40)
  %.not6 = icmp eq i32 %3, 0
  br i1 %.not6, label %4, label %8

4:                                                ; preds = %._crit_edge
  %5 = tail call i32 @seeing(i8 noundef signext 41)
  %.not7 = icmp eq i32 %5, 0
  br i1 %.not7, label %6, label %8

6:                                                ; preds = %4
  %7 = tail call i32 @seeing(i8 noundef signext 39)
  %.not8 = icmp eq i32 %7, 0
  br i1 %.not8, label %.preheader, label %8

8:                                                ; preds = %6, %4, %._crit_edge
  %9 = tail call signext i8 @get()
  store i8 %9, ptr @buf, align 16, !tbaa !7
  br label %.critedge

.preheader:                                       ; preds = %6, %16
  %indvars.iv = phi i64 [ %indvars.iv.next, %16 ], [ 0, %6 ]
  %10 = tail call signext i8 @get()
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %11 = getelementptr inbounds [40 x i8], ptr @buf, i64 0, i64 %indvars.iv
  store i8 %10, ptr %11, align 1, !tbaa !7
  %exitcond.not = icmp eq i64 %indvars.iv, 38
  br i1 %exitcond.not, label %.critedge.loopexit, label %12

12:                                               ; preds = %.preheader
  %13 = tail call i32 @seeing(i8 noundef signext 40)
  %.not9 = icmp eq i32 %13, 0
  br i1 %.not9, label %14, label %.critedge.loopexit

14:                                               ; preds = %12
  %15 = tail call i32 @seeing(i8 noundef signext 41)
  %.not10 = icmp eq i32 %15, 0
  br i1 %.not10, label %16, label %.critedge.loopexit

16:                                               ; preds = %14
  %17 = tail call i32 @seeing(i8 noundef signext 32)
  %.not11 = icmp eq i32 %17, 0
  br i1 %.not11, label %.preheader, label %.critedge.loopexit, !llvm.loop !10

.critedge.loopexit:                               ; preds = %16, %.preheader, %12, %14
  %.lcssa = phi i64 [ %indvars.iv.next, %16 ], [ 39, %.preheader ], [ %indvars.iv.next, %12 ], [ %indvars.iv.next, %14 ]
  %18 = and i64 %.lcssa, 4294967295
  br label %.critedge

.critedge:                                        ; preds = %.critedge.loopexit, %8
  %.1 = phi i64 [ 1, %8 ], [ %18, %.critedge.loopexit ]
  %19 = getelementptr inbounds [40 x i8], ptr @buf, i64 0, i64 %.1
  store i8 0, ptr %19, align 1, !tbaa !7
  %20 = load i8, ptr @buf, align 16, !tbaa !7
  ret i8 %20
}

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

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}
!7 = !{!8, !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = distinct !{!10, !6}


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