; LLVM IR from jingweizhanghuai/Morn :: src/util/morn_string.ll (DefaultString)

; 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.HandleDefaultString = type { [128 x ptr] }
%struct.MArray = type { i32, i16, %union.anon }
%union.anon = type { ptr }

@morn_default_string = external dso_local thread_local global ptr, align 8
@.str = external hidden unnamed_addr constant [5 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

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

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

declare ptr @m_ArrayWrite(ptr noundef, i64 noundef, ptr noundef, i32 noundef) #3

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #4

; Function Attrs: nounwind uwtable
define dso_local ptr @_DefaultString(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  %8 = load ptr, ptr %7, align 8, !tbaa !5
  %9 = icmp eq ptr %8, null
  br i1 %9, label %10, label %49

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %11 = call ptr @mMornObject(ptr noundef @.str, i32 noundef -1)
  %12 = call ptr @GetHandle(ptr noundef %11, i32 noundef 1024, i32 noundef -1317563621, ptr noundef @endDefaultString)
  store ptr %12, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.MHandle, ptr %13, i32 0, i32 3
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = call i32 @mHandleValid(ptr noundef %16)
  %18 = icmp ne i32 %17, 0
  br i1 %18, label %48, label %19

19:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %20 = call i32 (...) @mThreadOrder()
  store i32 %20, ptr %5, align 4, !tbaa !12
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.HandleDefaultString, ptr %21, i32 0, i32 0
  %23 = load i32, ptr %5, align 4, !tbaa !12
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds [128 x ptr], ptr %22, i64 0, i64 %24
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %35

28:                                               ; preds = %19
  %29 = call ptr @m_StringCreate(ptr noundef null, i32 noundef 1024)
  %30 = load ptr, ptr %4, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.HandleDefaultString, ptr %30, i32 0, i32 0
  %32 = load i32, ptr %5, align 4, !tbaa !12
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds [128 x ptr], ptr %31, i64 0, i64 %33
  store ptr %29, ptr %34, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %28, %19
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.HandleDefaultString, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %5, align 4, !tbaa !12
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [128 x ptr], ptr %37, i64 0, i64 %39
  %41 = load ptr, ptr %40, align 8, !tbaa !5
  %42 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  store ptr %41, ptr %42, align 8, !tbaa !5
  %43 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  %44 = load ptr, ptr %43, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.MArray, ptr %44, i32 0, i32 0
  store i32 0, ptr %45, align 8, !tbaa !13
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.MHandle, ptr %46, i32 0, i32 1
  store volatile i32 1, ptr %47, align 4, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %48

48:                                               ; preds = %35, %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  br label %49

49:                                               ; preds = %48, %1
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = icmp ne ptr %50, null
  br i1 %51, label %52, label %66

52:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #5
  %53 = load ptr, ptr %2, align 8, !tbaa !5
  %54 = call i64 @strlen(ptr noundef %53) #6
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %6, align 4, !tbaa !12
  %56 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  %57 = load ptr, ptr %56, align 8, !tbaa !5
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  %59 = load i32, ptr %6, align 4, !tbaa !12
  %60 = add nsw i32 %59, 1
  %61 = call ptr @m_ArrayWrite(ptr noundef %57, i64 noundef 0, ptr noundef %58, i32 noundef %60)
  %62 = load i32, ptr %6, align 4, !tbaa !12
  %63 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  %64 = load ptr, ptr %63, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.MArray, ptr %64, i32 0, i32 0
  store i32 %62, ptr %65, align 8, !tbaa !13
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  br label %66

66:                                               ; preds = %52, %49
  %67 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  %68 = load ptr, ptr %67, align 8, !tbaa !5
  ret ptr %68
}

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

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

declare i32 @mHandleValid(ptr noundef) #3

declare i32 @mThreadOrder(...) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 willreturn memory(read) "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 = { "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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #5 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 = !{!11, !11, i64 0}
!13 = !{!14, !11, i64 0}
!14 = !{!"MArray", !11, i64 0, !15, i64 4, !7, i64 8}
!15 = !{!"short", !7, i64 0}
!16 = !{!10, !11, i64 4}


; 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_default_string = external dso_local thread_local local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [5 x i8], align 1

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #1

declare ptr @m_ArrayWrite(ptr noundef, i64 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

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

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #3

; Function Attrs: nounwind uwtable
define dso_local ptr @_DefaultString(ptr noundef %0) local_unnamed_addr #0 {
  %2 = tail call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @morn_default_string)
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %5, label %22

5:                                                ; preds = %1
  %6 = tail call ptr @mMornObject(ptr noundef nonnull @.str, i32 noundef -1) #4
  %7 = tail call ptr @GetHandle(ptr noundef %6, i32 noundef 1024, i32 noundef -1317563621, ptr noundef nonnull @endDefaultString) #4
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !9
  %10 = tail call i32 @mHandleValid(ptr noundef %7) #4
  %.not = icmp eq i32 %10, 0
  br i1 %.not, label %11, label %._crit_edge

._crit_edge:                                      ; preds = %5
  %.pre.pre = load ptr, ptr %2, align 8, !tbaa !5
  br label %22

11:                                               ; preds = %5
  %12 = tail call i32 (...) @mThreadOrder() #4
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [128 x ptr], ptr %9, i64 0, i64 %13
  %15 = load ptr, ptr %14, align 8, !tbaa !5
  %16 = icmp eq ptr %15, null
  br i1 %16, label %17, label %19

17:                                               ; preds = %11
  %18 = tail call ptr @m_StringCreate(ptr noundef null, i32 noundef 1024)
  store ptr %18, ptr %14, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %17, %11
  %20 = phi ptr [ %18, %17 ], [ %15, %11 ]
  store ptr %20, ptr %2, align 8, !tbaa !5
  store i32 0, ptr %20, align 8, !tbaa !12
  %21 = getelementptr inbounds i8, ptr %7, i64 4
  store volatile i32 1, ptr %21, align 4, !tbaa !15
  br label %22

22:                                               ; preds = %._crit_edge, %19, %1
  %.pre = phi ptr [ %.pre.pre, %._crit_edge ], [ %20, %19 ], [ %3, %1 ]
  %.not13 = icmp eq ptr %0, null
  br i1 %.not13, label %29, label %23

23:                                               ; preds = %22
  %24 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #5
  %25 = trunc i64 %24 to i32
  %26 = add nsw i32 %25, 1
  %27 = tail call ptr @m_ArrayWrite(ptr noundef %.pre, i64 noundef 0, ptr noundef nonnull %0, i32 noundef %26) #4
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 %25, ptr %28, align 8, !tbaa !12
  br label %29

29:                                               ; preds = %23, %22
  %30 = phi ptr [ %28, %23 ], [ %.pre, %22 ]
  ret ptr %30
}

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 i32 @mThreadOrder(...) 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 nofree nounwind willreturn memory(argmem: read) "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 = { "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 nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }
attributes #5 = { nounwind willreturn memory(read) }

!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, !11, i64 0}
!13 = !{!"MArray", !11, i64 0, !14, i64 4, !7, i64 8}
!14 = !{!"short", !7, i64 0}
!15 = !{!10, !11, i64 4}


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