; LLVM IR from mist64/perfect6502 :: cbmbasic (runtime)

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

@PC = external global i16, align 2
@interactive = external dso_local global i32, align 4
@.str = external hidden unnamed_addr constant [2 x i8], align 1
@input_file = external dso_local global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @init_os(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %12, label %9

9:                                                ; preds = %2
  %10 = load i16, ptr @PC, align 2, !tbaa !11
  %11 = zext i16 %10 to i32
  store i32 %11, ptr %3, align 4
  br label %55

12:                                               ; preds = %2
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = icmp sgt i32 %13, 1
  br i1 %14, label %15, label %51

15:                                               ; preds = %12
  store i32 0, ptr @interactive, align 4, !tbaa !5
  %16 = load ptr, ptr %5, align 8, !tbaa !9
  %17 = getelementptr inbounds ptr, ptr %16, i64 1
  %18 = load ptr, ptr %17, align 8, !tbaa !9
  %19 = call noalias ptr @fopen(ptr noundef %18, ptr noundef @.str)
  store ptr %19, ptr @input_file, align 8, !tbaa !9
  %20 = load ptr, ptr @input_file, align 8, !tbaa !9
  %21 = icmp ne ptr %20, null
  br i1 %21, label %27, label %22

22:                                               ; preds = %15
  %23 = load ptr, ptr %5, align 8, !tbaa !9
  %24 = getelementptr inbounds ptr, ptr %23, i64 1
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str.1, ptr noundef %25)
  call void @exit(i32 noundef 1) #5
  unreachable

27:                                               ; preds = %15
  %28 = load ptr, ptr @input_file, align 8, !tbaa !9
  %29 = call i32 @fgetc(ptr noundef %28)
  %30 = icmp eq i32 %29, 35
  br i1 %30, label %31, label %47

31:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #6
  br label %32

32:                                               ; preds = %44, %31
  %33 = load ptr, ptr @input_file, align 8, !tbaa !9
  %34 = call i32 @fgetc(ptr noundef %33)
  %35 = trunc i32 %34 to i8
  store i8 %35, ptr %6, align 1, !tbaa !13
  br label %36

36:                                               ; preds = %32
  %37 = load i8, ptr %6, align 1, !tbaa !13
  %38 = sext i8 %37 to i32
  %39 = icmp ne i32 %38, 13
  br i1 %39, label %40, label %44

40:                                               ; preds = %36
  %41 = load i8, ptr %6, align 1, !tbaa !13
  %42 = sext i8 %41 to i32
  %43 = icmp ne i32 %42, 10
  br label %44

44:                                               ; preds = %40, %36
  %45 = phi i1 [ false, %36 ], [ %43, %40 ]
  br i1 %45, label %32, label %46, !llvm.loop !14

46:                                               ; preds = %44
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #6
  br label %50

47:                                               ; preds = %27
  %48 = load ptr, ptr @input_file, align 8, !tbaa !9
  %49 = call i32 @fseek(ptr noundef %48, i64 noundef 0, i32 noundef 0)
  br label %50

50:                                               ; preds = %47, %46
  br label %52

51:                                               ; preds = %12
  store i32 1, ptr @interactive, align 4, !tbaa !5
  store ptr null, ptr @input_file, align 8, !tbaa !9
  br label %52

52:                                               ; preds = %51, %50
  %53 = call i64 @time(ptr noundef null) #6
  %54 = trunc i64 %53 to i32
  call void @srand(i32 noundef %54) #6
  store i32 58260, ptr %3, align 4
  br label %55

55:                                               ; preds = %52, %9
  %56 = load i32, ptr %3, align 4
  ret i32 %56
}

declare noalias ptr @fopen(ptr noundef, ptr noundef) #1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #2

declare i32 @fgetc(ptr noundef) #1

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

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

declare i32 @fseek(ptr noundef, i64 noundef, i32 noundef) #1

; Function Attrs: nounwind
declare void @srand(i32 noundef) #4

; Function Attrs: nounwind
declare i64 @time(ptr noundef) #4

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 = { "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 "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { 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 #5 = { noreturn nounwind }
attributes #6 = { 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 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"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"

@PC = external local_unnamed_addr global i16, align 2
@interactive = external dso_local local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [2 x i8], align 1
@input_file = external dso_local local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [19 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 65536) i32 @init_os(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %.not = icmp eq i32 %0, 0
  br i1 %.not, label %3, label %6

3:                                                ; preds = %2
  %4 = load i16, ptr @PC, align 2, !tbaa !5
  %5 = zext i16 %4 to i32
  br label %26

6:                                                ; preds = %2
  %7 = icmp sgt i32 %0, 1
  br i1 %7, label %8, label %23

8:                                                ; preds = %6
  store i32 0, ptr @interactive, align 4, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %1, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = tail call noalias ptr @fopen(ptr noundef %10, ptr noundef nonnull @.str)
  store ptr %11, ptr @input_file, align 8, !tbaa !11
  %.not8 = icmp eq ptr %11, null
  br i1 %.not8, label %12, label %15

12:                                               ; preds = %8
  %13 = load ptr, ptr %9, align 8, !tbaa !11
  %14 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, ptr noundef %13)
  tail call void @exit(i32 noundef 1) #4
  unreachable

15:                                               ; preds = %8
  %16 = tail call i32 @fgetc(ptr noundef nonnull %11)
  %17 = icmp eq i32 %16, 35
  br i1 %17, label %.preheader, label %20

.preheader:                                       ; preds = %15, %.preheader
  %18 = load ptr, ptr @input_file, align 8, !tbaa !11
  %19 = tail call i32 @fgetc(ptr noundef %18)
  %trunc = trunc i32 %19 to i8
  switch i8 %trunc, label %.preheader [
    i8 13, label %.loopexit
    i8 10, label %.loopexit
  ]

20:                                               ; preds = %15
  %21 = load ptr, ptr @input_file, align 8, !tbaa !11
  %22 = tail call i32 @fseek(ptr noundef %21, i64 noundef 0, i32 noundef 0)
  br label %.loopexit

23:                                               ; preds = %6
  store i32 1, ptr @interactive, align 4, !tbaa !9
  store ptr null, ptr @input_file, align 8, !tbaa !11
  br label %.loopexit

.loopexit:                                        ; preds = %.preheader, %.preheader, %20, %23
  %24 = tail call i64 @time(ptr noundef null) #5
  %25 = trunc i64 %24 to i32
  tail call void @srand(i32 noundef %25) #5
  br label %26

26:                                               ; preds = %.loopexit, %3
  %.0 = phi i32 [ 58260, %.loopexit ], [ %5, %3 ]
  ret i32 %.0
}

; Function Attrs: nofree nounwind
declare noalias noundef ptr @fopen(ptr nocapture noundef readonly, ptr nocapture noundef readonly) local_unnamed_addr #1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @fgetc(ptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fseek(ptr nocapture noundef, i64 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind
declare void @srand(i32 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i64 @time(ptr noundef) local_unnamed_addr #3

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 "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 "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 #4 = { noreturn nounwind }
attributes #5 = { 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 = !{!"short", !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 = !{!"any pointer", !7, i64 0}


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