; LLVM IR from l1mey112/yr11-physics-applied :: cimgui/imgui/imgui.ll (Z17ImTextStrFromUtf8PtiPKcS1_PS1_)

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

; 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: mustprogress uwtable
declare dso_local noundef i32 @_Z18ImTextCharFromUtf8PjPKcS1_(ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z17ImTextStrFromUtf8PtiPKcS1_PS1_(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #1 {
  %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 ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %15, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #2
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i16, ptr %16, i64 %18
  store ptr %19, ptr %12, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %55, %5
  %21 = load ptr, ptr %11, align 8, !tbaa !5
  %22 = load ptr, ptr %12, align 8, !tbaa !5
  %23 = getelementptr inbounds i16, ptr %22, i64 -1
  %24 = icmp ult ptr %21, %23
  br i1 %24, label %25, label %36

25:                                               ; preds = %20
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = icmp ne ptr %26, null
  br i1 %27, label %28, label %32

28:                                               ; preds = %25
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = icmp ult ptr %29, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %28, %25
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = load i8, ptr %33, align 1, !tbaa !11
  %35 = icmp ne i8 %34, 0
  br label %36

36:                                               ; preds = %32, %28, %20
  %37 = phi i1 [ false, %28 ], [ false, %20 ], [ %35, %32 ]
  br i1 %37, label %38, label %56

38:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = load ptr, ptr %9, align 8, !tbaa !5
  %41 = call noundef i32 @_Z18ImTextCharFromUtf8PjPKcS1_(ptr noundef %13, ptr noundef %39, ptr noundef %40)
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = sext i32 %41 to i64
  %44 = getelementptr inbounds i8, ptr %42, i64 %43
  store ptr %44, ptr %8, align 8, !tbaa !5
  %45 = load i32, ptr %13, align 4, !tbaa !9
  %46 = icmp eq i32 %45, 0
  br i1 %46, label %47, label %48

47:                                               ; preds = %38
  store i32 3, ptr %14, align 4
  br label %53

48:                                               ; preds = %38
  %49 = load i32, ptr %13, align 4, !tbaa !9
  %50 = trunc i32 %49 to i16
  %51 = load ptr, ptr %11, align 8, !tbaa !5
  %52 = getelementptr inbounds i16, ptr %51, i32 1
  store ptr %52, ptr %11, align 8, !tbaa !5
  store i16 %50, ptr %51, align 2, !tbaa !12
  store i32 0, ptr %14, align 4
  br label %53

53:                                               ; preds = %48, %47
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  %54 = load i32, ptr %14, align 4
  switch i32 %54, label %71 [
    i32 0, label %55
    i32 3, label %56
  ]

55:                                               ; preds = %53
  br label %20, !llvm.loop !14

56:                                               ; preds = %53, %36
  %57 = load ptr, ptr %11, align 8, !tbaa !5
  store i16 0, ptr %57, align 2, !tbaa !12
  %58 = load ptr, ptr %10, align 8, !tbaa !5
  %59 = icmp ne ptr %58, null
  br i1 %59, label %60, label %63

60:                                               ; preds = %56
  %61 = load ptr, ptr %8, align 8, !tbaa !5
  %62 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %61, ptr %62, align 8, !tbaa !5
  br label %63

63:                                               ; preds = %60, %56
  %64 = load ptr, ptr %11, align 8, !tbaa !5
  %65 = load ptr, ptr %6, align 8, !tbaa !5
  %66 = ptrtoint ptr %64 to i64
  %67 = ptrtoint ptr %65 to i64
  %68 = sub i64 %66, %67
  %69 = sdiv exact i64 %68, 2
  %70 = trunc i64 %69 to i32
  store i32 1, ptr %14, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  ret i32 %70

71:                                               ; preds = %53
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"short", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

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

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

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z17ImTextStrFromUtf8PtiPKcS1_PS1_(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef writeonly %4) local_unnamed_addr #1 {
  %6 = alloca i32, align 4
  %7 = sext i32 %1 to i64
  %8 = getelementptr inbounds i16, ptr %0, i64 %7
  %9 = getelementptr inbounds i8, ptr %8, i64 -2
  %10 = icmp ugt ptr %9, %0
  br i1 %10, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %18
  %.01934.us = phi ptr [ %15, %18 ], [ %2, %.lr.ph ]
  %.02033.us = phi ptr [ %20, %18 ], [ %0, %.lr.ph ]
  %11 = load i8, ptr %.01934.us, align 1, !tbaa !5
  %.not25.us = icmp eq i8 %11, 0
  br i1 %.not25.us, label %.critedge, label %12

12:                                               ; preds = %.lr.ph.split.us
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #2
  %13 = call noundef i32 @_Z18ImTextCharFromUtf8PjPKcS1_(ptr noundef nonnull %6, ptr noundef nonnull %.01934.us, ptr noundef null)
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds i8, ptr %.01934.us, i64 %14
  %16 = load i32, ptr %6, align 4, !tbaa !8
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %.thread, label %18

18:                                               ; preds = %12
  %19 = trunc i32 %16 to i16
  %20 = getelementptr inbounds i8, ptr %.02033.us, i64 2
  store i16 %19, ptr %.02033.us, align 2, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  %21 = icmp ult ptr %20, %9
  br i1 %21, label %.lr.ph.split.us, label %.critedge

.lr.ph.split:                                     ; preds = %.lr.ph, %31
  %.01934 = phi ptr [ %28, %31 ], [ %2, %.lr.ph ]
  %.02033 = phi ptr [ %33, %31 ], [ %0, %.lr.ph ]
  %22 = icmp ult ptr %.01934, %3
  br i1 %22, label %23, label %.critedge

23:                                               ; preds = %.lr.ph.split
  %24 = load i8, ptr %.01934, align 1, !tbaa !5
  %.not25 = icmp eq i8 %24, 0
  br i1 %.not25, label %.critedge, label %25

25:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #2
  %26 = call noundef i32 @_Z18ImTextCharFromUtf8PjPKcS1_(ptr noundef nonnull %6, ptr noundef nonnull %.01934, ptr noundef nonnull %3)
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %.01934, i64 %27
  %29 = load i32, ptr %6, align 4, !tbaa !8
  %30 = icmp eq i32 %29, 0
  br i1 %30, label %.thread, label %31

.thread:                                          ; preds = %25, %12
  %.us-phi43 = phi ptr [ %15, %12 ], [ %28, %25 ]
  %.us-phi44 = phi ptr [ %.02033.us, %12 ], [ %.02033, %25 ]
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  br label %.critedge

31:                                               ; preds = %25
  %32 = trunc i32 %29 to i16
  %33 = getelementptr inbounds i8, ptr %.02033, i64 2
  store i16 %32, ptr %.02033, align 2, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #2
  %34 = icmp ult ptr %33, %9
  br i1 %34, label %.lr.ph.split, label %.critedge

.critedge:                                        ; preds = %23, %31, %.lr.ph.split, %.lr.ph.split.us, %18, %5, %.thread
  %.02032 = phi ptr [ %.us-phi44, %.thread ], [ %0, %5 ], [ %.02033.us, %.lr.ph.split.us ], [ %20, %18 ], [ %.02033, %23 ], [ %33, %31 ], [ %.02033, %.lr.ph.split ]
  %.1 = phi ptr [ %.us-phi43, %.thread ], [ %2, %5 ], [ %.01934.us, %.lr.ph.split.us ], [ %15, %18 ], [ %.01934, %23 ], [ %28, %31 ], [ %.01934, %.lr.ph.split ]
  store i16 0, ptr %.02032, align 2, !tbaa !10
  %.not26 = icmp eq ptr %4, null
  br i1 %.not26, label %36, label %35

35:                                               ; preds = %.critedge
  store ptr %.1, ptr %4, align 8, !tbaa !12
  br label %36

36:                                               ; preds = %35, %.critedge
  %37 = ptrtoint ptr %.02032 to i64
  %38 = ptrtoint ptr %0 to i64
  %39 = sub i64 %37, %38
  %40 = lshr exact i64 %39, 1
  %41 = trunc i64 %40 to i32
  ret i32 %41
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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, !6, i64 0}
!6 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"any pointer", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass