; 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.WINDOW = type { ptr, ptr, ptr, ptr, i32, ptr, i32, i32, i32, i32, i32, i32 }

@oldbp = external global ptr, align 8
@curbp = external global ptr, align 8
@curwp = external global ptr, align 8
@wheadp = external global ptr, align 8

; 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 void @gotobuf(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %5 = load ptr, ptr @curbp, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.BUFFER, ptr %5, i32 0, i32 6
  %7 = load i32, ptr %6, align 8, !tbaa !9
  %8 = add nsw i32 %7, -1
  store i32 %8, ptr %6, align 8, !tbaa !9
  %9 = icmp eq i32 %8, 0
  br i1 %9, label %10, label %31

10:                                               ; preds = %1
  %11 = load ptr, ptr @curwp, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.WINDOW, ptr %11, i32 0, i32 3
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %14 = load ptr, ptr @curbp, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.BUFFER, ptr %14, i32 0, i32 1
  store ptr %13, ptr %15, align 8, !tbaa !14
  %16 = load ptr, ptr @curwp, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.WINDOW, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %17, align 8, !tbaa !15
  %19 = load ptr, ptr @curbp, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.BUFFER, ptr %19, i32 0, i32 2
  store i32 %18, ptr %20, align 8, !tbaa !16
  %21 = load ptr, ptr @curwp, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.WINDOW, ptr %21, i32 0, i32 5
  %23 = load ptr, ptr %22, align 8, !tbaa !17
  %24 = load ptr, ptr @curbp, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.BUFFER, ptr %24, i32 0, i32 3
  store ptr %23, ptr %25, align 8, !tbaa !18
  %26 = load ptr, ptr @curwp, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.WINDOW, ptr %26, i32 0, i32 6
  %28 = load i32, ptr %27, align 8, !tbaa !19
  %29 = load ptr, ptr @curbp, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.BUFFER, ptr %29, i32 0, i32 4
  store i32 %28, ptr %30, align 8, !tbaa !20
  br label %31

31:                                               ; preds = %10, %1
  %32 = load ptr, ptr @curbp, align 8, !tbaa !5
  store ptr %32, ptr @oldbp, align 8, !tbaa !5
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %33, ptr @curbp, align 8, !tbaa !5
  %34 = load ptr, ptr %2, align 8, !tbaa !5
  %35 = load ptr, ptr @curwp, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.WINDOW, ptr %35, i32 0, i32 1
  store ptr %34, ptr %36, align 8, !tbaa !21
  %37 = load ptr, ptr @curwp, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.WINDOW, ptr %37, i32 0, i32 11
  %39 = load i32, ptr %38, align 4, !tbaa !22
  %40 = or i32 %39, 2
  store i32 %40, ptr %38, align 4, !tbaa !22
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.BUFFER, ptr %41, i32 0, i32 6
  %43 = load i32, ptr %42, align 8, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %42, align 8, !tbaa !9
  %45 = icmp eq i32 %43, 0
  br i1 %45, label %46, label %74

46:                                               ; preds = %31
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.BUFFER, ptr %47, i32 0, i32 5
  %49 = load ptr, ptr %48, align 8, !tbaa !23
  %50 = load ptr, ptr @curwp, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.WINDOW, ptr %50, i32 0, i32 2
  store ptr %49, ptr %51, align 8, !tbaa !24
  %52 = load ptr, ptr %2, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.BUFFER, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !14
  %55 = load ptr, ptr @curwp, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.WINDOW, ptr %55, i32 0, i32 3
  store ptr %54, ptr %56, align 8, !tbaa !12
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.BUFFER, ptr %57, i32 0, i32 2
  %59 = load i32, ptr %58, align 8, !tbaa !16
  %60 = load ptr, ptr @curwp, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.WINDOW, ptr %60, i32 0, i32 4
  store i32 %59, ptr %61, align 8, !tbaa !15
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.BUFFER, ptr %62, i32 0, i32 3
  %64 = load ptr, ptr %63, align 8, !tbaa !18
  %65 = load ptr, ptr @curwp, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.WINDOW, ptr %65, i32 0, i32 5
  store ptr %64, ptr %66, align 8, !tbaa !17
  %67 = load ptr, ptr %2, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.BUFFER, ptr %67, i32 0, i32 4
  %69 = load i32, ptr %68, align 8, !tbaa !20
  %70 = load ptr, ptr @curwp, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.WINDOW, ptr %70, i32 0, i32 6
  store i32 %69, ptr %71, align 8, !tbaa !19
  %72 = load ptr, ptr @curwp, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.WINDOW, ptr %72, i32 0, i32 10
  store i32 0, ptr %73, align 8, !tbaa !25
  store i32 1, ptr %4, align 4
  br label %120

74:                                               ; preds = %31
  %75 = load ptr, ptr @wheadp, align 8, !tbaa !5
  store ptr %75, ptr %3, align 8, !tbaa !5
  br label %76

76:                                               ; preds = %115, %74
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  %78 = icmp ne ptr %77, null
  br i1 %78, label %79, label %119

79:                                               ; preds = %76
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = load ptr, ptr @curwp, align 8, !tbaa !5
  %82 = icmp ne ptr %80, %81
  br i1 %82, label %83, label %115

83:                                               ; preds = %79
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.WINDOW, ptr %84, i32 0, i32 1
  %86 = load ptr, ptr %85, align 8, !tbaa !21
  %87 = load ptr, ptr %2, align 8, !tbaa !5
  %88 = icmp eq ptr %86, %87
  br i1 %88, label %89, label %115

89:                                               ; preds = %83
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.WINDOW, ptr %90, i32 0, i32 2
  %92 = load ptr, ptr %91, align 8, !tbaa !24
  %93 = load ptr, ptr @curwp, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.WINDOW, ptr %93, i32 0, i32 2
  store ptr %92, ptr %94, align 8, !tbaa !24
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.WINDOW, ptr %95, i32 0, i32 3
  %97 = load ptr, ptr %96, align 8, !tbaa !12
  %98 = load ptr, ptr @curwp, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.WINDOW, ptr %98, i32 0, i32 3
  store ptr %97, ptr %99, align 8, !tbaa !12
  %100 = load ptr, ptr %3, align 8, !tbaa !5
  %101 = getelementptr inbounds %struct.WINDOW, ptr %100, i32 0, i32 4
  %102 = load i32, ptr %101, align 8, !tbaa !15
  %103 = load ptr, ptr @curwp, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.WINDOW, ptr %103, i32 0, i32 4
  store i32 %102, ptr %104, align 8, !tbaa !15
  %105 = load ptr, ptr %3, align 8, !tbaa !5
  %106 = getelementptr inbounds %struct.WINDOW, ptr %105, i32 0, i32 5
  %107 = load ptr, ptr %106, align 8, !tbaa !17
  %108 = load ptr, ptr @curwp, align 8, !tbaa !5
  %109 = getelementptr inbounds %struct.WINDOW, ptr %108, i32 0, i32 5
  store ptr %107, ptr %109, align 8, !tbaa !17
  %110 = load ptr, ptr %3, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.WINDOW, ptr %110, i32 0, i32 6
  %112 = load i32, ptr %111, align 8, !tbaa !19
  %113 = load ptr, ptr @curwp, align 8, !tbaa !5
  %114 = getelementptr inbounds %struct.WINDOW, ptr %113, i32 0, i32 6
  store i32 %112, ptr %114, align 8, !tbaa !19
  br label %119

115:                                              ; preds = %83, %79
  %116 = load ptr, ptr %3, align 8, !tbaa !5
  %117 = getelementptr inbounds %struct.WINDOW, ptr %116, i32 0, i32 0
  %118 = load ptr, ptr %117, align 8, !tbaa !26
  store ptr %118, ptr %3, align 8, !tbaa !5
  br label %76, !llvm.loop !27

119:                                              ; preds = %89, %76
  store i32 1, ptr %4, align 4
  br label %120

120:                                              ; preds = %119, %46
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret void
}

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 = { 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 48}
!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 = !{!13, !6, i64 24}
!13 = !{!"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}
!14 = !{!10, !6, i64 8}
!15 = !{!13, !11, i64 32}
!16 = !{!10, !11, i64 16}
!17 = !{!13, !6, i64 40}
!18 = !{!10, !6, i64 24}
!19 = !{!13, !11, i64 48}
!20 = !{!10, !11, i64 32}
!21 = !{!13, !6, i64 8}
!22 = !{!13, !11, i64 68}
!23 = !{!10, !6, i64 40}
!24 = !{!13, !6, i64 16}
!25 = !{!13, !11, i64 64}
!26 = !{!13, !6, i64 0}
!27 = distinct !{!27, !28}
!28 = !{!"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"

@oldbp = external local_unnamed_addr global ptr, align 8
@curbp = external local_unnamed_addr global ptr, align 8
@curwp = external local_unnamed_addr global ptr, align 8
@wheadp = external local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @gotobuf(ptr noundef %0) local_unnamed_addr #0 {
  %2 = load ptr, ptr @curbp, align 8, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %2, i64 48
  %4 = load i32, ptr %3, align 8, !tbaa !9
  %5 = add nsw i32 %4, -1
  store i32 %5, ptr %3, align 8, !tbaa !9
  %6 = icmp eq i32 %5, 0
  %.pre = load ptr, ptr @curwp, align 8, !tbaa !5
  br i1 %6, label %7, label %20

7:                                                ; preds = %1
  %8 = getelementptr inbounds i8, ptr %.pre, i64 24
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %9, ptr %10, align 8, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %.pre, i64 32
  %12 = load i32, ptr %11, align 8, !tbaa !15
  %13 = getelementptr inbounds i8, ptr %2, i64 16
  store i32 %12, ptr %13, align 8, !tbaa !16
  %14 = getelementptr inbounds i8, ptr %.pre, i64 40
  %15 = load ptr, ptr %14, align 8, !tbaa !17
  %16 = getelementptr inbounds i8, ptr %2, i64 24
  store ptr %15, ptr %16, align 8, !tbaa !18
  %17 = getelementptr inbounds i8, ptr %.pre, i64 48
  %18 = load i32, ptr %17, align 8, !tbaa !19
  %19 = getelementptr inbounds i8, ptr %2, i64 32
  store i32 %18, ptr %19, align 8, !tbaa !20
  br label %20

20:                                               ; preds = %7, %1
  store ptr %2, ptr @oldbp, align 8, !tbaa !5
  store ptr %0, ptr @curbp, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %.pre, i64 8
  store ptr %0, ptr %21, align 8, !tbaa !21
  %22 = getelementptr inbounds i8, ptr %.pre, i64 68
  %23 = load i32, ptr %22, align 4, !tbaa !22
  %24 = or i32 %23, 2
  store i32 %24, ptr %22, align 4, !tbaa !22
  %25 = getelementptr inbounds i8, ptr %0, i64 48
  %26 = load i32, ptr %25, align 8, !tbaa !9
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %25, align 8, !tbaa !9
  %28 = icmp eq i32 %26, 0
  br i1 %28, label %29, label %.preheader

.preheader:                                       ; preds = %20
  %.021 = load ptr, ptr @wheadp, align 8, !tbaa !5
  %.not22 = icmp eq ptr %.021, null
  br i1 %.not22, label %.loopexit, label %.lr.ph

29:                                               ; preds = %20
  %30 = getelementptr inbounds i8, ptr %0, i64 40
  %31 = load ptr, ptr %30, align 8, !tbaa !23
  %32 = getelementptr inbounds i8, ptr %.pre, i64 16
  store ptr %31, ptr %32, align 8, !tbaa !24
  %33 = getelementptr inbounds i8, ptr %0, i64 8
  %34 = load ptr, ptr %33, align 8, !tbaa !14
  %35 = getelementptr inbounds i8, ptr %.pre, i64 24
  store ptr %34, ptr %35, align 8, !tbaa !12
  %36 = getelementptr inbounds i8, ptr %0, i64 16
  %37 = load i32, ptr %36, align 8, !tbaa !16
  %38 = getelementptr inbounds i8, ptr %.pre, i64 32
  store i32 %37, ptr %38, align 8, !tbaa !15
  %39 = getelementptr inbounds i8, ptr %0, i64 24
  %40 = load ptr, ptr %39, align 8, !tbaa !18
  %41 = getelementptr inbounds i8, ptr %.pre, i64 40
  store ptr %40, ptr %41, align 8, !tbaa !17
  %42 = getelementptr inbounds i8, ptr %0, i64 32
  %43 = load i32, ptr %42, align 8, !tbaa !20
  %44 = getelementptr inbounds i8, ptr %.pre, i64 48
  store i32 %43, ptr %44, align 8, !tbaa !19
  br label %.loopexit.sink.split

.lr.ph:                                           ; preds = %.preheader, %61
  %.023 = phi ptr [ %.0, %61 ], [ %.021, %.preheader ]
  %.not19 = icmp eq ptr %.023, %.pre
  br i1 %.not19, label %61, label %45

45:                                               ; preds = %.lr.ph
  %46 = getelementptr inbounds i8, ptr %.023, i64 8
  %47 = load ptr, ptr %46, align 8, !tbaa !21
  %48 = icmp eq ptr %47, %0
  br i1 %48, label %49, label %61

49:                                               ; preds = %45
  %50 = getelementptr inbounds i8, ptr %.023, i64 16
  %51 = getelementptr inbounds i8, ptr %.pre, i64 16
  %52 = load <2 x ptr>, ptr %50, align 8, !tbaa !5
  store <2 x ptr> %52, ptr %51, align 8, !tbaa !5
  %53 = getelementptr inbounds i8, ptr %.023, i64 32
  %54 = load i32, ptr %53, align 8, !tbaa !15
  %55 = getelementptr inbounds i8, ptr %.pre, i64 32
  store i32 %54, ptr %55, align 8, !tbaa !15
  %56 = getelementptr inbounds i8, ptr %.023, i64 40
  %57 = load ptr, ptr %56, align 8, !tbaa !17
  %58 = getelementptr inbounds i8, ptr %.pre, i64 40
  store ptr %57, ptr %58, align 8, !tbaa !17
  %59 = getelementptr inbounds i8, ptr %.023, i64 48
  %60 = load i32, ptr %59, align 8, !tbaa !19
  br label %.loopexit.sink.split

61:                                               ; preds = %45, %.lr.ph
  %.0 = load ptr, ptr %.023, align 8, !tbaa !5
  %.not = icmp eq ptr %.0, null
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !25

.loopexit.sink.split:                             ; preds = %29, %49
  %.sink27 = phi i64 [ 48, %49 ], [ 64, %29 ]
  %.sink = phi i32 [ %60, %49 ], [ 0, %29 ]
  %62 = getelementptr inbounds i8, ptr %.pre, i64 %.sink27
  store i32 %.sink, ptr %62, align 8, !tbaa !27
  br label %.loopexit

.loopexit:                                        ; preds = %61, %.loopexit.sink.split, %.preheader
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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" }

!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 48}
!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 = !{!13, !6, i64 24}
!13 = !{!"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}
!14 = !{!10, !6, i64 8}
!15 = !{!13, !11, i64 32}
!16 = !{!10, !11, i64 16}
!17 = !{!13, !6, i64 40}
!18 = !{!10, !6, i64 24}
!19 = !{!13, !11, i64 48}
!20 = !{!10, !11, i64 32}
!21 = !{!13, !6, i64 8}
!22 = !{!13, !11, i64 68}
!23 = !{!10, !6, i64 40}
!24 = !{!13, !6, i64 16}
!25 = distinct !{!25, !26}
!26 = !{!"llvm.loop.mustprogress"}
!27 = !{!11, !11, i64 0}


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