; LLVM IR from freakout42/macrocalc :: mc/mex (display)

; 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.TERM = type { i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }
%struct.VIDEO = type { i32, i32, [32768 x i8] }

@vscreen = external dso_local global ptr, align 8
@pscreen = external dso_local global ptr, align 8
@term = external global %struct.TERM, align 8

; Function Attrs: nounwind uwtable
define dso_local void @vtinit() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #4
  store i32 0, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %4 = load ptr, ptr getelementptr inbounds (%struct.TERM, ptr @term, i32 0, i32 2), align 8, !tbaa !9
  %5 = call i32 (...) %4()
  %6 = load ptr, ptr @vscreen, align 8, !tbaa !12
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %25

8:                                                ; preds = %0
  store i32 1, ptr %1, align 4, !tbaa !5
  %9 = load i32, ptr @term, align 8, !tbaa !13
  %10 = sext i32 %9 to i64
  %11 = mul i64 %10, 8
  %12 = call noalias ptr @malloc(i64 noundef %11) #5
  store ptr %12, ptr @vscreen, align 8, !tbaa !12
  %13 = load ptr, ptr @vscreen, align 8, !tbaa !12
  %14 = icmp eq ptr %13, null
  br i1 %14, label %15, label %16

15:                                               ; preds = %8
  call void @exit(i32 noundef 1) #6
  unreachable

16:                                               ; preds = %8
  %17 = load i32, ptr @term, align 8, !tbaa !13
  %18 = sext i32 %17 to i64
  %19 = mul i64 %18, 8
  %20 = call noalias ptr @malloc(i64 noundef %19) #5
  store ptr %20, ptr @pscreen, align 8, !tbaa !12
  %21 = load ptr, ptr @pscreen, align 8, !tbaa !12
  %22 = icmp eq ptr %21, null
  br i1 %22, label %23, label %24

23:                                               ; preds = %16
  call void @exit(i32 noundef 1) #6
  unreachable

24:                                               ; preds = %16
  br label %25

25:                                               ; preds = %24, %0
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %82, %25
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = load i32, ptr @term, align 8, !tbaa !13
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %30, label %85

30:                                               ; preds = %26
  %31 = load i32, ptr %1, align 4, !tbaa !5
  %32 = icmp ne i32 %31, 0
  br i1 %32, label %33, label %44

33:                                               ; preds = %30
  %34 = call noalias ptr @malloc(i64 noundef 32776) #5
  store ptr %34, ptr %3, align 8, !tbaa !12
  %35 = load ptr, ptr %3, align 8, !tbaa !12
  %36 = icmp eq ptr %35, null
  br i1 %36, label %37, label %38

37:                                               ; preds = %33
  call void @exit(i32 noundef 1) #6
  unreachable

38:                                               ; preds = %33
  %39 = load ptr, ptr %3, align 8, !tbaa !12
  %40 = load ptr, ptr @vscreen, align 8, !tbaa !12
  %41 = load i32, ptr %2, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds ptr, ptr %40, i64 %42
  store ptr %39, ptr %43, align 8, !tbaa !12
  br label %50

44:                                               ; preds = %30
  %45 = load ptr, ptr @vscreen, align 8, !tbaa !12
  %46 = load i32, ptr %2, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds ptr, ptr %45, i64 %47
  %49 = load ptr, ptr %48, align 8, !tbaa !12
  store ptr %49, ptr %3, align 8, !tbaa !12
  br label %50

50:                                               ; preds = %44, %38
  %51 = load ptr, ptr %3, align 8, !tbaa !12
  %52 = getelementptr inbounds %struct.VIDEO, ptr %51, i32 0, i32 1
  store i32 0, ptr %52, align 4, !tbaa !14
  %53 = load ptr, ptr %3, align 8, !tbaa !12
  %54 = getelementptr inbounds %struct.VIDEO, ptr %53, i32 0, i32 0
  store i32 0, ptr %54, align 4, !tbaa !16
  %55 = load i32, ptr %1, align 4, !tbaa !5
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %71

57:                                               ; preds = %50
  %58 = load i32, ptr getelementptr inbounds (%struct.TERM, ptr @term, i32 0, i32 1), align 4, !tbaa !17
  %59 = sext i32 %58 to i64
  %60 = add i64 8, %59
  %61 = call noalias ptr @malloc(i64 noundef %60) #5
  store ptr %61, ptr %3, align 8, !tbaa !12
  %62 = load ptr, ptr %3, align 8, !tbaa !12
  %63 = icmp eq ptr %62, null
  br i1 %63, label %64, label %65

64:                                               ; preds = %57
  call void @exit(i32 noundef 1) #6
  unreachable

65:                                               ; preds = %57
  %66 = load ptr, ptr %3, align 8, !tbaa !12
  %67 = load ptr, ptr @pscreen, align 8, !tbaa !12
  %68 = load i32, ptr %2, align 4, !tbaa !5
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds ptr, ptr %67, i64 %69
  store ptr %66, ptr %70, align 8, !tbaa !12
  br label %77

71:                                               ; preds = %50
  %72 = load ptr, ptr @pscreen, align 8, !tbaa !12
  %73 = load i32, ptr %2, align 4, !tbaa !5
  %74 = sext i32 %73 to i64
  %75 = getelementptr inbounds ptr, ptr %72, i64 %74
  %76 = load ptr, ptr %75, align 8, !tbaa !12
  store ptr %76, ptr %3, align 8, !tbaa !12
  br label %77

77:                                               ; preds = %71, %65
  %78 = load ptr, ptr %3, align 8, !tbaa !12
  %79 = getelementptr inbounds %struct.VIDEO, ptr %78, i32 0, i32 1
  store i32 0, ptr %79, align 4, !tbaa !14
  %80 = load ptr, ptr %3, align 8, !tbaa !12
  %81 = getelementptr inbounds %struct.VIDEO, ptr %80, i32 0, i32 0
  store i32 0, ptr %81, align 4, !tbaa !16
  br label %82

82:                                               ; preds = %77
  %83 = load i32, ptr %2, align 4, !tbaa !5
  %84 = add nsw i32 %83, 1
  store i32 %84, ptr %2, align 4, !tbaa !5
  br label %26, !llvm.loop !18

85:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #4
  ret void
}

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

; 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 = { nounwind allocsize(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 #3 = { 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { noreturn 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, !11, i64 8}
!10 = !{!"", !6, i64 0, !6, i64 4, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40, !11, i64 48, !11, i64 56, !11, i64 64, !11, i64 72, !11, i64 80, !11, i64 88, !11, i64 96}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !6, i64 0}
!14 = !{!15, !6, i64 4}
!15 = !{!"VIDEO", !6, i64 0, !6, i64 4, !7, i64 8}
!16 = !{!15, !6, i64 0}
!17 = !{!10, !6, i64 4}
!18 = distinct !{!18, !19}
!19 = !{!"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.TERM = type { i32, i32, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr, ptr }

@vscreen = external dso_local local_unnamed_addr global ptr, align 8
@pscreen = external dso_local local_unnamed_addr global ptr, align 8
@term = external local_unnamed_addr global %struct.TERM, align 8

; Function Attrs: nounwind uwtable
define dso_local void @vtinit() local_unnamed_addr #0 {
  %1 = load ptr, ptr getelementptr inbounds (i8, ptr @term, i64 8), align 8, !tbaa !5
  %2 = tail call i32 (...) %1() #3
  %3 = load ptr, ptr @vscreen, align 8, !tbaa !11
  %.not = icmp eq ptr %3, null
  %.pre = load i32, ptr @term, align 8, !tbaa !12
  br i1 %.not, label %4, label %.thread

4:                                                ; preds = %0
  %5 = sext i32 %.pre to i64
  %6 = shl nsw i64 %5, 3
  %7 = tail call noalias ptr @malloc(i64 noundef %6) #4
  store ptr %7, ptr @vscreen, align 8, !tbaa !11
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %10

9:                                                ; preds = %4
  tail call void @exit(i32 noundef 1) #5
  unreachable

10:                                               ; preds = %4
  %11 = tail call noalias ptr @malloc(i64 noundef %6) #4
  store ptr %11, ptr @pscreen, align 8, !tbaa !11
  %12 = icmp eq ptr %11, null
  br i1 %12, label %13, label %14

13:                                               ; preds = %10
  tail call void @exit(i32 noundef 1) #5
  unreachable

14:                                               ; preds = %10
  %15 = icmp sgt i32 %.pre, 0
  br i1 %15, label %.lr.ph, label %._crit_edge

.thread:                                          ; preds = %0
  %16 = icmp sgt i32 %.pre, 0
  br i1 %16, label %.lr.ph.split.preheader, label %._crit_edge

.lr.ph:                                           ; preds = %14
  %17 = load i32, ptr getelementptr inbounds (i8, ptr @term, i64 4), align 4
  %18 = sext i32 %17 to i64
  %19 = add nsw i64 %18, 8
  br i1 %.not, label %.lr.ph.split.us.preheader, label %.lr.ph.split.preheader

.lr.ph.split.preheader:                           ; preds = %.thread, %.lr.ph
  %20 = phi ptr [ %7, %.lr.ph ], [ %3, %.thread ]
  %wide.trip.count = zext nneg i32 %.pre to i64
  %.pre30 = load ptr, ptr @pscreen, align 8, !tbaa !11
  %xtraiter = and i64 %wide.trip.count, 1
  %21 = icmp eq i32 %.pre, 1
  br i1 %21, label %._crit_edge.loopexit33.unr-lcssa, label %.lr.ph.split.preheader.new

.lr.ph.split.preheader.new:                       ; preds = %.lr.ph.split.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph.split

.lr.ph.split.us.preheader:                        ; preds = %.lr.ph
  %wide.trip.count28 = zext nneg i32 %.pre to i64
  br label %.lr.ph.split.us

.lr.ph.split.us:                                  ; preds = %.lr.ph.split.us.preheader, %30
  %indvars.iv25 = phi i64 [ 0, %.lr.ph.split.us.preheader ], [ %indvars.iv.next26, %30 ]
  %22 = tail call noalias dereferenceable_or_null(32776) ptr @malloc(i64 noundef 32776) #4
  %23 = icmp eq ptr %22, null
  br i1 %23, label %.split.us, label %24

24:                                               ; preds = %.lr.ph.split.us
  %25 = load ptr, ptr @vscreen, align 8, !tbaa !11
  %26 = getelementptr inbounds ptr, ptr %25, i64 %indvars.iv25
  store ptr %22, ptr %26, align 8, !tbaa !11
  %27 = getelementptr inbounds i8, ptr %22, i64 4
  store i32 0, ptr %27, align 4, !tbaa !13
  store i32 0, ptr %22, align 4, !tbaa !15
  %28 = tail call noalias ptr @malloc(i64 noundef %19) #4
  %29 = icmp eq ptr %28, null
  br i1 %29, label %.split22.us, label %30

30:                                               ; preds = %24
  %31 = load ptr, ptr @pscreen, align 8, !tbaa !11
  %32 = getelementptr inbounds ptr, ptr %31, i64 %indvars.iv25
  store ptr %28, ptr %32, align 8, !tbaa !11
  %33 = getelementptr inbounds i8, ptr %28, i64 4
  store i32 0, ptr %33, align 4, !tbaa !13
  store i32 0, ptr %28, align 4, !tbaa !15
  %indvars.iv.next26 = add nuw nsw i64 %indvars.iv25, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next26, %wide.trip.count28
  br i1 %exitcond29.not, label %._crit_edge, label %.lr.ph.split.us, !llvm.loop !16

.lr.ph.split:                                     ; preds = %.lr.ph.split, %.lr.ph.split.preheader.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.split.preheader.new ], [ %indvars.iv.next.1, %.lr.ph.split ]
  %niter = phi i64 [ 0, %.lr.ph.split.preheader.new ], [ %niter.next.1, %.lr.ph.split ]
  %34 = getelementptr inbounds ptr, ptr %20, i64 %indvars.iv
  %35 = load ptr, ptr %34, align 8, !tbaa !11
  %36 = getelementptr inbounds i8, ptr %35, i64 4
  store i32 0, ptr %36, align 4, !tbaa !13
  store i32 0, ptr %35, align 4, !tbaa !15
  %37 = getelementptr inbounds ptr, ptr %.pre30, i64 %indvars.iv
  %38 = load ptr, ptr %37, align 8, !tbaa !11
  %39 = getelementptr inbounds i8, ptr %38, i64 4
  store i32 0, ptr %39, align 4, !tbaa !13
  store i32 0, ptr %38, align 4, !tbaa !15
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %40 = getelementptr inbounds ptr, ptr %20, i64 %indvars.iv.next
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = getelementptr inbounds i8, ptr %41, i64 4
  store i32 0, ptr %42, align 4, !tbaa !13
  store i32 0, ptr %41, align 4, !tbaa !15
  %43 = getelementptr inbounds ptr, ptr %.pre30, i64 %indvars.iv.next
  %44 = load ptr, ptr %43, align 8, !tbaa !11
  %45 = getelementptr inbounds i8, ptr %44, i64 4
  store i32 0, ptr %45, align 4, !tbaa !13
  store i32 0, ptr %44, align 4, !tbaa !15
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit33.unr-lcssa, label %.lr.ph.split, !llvm.loop !16

.split.us:                                        ; preds = %.lr.ph.split.us
  tail call void @exit(i32 noundef 1) #5
  unreachable

.split22.us:                                      ; preds = %24
  tail call void @exit(i32 noundef 1) #5
  unreachable

._crit_edge.loopexit33.unr-lcssa:                 ; preds = %.lr.ph.split, %.lr.ph.split.preheader
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph.split.preheader ], [ %indvars.iv.next.1, %.lr.ph.split ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.split.epil

.lr.ph.split.epil:                                ; preds = %._crit_edge.loopexit33.unr-lcssa
  %46 = getelementptr inbounds ptr, ptr %20, i64 %indvars.iv.unr
  %47 = load ptr, ptr %46, align 8, !tbaa !11
  %48 = getelementptr inbounds i8, ptr %47, i64 4
  store i32 0, ptr %48, align 4, !tbaa !13
  store i32 0, ptr %47, align 4, !tbaa !15
  %49 = getelementptr inbounds ptr, ptr %.pre30, i64 %indvars.iv.unr
  %50 = load ptr, ptr %49, align 8, !tbaa !11
  %51 = getelementptr inbounds i8, ptr %50, i64 4
  store i32 0, ptr %51, align 4, !tbaa !13
  store i32 0, ptr %50, align 4, !tbaa !15
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.split.epil, %._crit_edge.loopexit33.unr-lcssa, %30, %.thread, %14
  ret void
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

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

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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 }
attributes #4 = { nounwind allocsize(0) }
attributes #5 = { noreturn 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, !10, i64 8}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16, !10, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !10, i64 80, !10, i64 88, !10, i64 96}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !7, i64 4}
!14 = !{!"VIDEO", !7, i64 0, !7, i64 4, !8, i64 8}
!15 = !{!14, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass