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

@stdout = external global ptr, align 8
@.str = external hidden unnamed_addr constant [34 x i8], align 1
@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [47 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local ptr @merry_init_stack(i64 noundef %0, i8 noundef zeroext %1, i64 noundef %2, i64 noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i64 %0, ptr %6, align 8, !tbaa !5
  store i8 %1, ptr %7, align 1, !tbaa !9
  store i64 %2, ptr %8, align 8, !tbaa !5
  store i64 %3, ptr %9, align 8, !tbaa !5
  %12 = load ptr, ptr @stdout, align 8, !tbaa !10
  %13 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef @.str, ptr noundef null) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %14 = call noalias ptr @malloc(i64 noundef 48) #5
  store ptr %14, ptr %10, align 8, !tbaa !10
  %15 = load ptr, ptr %10, align 8, !tbaa !10
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %20

17:                                               ; preds = %4
  %18 = load ptr, ptr @stderr, align 8, !tbaa !10
  %19 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef @.str.1) #4
  store ptr null, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %56

20:                                               ; preds = %4
  %21 = load i64, ptr %6, align 8, !tbaa !5
  %22 = mul i64 8, %21
  %23 = call noalias ptr @malloc(i64 noundef %22) #5
  %24 = load ptr, ptr %10, align 8, !tbaa !10
  %25 = getelementptr inbounds %struct.MerryStack, ptr %24, i32 0, i32 0
  store ptr %23, ptr %25, align 8, !tbaa !12
  %26 = load ptr, ptr %10, align 8, !tbaa !10
  %27 = getelementptr inbounds %struct.MerryStack, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !12
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %34

30:                                               ; preds = %20
  %31 = load ptr, ptr %10, align 8, !tbaa !10
  call void @free(ptr noundef %31) #4
  %32 = load ptr, ptr @stderr, align 8, !tbaa !10
  %33 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %32, ptr noundef @.str.2) #4
  store ptr null, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %56

34:                                               ; preds = %20
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = load ptr, ptr %10, align 8, !tbaa !10
  %37 = getelementptr inbounds %struct.MerryStack, ptr %36, i32 0, i32 1
  store i64 %35, ptr %37, align 8, !tbaa !14
  %38 = load ptr, ptr %10, align 8, !tbaa !10
  %39 = getelementptr inbounds %struct.MerryStack, ptr %38, i32 0, i32 2
  store i64 -1, ptr %39, align 8, !tbaa !15
  %40 = load i8, ptr %7, align 1, !tbaa !9
  %41 = load ptr, ptr %10, align 8, !tbaa !10
  %42 = getelementptr inbounds %struct.MerryStack, ptr %41, i32 0, i32 3
  store i8 %40, ptr %42, align 8, !tbaa !16
  %43 = load i64, ptr %9, align 8, !tbaa !5
  %44 = load ptr, ptr %10, align 8, !tbaa !10
  %45 = getelementptr inbounds %struct.MerryStack, ptr %44, i32 0, i32 5
  store i64 %43, ptr %45, align 8, !tbaa !17
  %46 = load i64, ptr %8, align 8, !tbaa !5
  %47 = icmp eq i64 %46, 0
  br i1 %47, label %48, label %49

48:                                               ; preds = %34
  br label %51

49:                                               ; preds = %34
  %50 = load i64, ptr %8, align 8, !tbaa !5
  br label %51

51:                                               ; preds = %49, %48
  %52 = phi i64 [ 1000, %48 ], [ %50, %49 ]
  %53 = load ptr, ptr %10, align 8, !tbaa !10
  %54 = getelementptr inbounds %struct.MerryStack, ptr %53, i32 0, i32 4
  store i64 %52, ptr %54, align 8, !tbaa !18
  %55 = load ptr, ptr %10, align 8, !tbaa !10
  store ptr %55, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %56

56:                                               ; preds = %51, %30, %17
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %57 = load ptr, ptr %5, align 8
  ret ptr %57
}

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

; Function Attrs: nounwind
declare void @free(ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 = { nounwind "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 nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }

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


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

@stdout = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [34 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [47 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [54 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local noalias noundef ptr @merry_init_stack(i64 noundef %0, i8 noundef zeroext %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #0 {
  %5 = load ptr, ptr @stdout, align 8, !tbaa !5
  %6 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str, ptr noundef null) #4
  %7 = tail call noalias dereferenceable_or_null(48) ptr @malloc(i64 noundef 48) #5
  %8 = icmp eq ptr %7, null
  br i1 %8, label %9, label %12

9:                                                ; preds = %4
  %10 = load ptr, ptr @stderr, align 8, !tbaa !5
  %11 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %10, ptr noundef nonnull @.str.1) #6
  br label %27

12:                                               ; preds = %4
  %13 = shl i64 %0, 3
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #5
  store ptr %14, ptr %7, align 8, !tbaa !9
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %19

16:                                               ; preds = %12
  tail call void @free(ptr noundef nonnull %7) #4
  %17 = load ptr, ptr @stderr, align 8, !tbaa !5
  %18 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef nonnull @.str.2) #6
  br label %27

19:                                               ; preds = %12
  %20 = getelementptr inbounds i8, ptr %7, i64 8
  store i64 %0, ptr %20, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %7, i64 16
  store i64 -1, ptr %21, align 8, !tbaa !13
  %22 = getelementptr inbounds i8, ptr %7, i64 24
  store i8 %1, ptr %22, align 8, !tbaa !14
  %23 = getelementptr inbounds i8, ptr %7, i64 40
  store i64 %3, ptr %23, align 8, !tbaa !15
  %24 = icmp eq i64 %2, 0
  %25 = select i1 %24, i64 1000, i64 %2
  %26 = getelementptr inbounds i8, ptr %7, i64 32
  store i64 %25, ptr %26, align 8, !tbaa !16
  br label %27

27:                                               ; preds = %19, %16, %9
  %.0 = phi ptr [ null, %9 ], [ null, %16 ], [ %7, %19 ]
  ret ptr %.0
}

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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 = { nofree nounwind "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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { cold 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 0}
!10 = !{!"MerryStack", !6, i64 0, !11, i64 8, !11, i64 16, !7, i64 24, !11, i64 32, !11, i64 40}
!11 = !{!"long", !7, i64 0}
!12 = !{!10, !11, i64 8}
!13 = !{!10, !11, i64 16}
!14 = !{!10, !7, i64 24}
!15 = !{!10, !11, i64 40}
!16 = !{!10, !11, i64 32}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass