; LLVM IR from Aeternum-Veritas/Merry :: merry (merry_stack)

; 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.MerryStack = type { ptr, i64, i64, i8, i64, i64 }

; Function Attrs: nounwind uwtable
define dso_local zeroext i8 @merry_stack_push(ptr noundef %0, i64 noundef %1) #0 {
  %3 = alloca i8, align 1
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.MerryStack, ptr %6, i32 0, i32 2
  %8 = load i64, ptr %7, align 8, !tbaa !11
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.MerryStack, ptr %9, i32 0, i32 1
  %11 = load i64, ptr %10, align 8, !tbaa !13
  %12 = sub i64 %11, 1
  %13 = icmp eq i64 %8, %12
  br i1 %13, label %14, label %28

14:                                               ; preds = %2
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.MerryStack, ptr %15, i32 0, i32 3
  %17 = load i8, ptr %16, align 8, !tbaa !14
  %18 = zext i8 %17 to i32
  %19 = icmp ne i32 %18, 1
  br i1 %19, label %20, label %21

20:                                               ; preds = %14
  store i8 1, ptr %3, align 1
  br label %41

21:                                               ; preds = %14
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = call zeroext i8 @merry_stack_resize(ptr noundef %22)
  %24 = zext i8 %23 to i32
  %25 = icmp eq i32 %24, 1
  br i1 %25, label %26, label %27

26:                                               ; preds = %21
  store i8 1, ptr %3, align 1
  br label %41

27:                                               ; preds = %21
  br label %28

28:                                               ; preds = %27, %2
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.MerryStack, ptr %29, i32 0, i32 2
  %31 = load i64, ptr %30, align 8, !tbaa !11
  %32 = add i64 %31, 1
  store i64 %32, ptr %30, align 8, !tbaa !11
  %33 = load i64, ptr %5, align 8, !tbaa !9
  %34 = load ptr, ptr %4, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.MerryStack, ptr %34, i32 0, i32 0
  %36 = load ptr, ptr %35, align 8, !tbaa !15
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.MerryStack, ptr %37, i32 0, i32 2
  %39 = load i64, ptr %38, align 8, !tbaa !11
  %40 = getelementptr inbounds i64, ptr %36, i64 %39
  store i64 %33, ptr %40, align 8, !tbaa !9
  store i8 0, ptr %3, align 1
  br label %41

41:                                               ; preds = %28, %26, %20
  %42 = load i8, ptr %3, align 1
  ret i8 %42
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @merry_stack_resize(ptr noundef) #0

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

!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 = !{!"long", !7, i64 0}
!11 = !{!12, !10, i64 16}
!12 = !{!"MerryStack", !6, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !10, i64 32, !10, i64 40}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !7, i64 24}
!15 = !{!12, !6, i64 0}


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

; Function Attrs: nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @merry_stack_push(ptr noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !11
  %7 = add i64 %6, -1
  %8 = icmp eq i64 %4, %7
  br i1 %8, label %9, label %15

9:                                                ; preds = %2
  %10 = getelementptr inbounds i8, ptr %0, i64 24
  %11 = load i8, ptr %10, align 8, !tbaa !12
  %.not = icmp eq i8 %11, 1
  br i1 %.not, label %12, label %20

12:                                               ; preds = %9
  %13 = tail call zeroext i8 @merry_stack_resize(ptr noundef nonnull %0)
  %14 = icmp eq i8 %13, 1
  br i1 %14, label %20, label %._crit_edge

._crit_edge:                                      ; preds = %12
  %.pre = load i64, ptr %3, align 8, !tbaa !5
  br label %15

15:                                               ; preds = %._crit_edge, %2
  %16 = phi i64 [ %.pre, %._crit_edge ], [ %4, %2 ]
  %17 = add i64 %16, 1
  store i64 %17, ptr %3, align 8, !tbaa !5
  %18 = load ptr, ptr %0, align 8, !tbaa !13
  %19 = getelementptr inbounds i64, ptr %18, i64 %17
  store i64 %1, ptr %19, align 8, !tbaa !14
  br label %20

20:                                               ; preds = %12, %9, %15
  %.0 = phi i8 [ 0, %15 ], [ 1, %9 ], [ 1, %12 ]
  ret i8 %.0
}

; Function Attrs: nounwind uwtable
declare hidden zeroext i8 @merry_stack_resize(ptr noundef) local_unnamed_addr #0

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

!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 16}
!6 = !{!"MerryStack", !7, i64 0, !10, i64 8, !10, i64 16, !8, i64 24, !10, i64 32, !10, i64 40}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!6, !8, i64 24}
!13 = !{!6, !7, i64 0}
!14 = !{!10, !10, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass