; LLVM IR from jingweizhanghuai/Morn :: src/util (morn_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.MHandle = type { i32, i32, ptr, ptr }
%struct.HandleMemoryAlloc = type { ptr, [256 x i32] }

@morn_memory_handle = external dso_local global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [39 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @m_Malloc(i32 noundef) #0

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @MemoryAlloc(i32 noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  store ptr %3, ptr %8, align 8, !tbaa !9
  %14 = load ptr, ptr @morn_memory_handle, align 8, !tbaa !9
  %15 = icmp eq ptr %14, null
  br i1 %15, label %16, label %39

16:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %17 = call ptr @mMornObject(ptr noundef @.str.3, i32 noundef -1)
  %18 = call ptr @GetHandle(ptr noundef %17, i32 noundef 1032, i32 noundef -326495099, ptr noundef @endMemoryAlloc)
  store ptr %18, ptr %9, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %19 = load ptr, ptr %9, align 8, !tbaa !9
  %20 = getelementptr inbounds %struct.MHandle, ptr %19, i32 0, i32 3
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  store ptr %21, ptr %10, align 8, !tbaa !9
  %22 = load ptr, ptr %9, align 8, !tbaa !9
  %23 = call i32 @mHandleValid(ptr noundef %22)
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %38, label %25

25:                                               ; preds = %16
  %26 = load ptr, ptr %10, align 8, !tbaa !9
  %27 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %26, i32 0, i32 0
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %34

30:                                               ; preds = %25
  %31 = call ptr (...) @mMapCreate()
  %32 = load ptr, ptr %10, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %32, i32 0, i32 0
  store ptr %31, ptr %33, align 8, !tbaa !13
  br label %34

34:                                               ; preds = %30, %25
  %35 = load ptr, ptr %10, align 8, !tbaa !9
  store ptr %35, ptr @morn_memory_handle, align 8, !tbaa !9
  %36 = load ptr, ptr %9, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.MHandle, ptr %36, i32 0, i32 1
  store volatile i32 1, ptr %37, align 4, !tbaa !15
  br label %38

38:                                               ; preds = %34, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  br label %39

39:                                               ; preds = %38, %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %40 = load ptr, ptr @morn_memory_handle, align 8, !tbaa !9
  store ptr %40, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  %41 = load ptr, ptr %11, align 8, !tbaa !9
  %42 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %41, i32 0, i32 1
  %43 = getelementptr inbounds [256 x i32], ptr %42, i64 0, i64 0
  %44 = getelementptr inbounds i32, ptr %43, i64 2
  %45 = load ptr, ptr %6, align 8, !tbaa !9
  %46 = load i32, ptr %7, align 4, !tbaa !5
  %47 = load ptr, ptr %8, align 8, !tbaa !9
  %48 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef %44, ptr noundef @.str.4, ptr noundef %45, i32 noundef %46, ptr noundef %47) #4
  store i32 %48, ptr %12, align 4, !tbaa !5
  %49 = load i32, ptr %5, align 4, !tbaa !5
  %50 = add nsw i32 %49, 7
  %51 = sdiv i32 %50, 8
  store i32 %51, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %52 = load i32, ptr %5, align 4, !tbaa !5
  %53 = mul nsw i32 %52, 8
  %54 = sext i32 %53 to i64
  %55 = add i64 %54, 16
  %56 = trunc i64 %55 to i32
  %57 = call ptr @m_Malloc(i32 noundef %56)
  %58 = getelementptr inbounds i8, ptr %57, i64 1
  store ptr %58, ptr %13, align 8, !tbaa !9
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = load ptr, ptr %13, align 8, !tbaa !9
  %62 = getelementptr inbounds i64, ptr %61, i64 -1
  store i64 %60, ptr %62, align 8, !tbaa !16
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = load ptr, ptr %13, align 8, !tbaa !9
  %66 = load i32, ptr %5, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i64, ptr %65, i64 %67
  store i64 %64, ptr %68, align 8, !tbaa !16
  %69 = load i32, ptr %5, align 4, !tbaa !5
  %70 = load ptr, ptr %11, align 8, !tbaa !9
  %71 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %70, i32 0, i32 1
  %72 = getelementptr inbounds [256 x i32], ptr %71, i64 0, i64 0
  store i32 %69, ptr %72, align 8, !tbaa !5
  %73 = load i32, ptr %12, align 4, !tbaa !5
  %74 = load ptr, ptr %11, align 8, !tbaa !9
  %75 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %74, i32 0, i32 1
  %76 = getelementptr inbounds [256 x i32], ptr %75, i64 0, i64 1
  store i32 %73, ptr %76, align 4, !tbaa !5
  %77 = load ptr, ptr %11, align 8, !tbaa !9
  %78 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %77, i32 0, i32 0
  %79 = load ptr, ptr %78, align 8, !tbaa !13
  %80 = load ptr, ptr %11, align 8, !tbaa !9
  %81 = getelementptr inbounds %struct.HandleMemoryAlloc, ptr %80, i32 0, i32 1
  %82 = getelementptr inbounds [256 x i32], ptr %81, i64 0, i64 0
  %83 = load i32, ptr %12, align 4, !tbaa !5
  %84 = sext i32 %83 to i64
  %85 = add i64 12, %84
  %86 = load i32, ptr %12, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = add i64 %85, %87
  %89 = add i64 %88, 32
  %90 = trunc i64 %89 to i32
  %91 = call ptr @m_MapWrite(ptr noundef %79, ptr noundef %13, i32 noundef 8, ptr noundef %82, i32 noundef %90)
  %92 = load ptr, ptr %13, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  ret ptr %92
}

declare ptr @GetHandle(ptr noundef, i32 noundef, i32 noundef, ptr noundef) #2

declare ptr @mMornObject(ptr noundef, i32 noundef) #2

declare i32 @mHandleValid(ptr noundef) #2

declare ptr @mMapCreate(...) #2

; Function Attrs: nounwind
declare i32 @sprintf(ptr noundef, ptr noundef, ...) #3

declare ptr @m_MapWrite(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 "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 }

!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 16}
!12 = !{!"MHandle", !6, i64 0, !6, i64 4, !10, i64 8, !10, i64 16}
!13 = !{!14, !10, i64 0}
!14 = !{!"HandleMemoryAlloc", !10, i64 0, !7, i64 8}
!15 = !{!12, !6, i64 4}
!16 = !{!17, !17, i64 0}
!17 = !{!"long", !7, 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"

@morn_memory_handle = external dso_local local_unnamed_addr global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [39 x i8], align 1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @MemoryAlloc(i32 noundef %0, ptr noundef %1, i32 noundef %2, ptr noundef %3) local_unnamed_addr #0 {
  %5 = alloca ptr, align 8
  %6 = load ptr, ptr @morn_memory_handle, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %21

8:                                                ; preds = %4
  %9 = tail call ptr @mMornObject(ptr noundef nonnull @.str.3, i32 noundef -1) #4
  %10 = tail call ptr @GetHandle(ptr noundef %9, i32 noundef 1032, i32 noundef -326495099, ptr noundef nonnull @endMemoryAlloc) #4
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = tail call i32 @mHandleValid(ptr noundef %10) #4
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %14, label %._crit_edge

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

14:                                               ; preds = %8
  %15 = load ptr, ptr %12, align 8, !tbaa !12
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %19

17:                                               ; preds = %14
  %18 = tail call ptr (...) @mMapCreate() #4
  store ptr %18, ptr %12, align 8, !tbaa !12
  br label %19

19:                                               ; preds = %17, %14
  store ptr %12, ptr @morn_memory_handle, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %10, i64 4
  store volatile i32 1, ptr %20, align 4, !tbaa !14
  br label %21

21:                                               ; preds = %._crit_edge, %19, %4
  %22 = phi ptr [ %.pre, %._crit_edge ], [ %12, %19 ], [ %6, %4 ]
  %23 = getelementptr inbounds i8, ptr %22, i64 8
  %24 = getelementptr inbounds i8, ptr %22, i64 16
  %25 = tail call i32 (ptr, ptr, ...) @sprintf(ptr noundef nonnull dereferenceable(1) %24, ptr noundef nonnull dereferenceable(1) @.str.4, ptr noundef %1, i32 noundef %2, ptr noundef %3) #4
  %26 = add nsw i32 %0, 7
  %27 = sdiv i32 %26, 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #4
  %28 = shl nsw i32 %27, 3
  %29 = add i32 %28, 16
  %30 = tail call ptr @m_Malloc(i32 noundef %29)
  %31 = getelementptr inbounds i8, ptr %30, i64 1
  store ptr %31, ptr %5, align 8, !tbaa !5
  %32 = sext i32 %27 to i64
  %33 = getelementptr inbounds i8, ptr %30, i64 -7
  store i64 %32, ptr %33, align 8, !tbaa !15
  %34 = getelementptr inbounds i64, ptr %31, i64 %32
  store i64 %32, ptr %34, align 8, !tbaa !15
  store i32 %27, ptr %23, align 8, !tbaa !17
  %35 = getelementptr inbounds i8, ptr %22, i64 12
  store i32 %25, ptr %35, align 4, !tbaa !17
  %36 = load ptr, ptr %22, align 8, !tbaa !12
  %reass.add = shl i32 %25, 1
  %37 = add i32 %reass.add, 44
  %38 = call ptr @m_MapWrite(ptr noundef %36, ptr noundef nonnull %5, i32 noundef 8, ptr noundef nonnull %23, i32 noundef %37) #4
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #4
  ret ptr %39
}

declare ptr @GetHandle(ptr noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

declare ptr @mMornObject(ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @mHandleValid(ptr noundef) local_unnamed_addr #2

declare ptr @mMapCreate(...) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @sprintf(ptr noalias nocapture noundef writeonly, ptr nocapture noundef readonly, ...) local_unnamed_addr #3

declare ptr @m_MapWrite(ptr noundef, ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #4 = { 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 16}
!10 = !{!"MHandle", !11, i64 0, !11, i64 4, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"HandleMemoryAlloc", !6, i64 0, !7, i64 8}
!14 = !{!10, !11, i64 4}
!15 = !{!16, !16, i64 0}
!16 = !{!"long", !7, i64 0}
!17 = !{!11, !11, i64 0}


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