; LLVM IR from wrnlb666/yet-another-string-library-in-c :: yasli (str_utf8_char_at)

; 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.string_t = type { i64, i64, [1 x i8] }

@str_utf8_char_at.buf = external hidden global [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: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @str_utf8_char_at(ptr noundef %0, i64 noundef %1) #3 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  call void @llvm.memset.p0.i64(ptr align 1 @str_utf8_char_at.buf, i8 0, i64 5, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 0, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.string_t, ptr %9, i32 0, i32 2
  %11 = getelementptr inbounds [1 x i8], ptr %10, i64 0, i64 0
  store ptr %11, ptr %7, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %63, %2
  %13 = load i64, ptr %6, align 8, !tbaa !9
  %14 = load i64, ptr %5, align 8, !tbaa !9
  %15 = icmp ult i64 %13, %14
  br i1 %15, label %16, label %66

16:                                               ; preds = %12
  %17 = load ptr, ptr %7, align 8, !tbaa !5
  %18 = load i8, ptr %17, align 1, !tbaa !11
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %16
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

22:                                               ; preds = %16
  %23 = load ptr, ptr %7, align 8, !tbaa !5
  %24 = load i8, ptr %23, align 1, !tbaa !11
  %25 = zext i8 %24 to i32
  %26 = and i32 %25, 128
  %27 = icmp eq i32 %26, 0
  br i1 %27, label %28, label %31

28:                                               ; preds = %22
  %29 = load ptr, ptr %7, align 8, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %29, i32 1
  store ptr %30, ptr %7, align 8, !tbaa !5
  br label %62

31:                                               ; preds = %22
  %32 = load ptr, ptr %7, align 8, !tbaa !5
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = and i32 %34, 224
  %36 = icmp eq i32 %35, 192
  br i1 %36, label %37, label %40

37:                                               ; preds = %31
  %38 = load ptr, ptr %7, align 8, !tbaa !5
  %39 = getelementptr inbounds i8, ptr %38, i64 2
  store ptr %39, ptr %7, align 8, !tbaa !5
  br label %61

40:                                               ; preds = %31
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = zext i8 %42 to i32
  %44 = and i32 %43, 240
  %45 = icmp eq i32 %44, 224
  br i1 %45, label %46, label %49

46:                                               ; preds = %40
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 3
  store ptr %48, ptr %7, align 8, !tbaa !5
  br label %60

49:                                               ; preds = %40
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = load i8, ptr %50, align 1, !tbaa !11
  %52 = zext i8 %51 to i32
  %53 = and i32 %52, 248
  %54 = icmp eq i32 %53, 240
  br i1 %54, label %55, label %58

55:                                               ; preds = %49
  %56 = load ptr, ptr %7, align 8, !tbaa !5
  %57 = getelementptr inbounds i8, ptr %56, i64 4
  store ptr %57, ptr %7, align 8, !tbaa !5
  br label %59

58:                                               ; preds = %49
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

59:                                               ; preds = %55
  br label %60

60:                                               ; preds = %59, %46
  br label %61

61:                                               ; preds = %60, %37
  br label %62

62:                                               ; preds = %61, %28
  br label %63

63:                                               ; preds = %62
  %64 = load i64, ptr %6, align 8, !tbaa !9
  %65 = add i64 %64, 1
  store i64 %65, ptr %6, align 8, !tbaa !9
  br label %12, !llvm.loop !12

66:                                               ; preds = %12
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = load i8, ptr %67, align 1, !tbaa !11
  %69 = zext i8 %68 to i32
  %70 = icmp eq i32 %69, 0
  br i1 %70, label %71, label %72

71:                                               ; preds = %66
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

72:                                               ; preds = %66
  %73 = load ptr, ptr %7, align 8, !tbaa !5
  %74 = load i8, ptr %73, align 1, !tbaa !11
  %75 = zext i8 %74 to i32
  %76 = and i32 %75, 128
  %77 = icmp eq i32 %76, 0
  br i1 %77, label %78, label %80

78:                                               ; preds = %72
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 @str_utf8_char_at.buf, ptr align 1 %79, i64 1, i1 false)
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

80:                                               ; preds = %72
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = load i8, ptr %81, align 1, !tbaa !11
  %83 = zext i8 %82 to i32
  %84 = and i32 %83, 224
  %85 = icmp eq i32 %84, 192
  br i1 %85, label %86, label %88

86:                                               ; preds = %80
  %87 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 @str_utf8_char_at.buf, ptr align 1 %87, i64 2, i1 false)
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

88:                                               ; preds = %80
  %89 = load ptr, ptr %7, align 8, !tbaa !5
  %90 = load i8, ptr %89, align 1, !tbaa !11
  %91 = zext i8 %90 to i32
  %92 = and i32 %91, 240
  %93 = icmp eq i32 %92, 224
  br i1 %93, label %94, label %96

94:                                               ; preds = %88
  %95 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 @str_utf8_char_at.buf, ptr align 1 %95, i64 3, i1 false)
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

96:                                               ; preds = %88
  %97 = load ptr, ptr %7, align 8, !tbaa !5
  %98 = load i8, ptr %97, align 1, !tbaa !11
  %99 = zext i8 %98 to i32
  %100 = and i32 %99, 248
  %101 = icmp eq i32 %100, 240
  br i1 %101, label %102, label %104

102:                                              ; preds = %96
  %103 = load ptr, ptr %7, align 8, !tbaa !5
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 @str_utf8_char_at.buf, ptr align 1 %103, i64 4, i1 false)
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

104:                                              ; preds = %96
  br label %105

105:                                              ; preds = %104
  br label %106

106:                                              ; preds = %105
  br label %107

107:                                              ; preds = %106
  br label %108

108:                                              ; preds = %107
  store ptr @str_utf8_char_at.buf, ptr %3, align 8
  store i32 1, ptr %8, align 4
  br label %109

109:                                              ; preds = %108, %102, %94, %86, %78, %71, %58, %21
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %110 = load ptr, ptr %3, align 8
  ret ptr %110
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 #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, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@str_utf8_char_at.buf = external hidden global [5 x i8], align 1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #0

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef nonnull ptr @str_utf8_char_at(ptr nocapture noundef readonly %0, i64 noundef %1) local_unnamed_addr #2 {
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(5) @str_utf8_char_at.buf, i8 0, i64 5, i1 false)
  %3 = getelementptr inbounds i8, ptr %0, i64 16
  %.not = icmp eq i64 %1, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %18
  %.028 = phi ptr [ %19, %18 ], [ %3, %2 ]
  %.02227 = phi i64 [ %20, %18 ], [ 0, %2 ]
  %4 = load i8, ptr %.028, align 1, !tbaa !5
  %5 = zext i8 %4 to i32
  %6 = icmp eq i8 %4, 0
  br i1 %6, label %.loopexit, label %7

7:                                                ; preds = %.lr.ph
  %8 = icmp sgt i8 %4, -1
  br i1 %8, label %18, label %9

9:                                                ; preds = %7
  %10 = and i32 %5, 224
  %11 = icmp eq i32 %10, 192
  br i1 %11, label %18, label %12

12:                                               ; preds = %9
  %13 = and i32 %5, 240
  %14 = icmp eq i32 %13, 224
  br i1 %14, label %18, label %15

15:                                               ; preds = %12
  %16 = and i32 %5, 248
  %17 = icmp eq i32 %16, 240
  br i1 %17, label %18, label %.loopexit

18:                                               ; preds = %15, %12, %9, %7
  %.sink = phi i64 [ 1, %7 ], [ 2, %9 ], [ 3, %12 ], [ 4, %15 ]
  %19 = getelementptr inbounds i8, ptr %.028, i64 %.sink
  %20 = add nuw i64 %.02227, 1
  %exitcond.not = icmp eq i64 %20, %1
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %18, %2
  %.0.lcssa = phi ptr [ %3, %2 ], [ %19, %18 ]
  %21 = load i8, ptr %.0.lcssa, align 1
  %22 = zext i8 %21 to i32
  %23 = icmp eq i8 %21, 0
  br i1 %23, label %.loopexit, label %24

24:                                               ; preds = %._crit_edge
  %25 = icmp sgt i8 %21, -1
  br i1 %25, label %26, label %27

26:                                               ; preds = %24
  store i8 %21, ptr @str_utf8_char_at.buf, align 1
  br label %.loopexit

27:                                               ; preds = %24
  %28 = and i32 %22, 224
  %29 = icmp eq i32 %28, 192
  br i1 %29, label %30, label %32

30:                                               ; preds = %27
  %31 = load i16, ptr %.0.lcssa, align 1
  store i16 %31, ptr @str_utf8_char_at.buf, align 1
  br label %.loopexit

32:                                               ; preds = %27
  %33 = and i32 %22, 240
  %34 = icmp eq i32 %33, 224
  br i1 %34, label %35, label %36

35:                                               ; preds = %32
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(3) @str_utf8_char_at.buf, ptr noundef nonnull align 1 dereferenceable(3) %.0.lcssa, i64 3, i1 false)
  br label %.loopexit

36:                                               ; preds = %32
  %37 = and i32 %22, 248
  %38 = icmp eq i32 %37, 240
  br i1 %38, label %39, label %.loopexit

39:                                               ; preds = %36
  %40 = load i32, ptr %.0.lcssa, align 1
  store i32 %40, ptr @str_utf8_char_at.buf, align 1
  br label %.loopexit

.loopexit:                                        ; preds = %15, %.lr.ph, %36, %._crit_edge, %39, %35, %30, %26
  ret ptr @str_utf8_char_at.buf
}

attributes #0 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass