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

@curwp = 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
declare dso_local i32 @inword(i32 noundef) #1

declare i32 @forwchar(i32 noundef, i32 noundef) #2

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

12:                                               ; preds = %2
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

13:                                               ; preds = %2
  %14 = load ptr, ptr @curwp, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.WINDOW, ptr %14, i32 0, i32 3
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  store ptr %16, ptr %7, align 8, !tbaa !9
  %17 = load ptr, ptr @curwp, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.WINDOW, ptr %17, i32 0, i32 4
  %19 = load i32, ptr %18, align 8, !tbaa !13
  store i32 %19, ptr %8, align 4, !tbaa !5
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %46, %13
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %21, -1
  store i32 %22, ptr %5, align 4, !tbaa !5
  %23 = icmp ne i32 %21, 0
  br i1 %23, label %24, label %47

24:                                               ; preds = %20
  br label %25

25:                                               ; preds = %32, %24
  %26 = call i32 @inword(i32 noundef 0)
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %35

28:                                               ; preds = %25
  %29 = call i32 @forwchar(i32 noundef 0, i32 noundef 1)
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %28
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

32:                                               ; preds = %28
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %6, align 4, !tbaa !5
  br label %25, !llvm.loop !14

35:                                               ; preds = %25
  br label %36

36:                                               ; preds = %43, %35
  %37 = call i32 @inword(i32 noundef 0)
  %38 = icmp ne i32 %37, 0
  br i1 %38, label %39, label %46

39:                                               ; preds = %36
  %40 = call i32 @forwchar(i32 noundef 0, i32 noundef 1)
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %43

42:                                               ; preds = %39
  store i32 0, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

43:                                               ; preds = %39
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %6, align 4, !tbaa !5
  br label %36, !llvm.loop !16

46:                                               ; preds = %36
  br label %20, !llvm.loop !17

47:                                               ; preds = %20
  %48 = load ptr, ptr %7, align 8, !tbaa !9
  %49 = load ptr, ptr @curwp, align 8, !tbaa !9
  %50 = getelementptr inbounds %struct.WINDOW, ptr %49, i32 0, i32 3
  store ptr %48, ptr %50, align 8, !tbaa !11
  %51 = load i32, ptr %8, align 4, !tbaa !5
  %52 = load ptr, ptr @curwp, align 8, !tbaa !9
  %53 = getelementptr inbounds %struct.WINDOW, ptr %52, i32 0, i32 4
  store i32 %51, ptr %53, align 8, !tbaa !13
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = call i32 @ldelete(i32 noundef %54, i32 noundef 1)
  store i32 %55, ptr %3, align 4
  store i32 1, ptr %9, align 4
  br label %56

56:                                               ; preds = %47, %42, %31, %12
  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 4, ptr %6) #3
  %57 = load i32, ptr %3, align 4
  ret i32 %57
}

declare i32 @ldelete(i32 noundef, i32 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 }

!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, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !10, i64 24}
!12 = !{!"WINDOW", !10, i64 0, !10, i64 8, !10, i64 16, !10, i64 24, !6, i64 32, !10, i64 40, !6, i64 48, !6, i64 52, !6, i64 56, !6, i64 60, !6, i64 64, !6, i64 68}
!13 = !{!12, !6, i64 32}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


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

@curwp = external local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local i32 @inword(i32 noundef) local_unnamed_addr #0

declare i32 @forwchar(i32 noundef, i32 noundef) local_unnamed_addr #1

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

4:                                                ; preds = %2
  %5 = load ptr, ptr @curwp, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %5, i64 24
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = getelementptr inbounds i8, ptr %5, i64 32
  %9 = load i32, ptr %8, align 8, !tbaa !12
  %.not23 = icmp eq i32 %1, 0
  br i1 %.not23, label %._crit_edge, label %.preheader12

.loopexit:                                        ; preds = %22, %.preheader
  %.2.lcssa = phi i32 [ %.1.lcssa, %.preheader ], [ %23, %22 ]
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %._crit_edge.loopexit, label %.preheader12, !llvm.loop !13

.preheader12:                                     ; preds = %4, %.loopexit
  %.in = phi i32 [ %10, %.loopexit ], [ %1, %4 ]
  %.0824 = phi i32 [ %.2.lcssa, %.loopexit ], [ 0, %4 ]
  %10 = add nsw i32 %.in, -1
  %11 = tail call i32 @inword(i32 noundef 0)
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %16, %.preheader12
  %.1.lcssa = phi i32 [ %.0824, %.preheader12 ], [ %17, %16 ]
  %13 = tail call i32 @inword(i32 noundef 0)
  %.not1019 = icmp eq i32 %13, 0
  br i1 %.not1019, label %.loopexit, label %.lr.ph21

.lr.ph:                                           ; preds = %.preheader12, %16
  %.118 = phi i32 [ %17, %16 ], [ %.0824, %.preheader12 ]
  %14 = tail call i32 @forwchar(i32 noundef 0, i32 noundef 1) #2
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %.loopexit11, label %16

16:                                               ; preds = %.lr.ph
  %17 = add nsw i32 %.118, 1
  %18 = tail call i32 @inword(i32 noundef 0)
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %.lr.ph, label %.preheader, !llvm.loop !15

.lr.ph21:                                         ; preds = %.preheader, %22
  %.220 = phi i32 [ %23, %22 ], [ %.1.lcssa, %.preheader ]
  %20 = tail call i32 @forwchar(i32 noundef 0, i32 noundef 1) #2
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %.loopexit11, label %22

22:                                               ; preds = %.lr.ph21
  %23 = add nsw i32 %.220, 1
  %24 = tail call i32 @inword(i32 noundef 0)
  %.not10 = icmp eq i32 %24, 0
  br i1 %.not10, label %.loopexit, label %.lr.ph21, !llvm.loop !16

._crit_edge.loopexit:                             ; preds = %.loopexit
  %.pre = load ptr, ptr @curwp, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %4
  %25 = phi ptr [ %5, %4 ], [ %.pre, %._crit_edge.loopexit ]
  %.08.lcssa = phi i32 [ 0, %4 ], [ %.2.lcssa, %._crit_edge.loopexit ]
  %26 = getelementptr inbounds i8, ptr %25, i64 24
  store ptr %7, ptr %26, align 8, !tbaa !9
  %27 = getelementptr inbounds i8, ptr %25, i64 32
  store i32 %9, ptr %27, align 8, !tbaa !12
  %28 = tail call i32 @ldelete(i32 noundef %.08.lcssa, i32 noundef 1) #2
  br label %.loopexit11

.loopexit11:                                      ; preds = %.lr.ph, %.lr.ph21, %2, %._crit_edge
  %.0 = phi i32 [ %28, %._crit_edge ], [ 0, %2 ], [ 0, %.lr.ph21 ], [ 0, %.lr.ph ]
  ret i32 %.0
}

declare i32 @ldelete(i32 noundef, i32 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 24}
!10 = !{!"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}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 32}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


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