; LLVM IR from Samson-Mano/EM_Propogation_simulation :: EM_Propogation_simulation/src/ImGui/imgui.ll (Z15ImTextStrToUtf8PciPKtS1_)

; 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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL23ImTextCharToUtf8_inlinePcij(ptr noundef, i32 noundef, i32 noundef) #1

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

17:                                               ; preds = %61, %4
  %18 = load ptr, ptr %9, align 8, !tbaa !5
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %19, i64 -1
  %21 = icmp ult ptr %18, %20
  br i1 %21, label %22, label %33

22:                                               ; preds = %17
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %29

25:                                               ; preds = %22
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = icmp ult ptr %26, %27
  br i1 %28, label %29, label %33

29:                                               ; preds = %25, %22
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = load i16, ptr %30, align 2, !tbaa !11
  %32 = icmp ne i16 %31, 0
  br label %33

33:                                               ; preds = %29, %25, %17
  %34 = phi i1 [ false, %25 ], [ false, %17 ], [ %32, %29 ]
  br i1 %34, label %35, label %62

35:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds i16, ptr %36, i32 1
  store ptr %37, ptr %7, align 8, !tbaa !5
  %38 = load i16, ptr %36, align 2, !tbaa !11
  %39 = zext i16 %38 to i32
  store i32 %39, ptr %11, align 4, !tbaa !9
  %40 = load i32, ptr %11, align 4, !tbaa !9
  %41 = icmp ult i32 %40, 128
  br i1 %41, label %42, label %47

42:                                               ; preds = %35
  %43 = load i32, ptr %11, align 4, !tbaa !9
  %44 = trunc i32 %43 to i8
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %45, i32 1
  store ptr %46, ptr %9, align 8, !tbaa !5
  store i8 %44, ptr %45, align 1, !tbaa !13
  br label %61

47:                                               ; preds = %35
  %48 = load ptr, ptr %9, align 8, !tbaa !5
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = load ptr, ptr %9, align 8, !tbaa !5
  %51 = ptrtoint ptr %49 to i64
  %52 = ptrtoint ptr %50 to i64
  %53 = sub i64 %51, %52
  %54 = sub nsw i64 %53, 1
  %55 = trunc i64 %54 to i32
  %56 = load i32, ptr %11, align 4, !tbaa !9
  %57 = call noundef i32 @_ZL23ImTextCharToUtf8_inlinePcij(ptr noundef %48, i32 noundef %55, i32 noundef %56)
  %58 = load ptr, ptr %9, align 8, !tbaa !5
  %59 = sext i32 %57 to i64
  %60 = getelementptr inbounds i8, ptr %58, i64 %59
  store ptr %60, ptr %9, align 8, !tbaa !5
  br label %61

61:                                               ; preds = %47, %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %17, !llvm.loop !14

62:                                               ; preds = %33
  %63 = load ptr, ptr %9, align 8, !tbaa !5
  store i8 0, ptr %63, align 1, !tbaa !13
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = ptrtoint ptr %64 to i64
  %67 = ptrtoint ptr %65 to i64
  %68 = sub i64 %66, %67
  %69 = trunc i64 %68 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret i32 %69
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint mustprogress 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 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"short", !7, i64 0}
!13 = !{!7, !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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i32 @_ZL23ImTextCharToUtf8_inlinePcij(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z15ImTextStrToUtf8PciPKtS1_(ptr noundef %0, i32 noundef %1, ptr noundef readonly %2, ptr noundef readnone %3) local_unnamed_addr #1 {
  %5 = sext i32 %1 to i64
  %6 = getelementptr inbounds i8, ptr %0, i64 %5
  %7 = getelementptr inbounds i8, ptr %6, i64 -1
  %8 = icmp ugt ptr %7, %0
  br i1 %8, label %.lr.ph, label %.critedge

.lr.ph:                                           ; preds = %4
  %.not = icmp eq ptr %3, null
  %9 = ptrtoint ptr %6 to i64
  br i1 %.not, label %.lr.ph.split.us, label %.lr.ph.split

.lr.ph.split.us:                                  ; preds = %.lr.ph, %26
  %.027.us = phi ptr [ %12, %26 ], [ %2, %.lr.ph ]
  %.02026.us = phi ptr [ %.1.us, %26 ], [ %0, %.lr.ph ]
  %10 = load i16, ptr %.027.us, align 2, !tbaa !5
  %.not25.us = icmp eq i16 %10, 0
  br i1 %.not25.us, label %.critedge, label %11

11:                                               ; preds = %.lr.ph.split.us
  %12 = getelementptr inbounds i8, ptr %.027.us, i64 2
  %13 = icmp ult i16 %10, 128
  br i1 %13, label %23, label %14

14:                                               ; preds = %11
  %15 = zext i16 %10 to i32
  %16 = ptrtoint ptr %.02026.us to i64
  %17 = xor i64 %16, -1
  %18 = add i64 %17, %9
  %19 = trunc i64 %18 to i32
  %20 = tail call noundef i32 @_ZL23ImTextCharToUtf8_inlinePcij(ptr noundef %.02026.us, i32 noundef %19, i32 noundef %15)
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %.02026.us, i64 %21
  br label %26

23:                                               ; preds = %11
  %24 = trunc nuw i16 %10 to i8
  %25 = getelementptr inbounds i8, ptr %.02026.us, i64 1
  store i8 %24, ptr %.02026.us, align 1, !tbaa !9
  br label %26

26:                                               ; preds = %23, %14
  %.1.us = phi ptr [ %25, %23 ], [ %22, %14 ]
  %27 = icmp ult ptr %.1.us, %7
  br i1 %27, label %.lr.ph.split.us, label %.critedge, !llvm.loop !10

.lr.ph.split:                                     ; preds = %.lr.ph, %46
  %.027 = phi ptr [ %32, %46 ], [ %2, %.lr.ph ]
  %.02026 = phi ptr [ %.1, %46 ], [ %0, %.lr.ph ]
  %28 = icmp ult ptr %.027, %3
  br i1 %28, label %29, label %.critedge

29:                                               ; preds = %.lr.ph.split
  %30 = load i16, ptr %.027, align 2, !tbaa !5
  %.not25 = icmp eq i16 %30, 0
  br i1 %.not25, label %.critedge, label %31

31:                                               ; preds = %29
  %32 = getelementptr inbounds i8, ptr %.027, i64 2
  %33 = icmp ult i16 %30, 128
  br i1 %33, label %34, label %37

34:                                               ; preds = %31
  %35 = trunc nuw i16 %30 to i8
  %36 = getelementptr inbounds i8, ptr %.02026, i64 1
  store i8 %35, ptr %.02026, align 1, !tbaa !9
  br label %46

37:                                               ; preds = %31
  %38 = zext i16 %30 to i32
  %39 = ptrtoint ptr %.02026 to i64
  %40 = xor i64 %39, -1
  %41 = add i64 %40, %9
  %42 = trunc i64 %41 to i32
  %43 = tail call noundef i32 @_ZL23ImTextCharToUtf8_inlinePcij(ptr noundef %.02026, i32 noundef %42, i32 noundef %38)
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %.02026, i64 %44
  br label %46

46:                                               ; preds = %37, %34
  %.1 = phi ptr [ %36, %34 ], [ %45, %37 ]
  %47 = icmp ult ptr %.1, %7
  br i1 %47, label %.lr.ph.split, label %.critedge, !llvm.loop !10

.critedge:                                        ; preds = %29, %46, %.lr.ph.split, %.lr.ph.split.us, %26, %4
  %.020.lcssa = phi ptr [ %0, %4 ], [ %.1.us, %26 ], [ %.02026.us, %.lr.ph.split.us ], [ %.02026, %.lr.ph.split ], [ %.1, %46 ], [ %.02026, %29 ]
  store i8 0, ptr %.020.lcssa, align 1, !tbaa !9
  %48 = ptrtoint ptr %.020.lcssa to i64
  %49 = ptrtoint ptr %0 to i64
  %50 = sub i64 %48, %49
  %51 = trunc i64 %50 to i32
  ret i32 %51
}

attributes #0 = { inlinehint mustprogress 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 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" }

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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