; 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] }

@lmargin = external global i32, align 4
@tabsize = external global i32, align 4
@fillcol = 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 ptr @falloc(ptr noundef %0, i32 noundef %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %29

14:                                               ; preds = %2
  %15 = load i32, ptr @lmargin, align 4, !tbaa !9
  %16 = load i32, ptr @tabsize, align 4, !tbaa !9
  %17 = sdiv i32 %15, %16
  store i32 %17, ptr %9, align 4, !tbaa !9
  %18 = load i32, ptr @lmargin, align 4, !tbaa !9
  %19 = load i32, ptr @tabsize, align 4, !tbaa !9
  %20 = srem i32 %18, %19
  store i32 %20, ptr %10, align 4, !tbaa !9
  %21 = load i32, ptr @fillcol, align 4, !tbaa !9
  %22 = load i32, ptr @lmargin, align 4, !tbaa !9
  %23 = sub nsw i32 %21, %22
  %24 = load i32, ptr %9, align 4, !tbaa !9
  %25 = add nsw i32 %23, %24
  %26 = load i32, ptr %10, align 4, !tbaa !9
  %27 = add nsw i32 %25, %26
  %28 = call ptr (i32, ...) @lalloc(i32 noundef %27)
  store ptr %28, ptr %6, align 8, !tbaa !5
  br label %32

29:                                               ; preds = %2
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = call ptr (i32, ...) @lalloc(i32 noundef %30)
  store ptr %31, ptr %6, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %29, %14
  %33 = load ptr, ptr %6, align 8, !tbaa !5
  %34 = icmp eq ptr %33, null
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  store ptr null, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %92

36:                                               ; preds = %32
  %37 = load i32, ptr %5, align 4, !tbaa !9
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %39, label %71

39:                                               ; preds = %36
  %40 = load ptr, ptr %6, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.LINE, ptr %40, i32 0, i32 4
  %42 = getelementptr inbounds [32768 x i8], ptr %41, i64 0, i64 0
  store ptr %42, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %50, %39
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = load i32, ptr %9, align 4, !tbaa !9
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %47, label %53

47:                                               ; preds = %43
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds i8, ptr %48, i32 1
  store ptr %49, ptr %7, align 8, !tbaa !5
  store i8 9, ptr %48, align 1, !tbaa !11
  br label %50

50:                                               ; preds = %47
  %51 = load i32, ptr %8, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %8, align 4, !tbaa !9
  br label %43, !llvm.loop !12

53:                                               ; preds = %43
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %54

54:                                               ; preds = %61, %53
  %55 = load i32, ptr %8, align 4, !tbaa !9
  %56 = load i32, ptr %10, align 4, !tbaa !9
  %57 = icmp slt i32 %55, %56
  br i1 %57, label %58, label %64

58:                                               ; preds = %54
  %59 = load ptr, ptr %7, align 8, !tbaa !5
  %60 = getelementptr inbounds i8, ptr %59, i32 1
  store ptr %60, ptr %7, align 8, !tbaa !5
  store i8 32, ptr %59, align 1, !tbaa !11
  br label %61

61:                                               ; preds = %58
  %62 = load i32, ptr %8, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !9
  br label %54, !llvm.loop !14

64:                                               ; preds = %54
  %65 = load i32, ptr %9, align 4, !tbaa !9
  %66 = load i32, ptr %10, align 4, !tbaa !9
  %67 = add nsw i32 %65, %66
  %68 = trunc i32 %67 to i16
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.LINE, ptr %69, i32 0, i32 3
  store i16 %68, ptr %70, align 2, !tbaa !15
  br label %74

71:                                               ; preds = %36
  %72 = load ptr, ptr %6, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.LINE, ptr %72, i32 0, i32 3
  store i16 0, ptr %73, align 2, !tbaa !15
  br label %74

74:                                               ; preds = %71, %64
  %75 = load ptr, ptr %4, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.LINE, ptr %75, i32 0, i32 0
  %77 = load ptr, ptr %76, align 8, !tbaa !18
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.LINE, ptr %78, i32 0, i32 0
  store ptr %77, ptr %79, align 8, !tbaa !18
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = load ptr, ptr %6, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.LINE, ptr %81, i32 0, i32 1
  store ptr %80, ptr %82, align 8, !tbaa !19
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.LINE, ptr %84, i32 0, i32 0
  store ptr %83, ptr %85, align 8, !tbaa !18
  %86 = load ptr, ptr %6, align 8, !tbaa !5
  %87 = load ptr, ptr %6, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.LINE, ptr %87, i32 0, i32 0
  %89 = load ptr, ptr %88, align 8, !tbaa !18
  %90 = getelementptr inbounds %struct.LINE, ptr %89, i32 0, i32 1
  store ptr %86, ptr %90, align 8, !tbaa !19
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %91, ptr %3, align 8
  store i32 1, ptr %11, align 4
  br label %92

92:                                               ; preds = %74, %35
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %93 = load ptr, ptr %3, align 8
  ret ptr %93
}

declare ptr @lalloc(...) #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 }

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


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

@lmargin = external local_unnamed_addr global i32, align 4
@tabsize = external local_unnamed_addr global i32, align 4
@fillcol = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @falloc(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %4, label %.thread

4:                                                ; preds = %2
  %5 = load i32, ptr @lmargin, align 4, !tbaa !5
  %6 = load i32, ptr @tabsize, align 4, !tbaa !5
  %7 = sdiv i32 %5, %6
  %8 = srem i32 %5, %6
  %9 = load i32, ptr @fillcol, align 4, !tbaa !5
  %10 = add i32 %8, %7
  %11 = sub i32 %10, %5
  %12 = add i32 %11, %9
  %13 = tail call ptr (i32, ...) @lalloc(i32 noundef %12) #3
  %14 = icmp eq ptr %13, null
  br i1 %14, label %31, label %17

.thread:                                          ; preds = %2
  %15 = tail call ptr (i32, ...) @lalloc(i32 noundef %1) #3
  %16 = icmp eq ptr %15, null
  br i1 %16, label %31, label %.thread41

17:                                               ; preds = %4
  %18 = getelementptr i8, ptr %13, i64 20
  %19 = icmp sgt i32 %7, 0
  br i1 %19, label %.lr.ph.preheader, label %.preheader

.lr.ph.preheader:                                 ; preds = %17
  %20 = zext nneg i32 %7 to i64
  tail call void @llvm.memset.p0.i64(ptr align 1 %18, i8 9, i64 %20, i1 false), !tbaa !9
  %21 = zext nneg i32 %7 to i64
  %22 = getelementptr i8, ptr %13, i64 %21
  %scevgep = getelementptr i8, ptr %22, i64 20
  br label %.preheader

.preheader:                                       ; preds = %.lr.ph.preheader, %17
  %.030.lcssa = phi ptr [ %18, %17 ], [ %scevgep, %.lr.ph.preheader ]
  %23 = icmp sgt i32 %8, 0
  br i1 %23, label %.lr.ph51.preheader, label %._crit_edge

.lr.ph51.preheader:                               ; preds = %.preheader
  %24 = zext nneg i32 %8 to i64
  tail call void @llvm.memset.p0.i64(ptr align 1 %.030.lcssa, i8 32, i64 %24, i1 false), !tbaa !9
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph51.preheader, %.preheader
  %25 = trunc i32 %10 to i16
  br label %.thread41

.thread41:                                        ; preds = %.thread, %._crit_edge
  %.sink53 = phi ptr [ %13, %._crit_edge ], [ %15, %.thread ]
  %.sink = phi i16 [ %25, %._crit_edge ], [ 0, %.thread ]
  %26 = getelementptr inbounds i8, ptr %.sink53, i64 18
  store i16 %.sink, ptr %26, align 2, !tbaa !10
  %27 = load ptr, ptr %0, align 8, !tbaa !14
  store ptr %27, ptr %.sink53, align 8, !tbaa !14
  %28 = getelementptr inbounds i8, ptr %.sink53, i64 8
  store ptr %0, ptr %28, align 8, !tbaa !15
  store ptr %.sink53, ptr %0, align 8, !tbaa !14
  %29 = load ptr, ptr %.sink53, align 8, !tbaa !14
  %30 = getelementptr inbounds i8, ptr %29, i64 8
  store ptr %.sink53, ptr %30, align 8, !tbaa !15
  br label %31

31:                                               ; preds = %.thread, %4, %.thread41
  %.033 = phi ptr [ %.sink53, %.thread41 ], [ null, %4 ], [ null, %.thread ]
  ret ptr %.033
}

declare ptr @lalloc(...) local_unnamed_addr #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 = { "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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !13, i64 18}
!11 = !{!"LINE", !12, i64 0, !12, i64 8, !13, i64 16, !13, i64 18, !7, i64 20}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = !{!11, !12, i64 0}
!15 = !{!11, !12, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass