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

; 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.BUFFER = type { ptr, ptr, i32, ptr, i32, ptr, i32, i32, [128 x i8], [128 x i8] }
%struct.LINE = type { ptr, ptr, i16, i16, [32768 x i8] }
%struct.WINDOW = type { ptr, ptr, ptr, ptr, i32, ptr, i32, i32, i32, i32, i32, i32 }

@wheadp = external global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @bbclear(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.BUFFER, ptr %5, i32 0, i32 7
  %7 = load i32, ptr %6, align 4, !tbaa !9
  %8 = and i32 %7, -3
  store i32 %8, ptr %6, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %19, %1
  %10 = load ptr, ptr %2, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.BUFFER, ptr %10, i32 0, i32 5
  %12 = load ptr, ptr %11, align 8, !tbaa !12
  %13 = getelementptr inbounds %struct.LINE, ptr %12, i32 0, i32 0
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  store ptr %14, ptr %4, align 8, !tbaa !5
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.BUFFER, ptr %15, i32 0, i32 5
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = icmp ne ptr %14, %17
  br i1 %18, label %19, label %21

19:                                               ; preds = %9
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  call void @lfree(ptr noundef %20)
  br label %9, !llvm.loop !16

21:                                               ; preds = %9
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.BUFFER, ptr %22, i32 0, i32 5
  %24 = load ptr, ptr %23, align 8, !tbaa !12
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.BUFFER, ptr %25, i32 0, i32 1
  store ptr %24, ptr %26, align 8, !tbaa !18
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.BUFFER, ptr %27, i32 0, i32 2
  store i32 0, ptr %28, align 8, !tbaa !19
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.BUFFER, ptr %29, i32 0, i32 3
  store ptr null, ptr %30, align 8, !tbaa !20
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.BUFFER, ptr %31, i32 0, i32 4
  store i32 0, ptr %32, align 8, !tbaa !21
  %33 = load ptr, ptr @wheadp, align 8, !tbaa !5
  store ptr %33, ptr %3, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %61, %21
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %65

37:                                               ; preds = %34
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.WINDOW, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !22
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = icmp eq ptr %40, %41
  br i1 %42, label %43, label %60

43:                                               ; preds = %37
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.BUFFER, ptr %44, i32 0, i32 5
  %46 = load ptr, ptr %45, align 8, !tbaa !12
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.WINDOW, ptr %47, i32 0, i32 2
  store ptr %46, ptr %48, align 8, !tbaa !24
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.BUFFER, ptr %49, i32 0, i32 5
  %51 = load ptr, ptr %50, align 8, !tbaa !12
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.WINDOW, ptr %52, i32 0, i32 3
  store ptr %51, ptr %53, align 8, !tbaa !25
  %54 = load ptr, ptr %3, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.WINDOW, ptr %54, i32 0, i32 4
  store i32 0, ptr %55, align 8, !tbaa !26
  %56 = load ptr, ptr %3, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.WINDOW, ptr %56, i32 0, i32 11
  %58 = load i32, ptr %57, align 4, !tbaa !27
  %59 = or i32 %58, 25
  store i32 %59, ptr %57, align 4, !tbaa !27
  br label %60

60:                                               ; preds = %43, %37
  br label %61

61:                                               ; preds = %60
  %62 = load ptr, ptr %3, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.WINDOW, ptr %62, i32 0, i32 0
  %64 = load ptr, ptr %63, align 8, !tbaa !28
  store ptr %64, ptr %3, align 8, !tbaa !5
  br label %34, !llvm.loop !29

65:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare void @lfree(ptr noundef) #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 = { 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, !11, i64 52}
!10 = !{!"BUFFER", !6, i64 0, !6, i64 8, !11, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !7, i64 56, !7, i64 184}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 40}
!13 = !{!14, !6, i64 0}
!14 = !{!"LINE", !6, i64 0, !6, i64 8, !15, i64 16, !15, i64 18, !7, i64 20}
!15 = !{!"short", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!10, !6, i64 8}
!19 = !{!10, !11, i64 16}
!20 = !{!10, !6, i64 24}
!21 = !{!10, !11, i64 32}
!22 = !{!23, !6, i64 8}
!23 = !{!"WINDOW", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !11, i64 32, !6, i64 40, !11, i64 48, !11, i64 52, !11, i64 56, !11, i64 60, !11, i64 64, !11, i64 68}
!24 = !{!23, !6, i64 16}
!25 = !{!23, !6, i64 24}
!26 = !{!23, !11, i64 32}
!27 = !{!23, !11, i64 68}
!28 = !{!23, !6, i64 0}
!29 = distinct !{!29, !17}


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

@wheadp = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @bbclear(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 52
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = and i32 %3, -3
  store i32 %4, ptr %2, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 40
  %6 = load ptr, ptr %5, align 8, !tbaa !11
  %7 = load ptr, ptr %6, align 8, !tbaa !12
  %.not22 = icmp eq ptr %7, %6
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %.lr.ph
  %8 = phi ptr [ %10, %.lr.ph ], [ %7, %1 ]
  tail call void @lfree(ptr noundef %8) #2
  %9 = load ptr, ptr %5, align 8, !tbaa !11
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %.not = icmp eq ptr %10, %9
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !15

._crit_edge:                                      ; preds = %.lr.ph, %1
  %.lcssa = phi ptr [ %6, %1 ], [ %9, %.lr.ph ]
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  store ptr %.lcssa, ptr %11, align 8, !tbaa !17
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  store i32 0, ptr %12, align 8, !tbaa !18
  %13 = getelementptr inbounds i8, ptr %0, i64 24
  store ptr null, ptr %13, align 8, !tbaa !19
  %14 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 0, ptr %14, align 8, !tbaa !20
  %.023 = load ptr, ptr @wheadp, align 8, !tbaa !21
  %.not2124 = icmp eq ptr %.023, null
  br i1 %.not2124, label %._crit_edge28, label %.lr.ph27

.lr.ph27:                                         ; preds = %._crit_edge, %25
  %.025 = phi ptr [ %.0, %25 ], [ %.023, %._crit_edge ]
  %15 = getelementptr inbounds i8, ptr %.025, i64 8
  %16 = load ptr, ptr %15, align 8, !tbaa !22
  %17 = icmp eq ptr %16, %0
  br i1 %17, label %18, label %25

18:                                               ; preds = %.lr.ph27
  %19 = getelementptr inbounds i8, ptr %.025, i64 16
  store ptr %.lcssa, ptr %19, align 8, !tbaa !24
  %20 = getelementptr inbounds i8, ptr %.025, i64 24
  store ptr %.lcssa, ptr %20, align 8, !tbaa !25
  %21 = getelementptr inbounds i8, ptr %.025, i64 32
  store i32 0, ptr %21, align 8, !tbaa !26
  %22 = getelementptr inbounds i8, ptr %.025, i64 68
  %23 = load i32, ptr %22, align 4, !tbaa !27
  %24 = or i32 %23, 25
  store i32 %24, ptr %22, align 4, !tbaa !27
  br label %25

25:                                               ; preds = %.lr.ph27, %18
  %.0 = load ptr, ptr %.025, align 8, !tbaa !21
  %.not21 = icmp eq ptr %.0, null
  br i1 %.not21, label %._crit_edge28, label %.lr.ph27, !llvm.loop !28

._crit_edge28:                                    ; preds = %25, %._crit_edge
  ret void
}

declare void @lfree(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 = { 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 52}
!6 = !{!"BUFFER", !7, i64 0, !7, i64 8, !10, i64 16, !7, i64 24, !10, i64 32, !7, i64 40, !10, i64 48, !10, i64 52, !8, i64 56, !8, i64 184}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !7, i64 40}
!12 = !{!13, !7, i64 0}
!13 = !{!"LINE", !7, i64 0, !7, i64 8, !14, i64 16, !14, i64 18, !8, i64 20}
!14 = !{!"short", !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!6, !7, i64 8}
!18 = !{!6, !10, i64 16}
!19 = !{!6, !7, i64 24}
!20 = !{!6, !10, i64 32}
!21 = !{!7, !7, i64 0}
!22 = !{!23, !7, i64 8}
!23 = !{!"WINDOW", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !7, i64 40, !10, i64 48, !10, i64 52, !10, i64 56, !10, i64 60, !10, i64 64, !10, i64 68}
!24 = !{!23, !7, i64 16}
!25 = !{!23, !7, i64 24}
!26 = !{!23, !10, i64 32}
!27 = !{!23, !10, i64 68}
!28 = distinct !{!28, !16}


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