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

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

@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@memory = external global [65536 x i8], align 16

; Function Attrs: nounwind uwtable
define dso_local void @init_monitor() #0 {
  %1 = alloca ptr, align 8
  %2 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #3
  %3 = call noalias ptr @fopen(ptr noundef @.str, ptr noundef @.str.1)
  store ptr %3, ptr %1, align 8, !tbaa !5
  %4 = load ptr, ptr %1, align 8, !tbaa !5
  %5 = call i64 @fread(ptr noundef getelementptr inbounds (i8, ptr @memory, i64 40960), i64 noundef 1, i64 noundef 17591, ptr noundef %4)
  %6 = load ptr, ptr %1, align 8, !tbaa !5
  %7 = call i32 @fclose(ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 2, ptr %2) #3
  store i16 -112, ptr %2, align 2, !tbaa !9
  br label %8

8:                                                ; preds = %29, %0
  %9 = load i16, ptr %2, align 2, !tbaa !9
  %10 = zext i16 %9 to i32
  %11 = icmp slt i32 %10, 65523
  br i1 %11, label %13, label %12

12:                                               ; preds = %8
  call void @llvm.lifetime.end.p0(i64 2, ptr %2) #3
  br label %34

13:                                               ; preds = %8
  %14 = load i16, ptr %2, align 2, !tbaa !9
  %15 = zext i16 %14 to i32
  %16 = add nsw i32 %15, 0
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %17
  store i8 76, ptr %18, align 1, !tbaa !11
  %19 = load i16, ptr %2, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  %21 = add nsw i32 %20, 1
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %22
  store i8 0, ptr %23, align 1, !tbaa !11
  %24 = load i16, ptr %2, align 2, !tbaa !9
  %25 = zext i16 %24 to i32
  %26 = add nsw i32 %25, 2
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %27
  store i8 -8, ptr %28, align 1, !tbaa !11
  br label %29

29:                                               ; preds = %13
  %30 = load i16, ptr %2, align 2, !tbaa !9
  %31 = zext i16 %30 to i32
  %32 = add nsw i32 %31, 3
  %33 = trunc i32 %32 to i16
  store i16 %33, ptr %2, align 2, !tbaa !9
  br label %8, !llvm.loop !12

34:                                               ; preds = %12
  store i8 32, ptr getelementptr inbounds ([65536 x i8], ptr @memory, i64 0, i64 61440), align 16, !tbaa !11
  store i8 -108, ptr getelementptr inbounds ([65536 x i8], ptr @memory, i64 0, i64 61441), align 1, !tbaa !11
  store i8 -29, ptr getelementptr inbounds ([65536 x i8], ptr @memory, i64 0, i64 61442), align 2, !tbaa !11
  store i8 0, ptr getelementptr inbounds ([65536 x i8], ptr @memory, i64 0, i64 65532), align 4, !tbaa !11
  store i8 -16, ptr getelementptr inbounds ([65536 x i8], ptr @memory, i64 0, i64 65533), align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #3
  ret void
}

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

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

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #2

declare i32 @fclose(ptr noundef) #2

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

@.str = external hidden unnamed_addr constant [22 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@memory = external global [65536 x i8], align 16

; Function Attrs: nofree nounwind uwtable
define dso_local void @init_monitor() local_unnamed_addr #0 {
  %1 = tail call noalias ptr @fopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1)
  %2 = tail call i64 @fread(ptr noundef nonnull getelementptr inbounds (i8, ptr @memory, i64 40960), i64 noundef 1, i64 noundef 17591, ptr noundef %1)
  %3 = tail call i32 @fclose(ptr noundef %1)
  br label %5

4:                                                ; preds = %5
  store i8 32, ptr getelementptr inbounds (i8, ptr @memory, i64 61440), align 16, !tbaa !5
  store i8 -108, ptr getelementptr inbounds (i8, ptr @memory, i64 61441), align 1, !tbaa !5
  store i8 -29, ptr getelementptr inbounds (i8, ptr @memory, i64 61442), align 2, !tbaa !5
  store i8 0, ptr getelementptr inbounds (i8, ptr @memory, i64 65532), align 4, !tbaa !5
  store i8 -16, ptr getelementptr inbounds (i8, ptr @memory, i64 65533), align 1, !tbaa !5
  ret void

5:                                                ; preds = %16, %0
  %6 = phi i32 [ 65424, %0 ], [ %26, %16 ]
  %7 = zext nneg i32 %6 to i64
  %8 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %7
  store i8 76, ptr %8, align 2, !tbaa !5
  %9 = or disjoint i32 %6, 1
  %10 = zext nneg i32 %9 to i64
  %11 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %10
  store i8 0, ptr %11, align 1, !tbaa !5
  %12 = add nuw nsw i32 %6, 2
  %13 = zext nneg i32 %12 to i64
  %14 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %13
  store i8 -8, ptr %14, align 2, !tbaa !5
  %15 = icmp ult i32 %6, 65520
  br i1 %15, label %16, label %4, !llvm.loop !8

16:                                               ; preds = %5
  %17 = add nuw nsw i32 %6, 3
  %18 = zext nneg i32 %17 to i64
  %19 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %18
  store i8 76, ptr %19, align 1, !tbaa !5
  %20 = add nuw nsw i32 %6, 4
  %21 = zext nneg i32 %20 to i64
  %22 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %21
  store i8 0, ptr %22, align 2, !tbaa !5
  %23 = add nuw nsw i32 %6, 5
  %24 = zext nneg i32 %23 to i64
  %25 = getelementptr inbounds [65536 x i8], ptr @memory, i64 0, i64 %24
  store i8 -8, ptr %25, align 1, !tbaa !5
  %26 = add nuw nsw i32 %6, 6
  br label %5
}

; 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 i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #1

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

attributes #0 = { nofree 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" }

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass