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

; 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.LINE = type { ptr, ptr, i16, i16, [32768 x i8] }

@kbufp = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [18 x i8], align 1
@kused = external global i32, align 4
@ksize = external global i32, align 4

; 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
define dso_local i32 @ppkill(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 1, ptr %8, align 4, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %6, align 8, !tbaa !5
  br label %11

11:                                               ; preds = %24, %2
  %12 = load ptr, ptr %6, align 8, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp ne ptr %12, %13
  br i1 %14, label %15, label %28

15:                                               ; preds = %11
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.LINE, ptr %16, i32 0, i32 3
  %18 = load i16, ptr %17, align 2, !tbaa !11
  %19 = sext i16 %18 to i32
  %20 = and i32 65535, %19
  %21 = add nsw i32 %20, 1
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = add nsw i32 %22, %21
  store i32 %23, ptr %8, align 4, !tbaa !9
  br label %24

24:                                               ; preds = %15
  %25 = load ptr, ptr %6, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.LINE, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  store ptr %27, ptr %6, align 8, !tbaa !5
  br label %11, !llvm.loop !15

28:                                               ; preds = %11
  call void (...) @kdelete()
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = call noalias ptr @malloc(i64 noundef %30) #5
  store ptr %31, ptr @kbufp, align 8, !tbaa !5
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %34

33:                                               ; preds = %28
  call void (ptr, ...) @mlwrite(ptr noundef @.str.2)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %74

34:                                               ; preds = %28
  %35 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %35, ptr @kused, align 4, !tbaa !9
  %36 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %36, ptr @ksize, align 4, !tbaa !9
  %37 = load ptr, ptr @kbufp, align 8, !tbaa !5
  store ptr %37, ptr %7, align 8, !tbaa !5
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i32 1
  store ptr %39, ptr %7, align 8, !tbaa !5
  store i8 10, ptr %38, align 1, !tbaa !17
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %40, ptr %6, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %69, %34
  %42 = load ptr, ptr %6, align 8, !tbaa !5
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = icmp ne ptr %42, %43
  br i1 %44, label %45, label %73

45:                                               ; preds = %41
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %63, %45
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.LINE, ptr %48, i32 0, i32 3
  %50 = load i16, ptr %49, align 2, !tbaa !11
  %51 = sext i16 %50 to i32
  %52 = and i32 65535, %51
  %53 = icmp slt i32 %47, %52
  br i1 %53, label %54, label %66

54:                                               ; preds = %46
  %55 = load ptr, ptr %6, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.LINE, ptr %55, i32 0, i32 4
  %57 = load i32, ptr %8, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds [32768 x i8], ptr %56, i64 0, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !17
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = getelementptr inbounds i8, ptr %61, i32 1
  store ptr %62, ptr %7, align 8, !tbaa !5
  store i8 %60, ptr %61, align 1, !tbaa !17
  br label %63

63:                                               ; preds = %54
  %64 = load i32, ptr %8, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %8, align 4, !tbaa !9
  br label %46, !llvm.loop !18

66:                                               ; preds = %46
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds i8, ptr %67, i32 1
  store ptr %68, ptr %7, align 8, !tbaa !5
  store i8 10, ptr %67, align 1, !tbaa !17
  br label %69

69:                                               ; preds = %66
  %70 = load ptr, ptr %6, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.LINE, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !14
  store ptr %72, ptr %6, align 8, !tbaa !5
  br label %41, !llvm.loop !19

73:                                               ; preds = %41
  store i32 1, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %74

74:                                               ; preds = %73, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %75 = load i32, ptr %3, align 4
  ret i32 %75
}

declare void @kdelete(...) #2

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

declare void @mlwrite(ptr noundef, ...) #2

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 = { "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 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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

!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 = !{!"int", !7, i64 0}
!11 = !{!12, !13, i64 18}
!12 = !{!"LINE", !6, i64 0, !6, i64 8, !13, i64 16, !13, i64 18, !7, i64 20}
!13 = !{!"short", !7, i64 0}
!14 = !{!12, !6, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!7, !7, i64 0}
!18 = distinct !{!18, !16}
!19 = distinct !{!19, !16}


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

@kbufp = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [18 x i8], align 1
@kused = external local_unnamed_addr global i32, align 4
@ksize = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 0, 2) i32 @ppkill(ptr noundef readonly %0, ptr noundef readnone %1) local_unnamed_addr #0 {
  %.not28 = icmp eq ptr %0, %1
  br i1 %.not28, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %.lr.ph
  %.030 = phi i32 [ %7, %.lr.ph ], [ 1, %2 ]
  %.02329 = phi ptr [ %8, %.lr.ph ], [ %0, %2 ]
  %3 = getelementptr inbounds i8, ptr %.02329, i64 18
  %4 = load i16, ptr %3, align 2, !tbaa !5
  %5 = zext i16 %4 to i32
  %6 = add nuw nsw i32 %.030, 1
  %7 = add nuw nsw i32 %6, %5
  %8 = load ptr, ptr %.02329, align 8, !tbaa !11
  %.not = icmp eq ptr %8, %1
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.0.lcssa = phi i32 [ 1, %2 ], [ %7, %.lr.ph ]
  tail call void (...) @kdelete() #3
  %9 = zext nneg i32 %.0.lcssa to i64
  %10 = tail call noalias ptr @malloc(i64 noundef %9) #4
  store ptr %10, ptr @kbufp, align 8, !tbaa !14
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %._crit_edge
  tail call void (ptr, ...) @mlwrite(ptr noundef nonnull @.str.2) #3
  br label %.loopexit

13:                                               ; preds = %._crit_edge
  store i32 %.0.lcssa, ptr @kused, align 4, !tbaa !15
  store i32 %.0.lcssa, ptr @ksize, align 4, !tbaa !15
  store i8 10, ptr %10, align 1, !tbaa !17
  br i1 %.not28, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %13, %._crit_edge35
  %.pn39 = phi ptr [ %.122.lcssa, %._crit_edge35 ], [ %10, %13 ]
  %.12438 = phi ptr [ %23, %._crit_edge35 ], [ %0, %13 ]
  %14 = getelementptr inbounds i8, ptr %.12438, i64 18
  %.12231 = getelementptr inbounds i8, ptr %.pn39, i64 1
  %15 = load i16, ptr %14, align 2, !tbaa !5
  %.not40 = icmp eq i16 %15, 0
  br i1 %.not40, label %._crit_edge35, label %.lr.ph34

.lr.ph34:                                         ; preds = %.preheader
  %16 = getelementptr inbounds i8, ptr %.12438, i64 20
  br label %17

17:                                               ; preds = %.lr.ph34, %17
  %indvars.iv = phi i64 [ 0, %.lr.ph34 ], [ %indvars.iv.next, %17 ]
  %.12233 = phi ptr [ %.12231, %.lr.ph34 ], [ %.122, %17 ]
  %18 = getelementptr inbounds [32768 x i8], ptr %16, i64 0, i64 %indvars.iv
  %19 = load i8, ptr %18, align 1, !tbaa !17
  store i8 %19, ptr %.12233, align 1, !tbaa !17
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %.122 = getelementptr inbounds i8, ptr %.12233, i64 1
  %20 = load i16, ptr %14, align 2, !tbaa !5
  %21 = zext i16 %20 to i64
  %22 = icmp ult i64 %indvars.iv.next, %21
  br i1 %22, label %17, label %._crit_edge35, !llvm.loop !18

._crit_edge35:                                    ; preds = %17, %.preheader
  %.122.lcssa = phi ptr [ %.12231, %.preheader ], [ %.122, %17 ]
  store i8 10, ptr %.122.lcssa, align 1, !tbaa !17
  %23 = load ptr, ptr %.12438, align 8, !tbaa !11
  %.not27 = icmp eq ptr %23, %1
  br i1 %.not27, label %.loopexit, label %.preheader, !llvm.loop !19

.loopexit:                                        ; preds = %._crit_edge35, %13, %12
  %.025 = phi i32 [ 0, %12 ], [ 1, %13 ], [ 1, %._crit_edge35 ]
  ret i32 %.025
}

declare void @kdelete(...) local_unnamed_addr #1

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

declare void @mlwrite(ptr noundef, ...) local_unnamed_addr #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 = { "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 = { 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

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


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass