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

; 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.MerryMemory = type { ptr, i64, i64, ptr, ptr }
%struct.MerryMemPage = type { ptr }

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

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

12:                                               ; preds = %1
  %13 = load ptr, ptr @stderr, align 8, !tbaa !9
  %14 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef @.str.1) #5
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %99

15:                                               ; preds = %1
  %16 = load ptr, ptr %4, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.MerryMemory, ptr %16, i32 0, i32 2
  store i64 0, ptr %17, align 8, !tbaa !11
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  %19 = getelementptr inbounds %struct.MerryMemory, ptr %18, i32 0, i32 1
  store i64 0, ptr %19, align 8, !tbaa !13
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = mul i64 8, %20
  %22 = call noalias ptr @malloc(i64 noundef %21) #6
  %23 = load ptr, ptr %4, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.MerryMemory, ptr %23, i32 0, i32 0
  store ptr %22, ptr %24, align 8, !tbaa !14
  %25 = load ptr, ptr %4, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.MerryMemory, ptr %25, i32 0, i32 0
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  %28 = icmp eq ptr %27, null
  br i1 %28, label %29, label %33

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

33:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store i64 0, ptr %6, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %74, %33
  %35 = load i64, ptr %6, align 8, !tbaa !5
  %36 = load i64, ptr %3, align 8, !tbaa !5
  %37 = icmp ult i64 %35, %36
  br i1 %37, label %39, label %38

38:                                               ; preds = %34
  store i32 2, ptr %5, align 4
  br label %81

39:                                               ; preds = %34
  %40 = call ptr @merry_mem_allocate_new_mempage()
  %41 = load ptr, ptr %4, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.MerryMemory, ptr %41, i32 0, i32 0
  %43 = load ptr, ptr %42, align 8, !tbaa !14
  %44 = load i64, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds ptr, ptr %43, i64 %44
  store ptr %40, ptr %45, align 8, !tbaa !9
  %46 = load ptr, ptr %4, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.MerryMemory, ptr %46, i32 0, i32 0
  %48 = load ptr, ptr %47, align 8, !tbaa !14
  %49 = load i64, ptr %6, align 8, !tbaa !5
  %50 = getelementptr inbounds ptr, ptr %48, i64 %49
  %51 = load ptr, ptr %50, align 8, !tbaa !9
  %52 = icmp eq ptr %51, null
  br i1 %52, label %53, label %55

53:                                               ; preds = %39
  %54 = load ptr, ptr %4, align 8, !tbaa !9
  call void @merry_memory_free(ptr noundef %54)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %81

55:                                               ; preds = %39
  %56 = load ptr, ptr %4, align 8, !tbaa !9
  %57 = getelementptr inbounds %struct.MerryMemory, ptr %56, i32 0, i32 0
  %58 = load ptr, ptr %57, align 8, !tbaa !14
  %59 = load i64, ptr %6, align 8, !tbaa !5
  %60 = getelementptr inbounds ptr, ptr %58, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !9
  %62 = getelementptr inbounds %struct.MerryMemPage, ptr %61, i32 0, i32 0
  %63 = load ptr, ptr %62, align 8, !tbaa !15
  %64 = call i32 @madvise(ptr noundef %63, i64 noundef 1048576, i32 noundef 1) #5
  %65 = load ptr, ptr %4, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.MerryMemory, ptr %65, i32 0, i32 0
  %67 = load ptr, ptr %66, align 8, !tbaa !14
  %68 = load i64, ptr %6, align 8, !tbaa !5
  %69 = getelementptr inbounds ptr, ptr %67, i64 %68
  %70 = load ptr, ptr %69, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.MerryMemPage, ptr %70, i32 0, i32 0
  %72 = load ptr, ptr %71, align 8, !tbaa !15
  %73 = call i32 @madvise(ptr noundef %72, i64 noundef 1048576, i32 noundef 3) #5
  br label %74

74:                                               ; preds = %55
  %75 = load i64, ptr %6, align 8, !tbaa !5
  %76 = add i64 %75, 1
  store i64 %76, ptr %6, align 8, !tbaa !5
  %77 = load ptr, ptr %4, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.MerryMemory, ptr %77, i32 0, i32 1
  %79 = load i64, ptr %78, align 8, !tbaa !13
  %80 = add i64 %79, 1
  store i64 %80, ptr %78, align 8, !tbaa !13
  br label %34, !llvm.loop !17

81:                                               ; preds = %53, %38
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  %82 = load i32, ptr %5, align 4
  switch i32 %82, label %99 [
    i32 2, label %83
  ]

83:                                               ; preds = %81
  %84 = call ptr (...) @merry_cond_init()
  %85 = load ptr, ptr %4, align 8, !tbaa !9
  %86 = getelementptr inbounds %struct.MerryMemory, ptr %85, i32 0, i32 3
  store ptr %84, ptr %86, align 8, !tbaa !19
  %87 = icmp eq ptr %84, null
  br i1 %87, label %88, label %90

88:                                               ; preds = %83
  %89 = load ptr, ptr %4, align 8, !tbaa !9
  call void @merry_memory_free(ptr noundef %89)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %99

90:                                               ; preds = %83
  %91 = call ptr (...) @merry_mutex_init()
  %92 = load ptr, ptr %4, align 8, !tbaa !9
  %93 = getelementptr inbounds %struct.MerryMemory, ptr %92, i32 0, i32 4
  store ptr %91, ptr %93, align 8, !tbaa !20
  %94 = icmp eq ptr %91, null
  br i1 %94, label %95, label %97

95:                                               ; preds = %90
  %96 = load ptr, ptr %4, align 8, !tbaa !9
  call void @merry_memory_free(ptr noundef %96)
  store ptr null, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %99

97:                                               ; preds = %90
  %98 = load ptr, ptr %4, align 8, !tbaa !9
  store ptr %98, ptr %2, align 8
  store i32 1, ptr %5, align 4
  br label %99

99:                                               ; preds = %97, %95, %88, %81, %29, %12
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %100 = load ptr, ptr %2, align 8
  ret ptr %100
}

; 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: nounwind uwtable
declare dso_local void @merry_memory_free(ptr noundef) #0

; Function Attrs: nounwind
declare i32 @madvise(ptr noundef, i64 noundef, i32 noundef) #1

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

declare ptr @merry_cond_init(...) #4

declare ptr @merry_mutex_init(...) #4

; Function Attrs: nounwind uwtable
declare hidden ptr @merry_mem_allocate_new_mempage() #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" }
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 = { "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 #5 = { nounwind }
attributes #6 = { 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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"MerryMemory", !10, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 32}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !10, i64 0}
!15 = !{!16, !10, i64 0}
!16 = !{!"MerryMemPage", !10, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!12, !10, i64 24}
!20 = !{!12, !10, 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 [50 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [54 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [60 x i8], align 1

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

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

9:                                                ; preds = %1
  %10 = getelementptr inbounds i8, ptr %4, i64 8
  %11 = shl i64 %0, 3
  %12 = tail call noalias ptr @malloc(i64 noundef %11) #8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %10, i8 0, i64 16, i1 false)
  store ptr %12, ptr %4, align 8, !tbaa !9
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %.preheader

.preheader:                                       ; preds = %9
  %.not30.not = icmp eq i64 %0, 0
  br i1 %.not30.not, label %.critedge, label %.lr.ph

14:                                               ; preds = %9
  %15 = load ptr, ptr @stderr, align 8, !tbaa !5
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.2) #9
  tail call void @free(ptr noundef nonnull %4) #7
  br label %45

.lr.ph:                                           ; preds = %.preheader, %25
  %.031 = phi i64 [ %33, %25 ], [ 0, %.preheader ]
  %17 = tail call ptr @merry_mem_allocate_new_mempage()
  %18 = load ptr, ptr %4, align 8, !tbaa !9
  %19 = getelementptr inbounds ptr, ptr %18, i64 %.031
  store ptr %17, ptr %19, align 8, !tbaa !5
  %20 = load ptr, ptr %4, align 8, !tbaa !9
  %21 = getelementptr inbounds ptr, ptr %20, i64 %.031
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %25

24:                                               ; preds = %.lr.ph
  tail call void @merry_memory_free(ptr noundef nonnull %4)
  br label %45

25:                                               ; preds = %.lr.ph
  %26 = load ptr, ptr %22, align 8, !tbaa !12
  %27 = tail call i32 @madvise(ptr noundef %26, i64 noundef 1048576, i32 noundef 1) #7
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = getelementptr inbounds ptr, ptr %28, i64 %.031
  %30 = load ptr, ptr %29, align 8, !tbaa !5
  %31 = load ptr, ptr %30, align 8, !tbaa !12
  %32 = tail call i32 @madvise(ptr noundef %31, i64 noundef 1048576, i32 noundef 3) #7
  %33 = add nuw i64 %.031, 1
  %34 = load i64, ptr %10, align 8, !tbaa !14
  %35 = add i64 %34, 1
  store i64 %35, ptr %10, align 8, !tbaa !14
  %exitcond.not = icmp eq i64 %33, %0
  br i1 %exitcond.not, label %.critedge, label %.lr.ph, !llvm.loop !15

.critedge:                                        ; preds = %25, %.preheader
  %36 = tail call ptr (...) @merry_cond_init() #7
  %37 = getelementptr inbounds i8, ptr %4, i64 24
  store ptr %36, ptr %37, align 8, !tbaa !17
  %38 = icmp eq ptr %36, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %.critedge
  tail call void @merry_memory_free(ptr noundef nonnull %4)
  br label %45

40:                                               ; preds = %.critedge
  %41 = tail call ptr (...) @merry_mutex_init() #7
  %42 = getelementptr inbounds i8, ptr %4, i64 32
  store ptr %41, ptr %42, align 8, !tbaa !18
  %43 = icmp eq ptr %41, null
  br i1 %43, label %44, label %45

44:                                               ; preds = %40
  tail call void @merry_memory_free(ptr noundef nonnull %4)
  br label %45

45:                                               ; preds = %24, %40, %44, %39, %14, %6
  %.1 = phi ptr [ null, %6 ], [ null, %14 ], [ null, %39 ], [ null, %44 ], [ null, %24 ], [ %4, %40 ]
  ret ptr %.1
}

; 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

; Function Attrs: nounwind uwtable
declare dso_local void @merry_memory_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind
declare i32 @madvise(ptr noundef, i64 noundef, i32 noundef) local_unnamed_addr #4

declare ptr @merry_cond_init(...) local_unnamed_addr #5

declare ptr @merry_mutex_init(...) local_unnamed_addr #5

; Function Attrs: nounwind uwtable
declare hidden ptr @merry_mem_allocate_new_mempage() local_unnamed_addr #0

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #6

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 "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 #5 = { "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 #6 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #7 = { nounwind }
attributes #8 = { nounwind allocsize(0) }
attributes #9 = { 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 = !{!"MerryMemory", !6, i64 0, !11, i64 8, !11, i64 16, !6, i64 24, !6, i64 32}
!11 = !{!"long", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"MerryMemPage", !6, i64 0}
!14 = !{!10, !11, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!10, !6, i64 24}
!18 = !{!10, !6, i64 32}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass