; LLVM IR from jingweizhanghuai/Morn :: src/util (morn_list)

; 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.HandleStack = type { i32 }
%struct.MList = type { i32, ptr }

@.str = external hidden unnamed_addr constant [76 x i8], align 1
@__FUNCTION__.mStackWrite = external hidden unnamed_addr constant [12 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [14 x i8], align 1

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

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @mListWrite(ptr noundef, i32 noundef, ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @endStack(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @mStackWrite(ptr noundef %0, ptr noundef %1, i32 noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i32 %2, ptr %7, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = icmp eq ptr %13, null
  br i1 %14, label %19, label %15

15:                                               ; preds = %12
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = ptrtoint ptr %16 to i64
  %18 = icmp eq i64 %17, -1
  br label %19

19:                                               ; preds = %15, %12
  %20 = phi i1 [ true, %12 ], [ %18, %15 ]
  %21 = zext i1 %20 to i32
  store i32 %21, ptr %8, align 4, !tbaa !9
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %26

24:                                               ; preds = %19
  %25 = load i32, ptr %8, align 4, !tbaa !9
  call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %25, i32 noundef -1, ptr noundef @.str, i32 noundef 634, ptr noundef @__FUNCTION__.mStackWrite, ptr noundef @.str.8)
  br label %26

26:                                               ; preds = %24, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %27

27:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = call ptr @GetHandle(ptr noundef %28, i32 noundef 4, i32 noundef -1941091213, ptr noundef @endStack)
  store ptr %29, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.MHandle, ptr %30, i32 0, i32 3
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  store ptr %32, ptr %10, align 8, !tbaa !5
  %33 = load ptr, ptr %9, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.MHandle, ptr %33, i32 0, i32 1
  %35 = load volatile i32, ptr %34, align 4, !tbaa !13
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %40

37:                                               ; preds = %27
  %38 = load ptr, ptr %10, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.HandleStack, ptr %38, i32 0, i32 0
  store volatile i32 -1, ptr %39, align 4, !tbaa !14
  br label %40

40:                                               ; preds = %37, %27
  %41 = load ptr, ptr %9, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.MHandle, ptr %41, i32 0, i32 1
  store volatile i32 1, ptr %42, align 4, !tbaa !13
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.HandleStack, ptr %43, i32 0, i32 0
  %45 = load volatile i32, ptr %44, align 4, !tbaa !14
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.MList, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 8, !tbaa !16
  %49 = sub nsw i32 %48, 1
  %50 = icmp eq i32 %45, %49
  br i1 %50, label %51, label %52

51:                                               ; preds = %40
  store ptr null, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %64

52:                                               ; preds = %40
  %53 = load ptr, ptr %10, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.HandleStack, ptr %53, i32 0, i32 0
  %55 = atomicrmw add ptr %54, i32 1 seq_cst, align 4
  %56 = add i32 %55, 1
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.HandleStack, ptr %58, i32 0, i32 0
  %60 = load volatile i32, ptr %59, align 4, !tbaa !14
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = call ptr @mListWrite(ptr noundef %57, i32 noundef %60, ptr noundef %61, i32 noundef %62)
  store ptr %63, ptr %4, align 8
  store i32 1, ptr %11, align 4
  br label %64

64:                                               ; preds = %52, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %65 = load ptr, ptr %4, align 8
  ret ptr %65
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 16}
!12 = !{!"MHandle", !10, i64 0, !10, i64 4, !6, i64 8, !6, i64 16}
!13 = !{!12, !10, i64 4}
!14 = !{!15, !10, i64 0}
!15 = !{!"HandleStack", !10, i64 0}
!16 = !{!17, !10, i64 0}
!17 = !{!"MList", !10, i64 0, !6, i64 8}


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

@.str = external hidden unnamed_addr constant [76 x i8], align 1
@__FUNCTION__.mStackWrite = external hidden unnamed_addr constant [12 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [14 x i8], align 1

declare void @m_Exception(i32 noundef, i32 noundef, ptr noundef, i32 noundef, ptr noundef, ptr noundef, ...) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @mListWrite(ptr noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @endStack(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @mStackWrite(ptr noundef %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #1 {
  %magicptr = ptrtoint ptr %0 to i64
  switch i64 %magicptr, label %9 [
    i64 -1, label %4
    i64 0, label %4
  ]

4:                                                ; preds = %3, %3
  %5 = icmp eq ptr %0, null
  %6 = icmp eq ptr %0, inttoptr (i64 -1 to ptr)
  %7 = or i1 %5, %6
  %8 = zext i1 %7 to i32
  tail call void (i32, i32, ptr, i32, ptr, ptr, ...) @m_Exception(i32 noundef %8, i32 noundef -1, ptr noundef nonnull @.str, i32 noundef 634, ptr noundef nonnull @__FUNCTION__.mStackWrite, ptr noundef nonnull @.str.8) #2
  br label %9

9:                                                ; preds = %3, %4
  %10 = tail call ptr @GetHandle(ptr noundef %0, i32 noundef 4, i32 noundef -1941091213, ptr noundef nonnull @endStack) #2
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %12 = load ptr, ptr %11, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %10, i64 4
  %14 = load volatile i32, ptr %13, align 4, !tbaa !11
  %15 = icmp eq i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %9
  store volatile i32 -1, ptr %12, align 4, !tbaa !12
  br label %17

17:                                               ; preds = %16, %9
  store volatile i32 1, ptr %13, align 4, !tbaa !11
  %18 = load volatile i32, ptr %12, align 4, !tbaa !12
  %19 = load i32, ptr %0, align 8, !tbaa !14
  %20 = add nsw i32 %19, -1
  %21 = icmp eq i32 %18, %20
  br i1 %21, label %26, label %22

22:                                               ; preds = %17
  %23 = atomicrmw add ptr %12, i32 1 seq_cst, align 4
  %24 = load volatile i32, ptr %12, align 4, !tbaa !12
  %25 = tail call ptr @mListWrite(ptr noundef nonnull %0, i32 noundef %24, ptr noundef %1, i32 noundef %2)
  br label %26

26:                                               ; preds = %17, %22
  %.0 = phi ptr [ %25, %22 ], [ null, %17 ]
  ret ptr %.0
}

attributes #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 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 = { 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, !10, i64 16}
!6 = !{!"MHandle", !7, i64 0, !7, i64 4, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!13, !7, i64 0}
!13 = !{!"HandleStack", !7, i64 0}
!14 = !{!15, !7, i64 0}
!15 = !{!"MList", !7, i64 0, !10, i64 8}


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