; LLVM IR from AngelJumbo/lavat :: lavat (tb_utf8_char_to_unicode)

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

@utf8_mask = external hidden constant [6 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
define dso_local i32 @tb_utf8_char_to_unicode(ptr noundef %0, ptr noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i8, align 1
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = load i8, ptr %10, align 1, !tbaa !9
  %12 = sext i8 %11 to i32
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %2
  store i32 -1, ptr %3, align 4
  br label %58

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #2
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = call i32 @tb_utf8_char_length(i8 noundef signext %17)
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %7, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  %20 = load i8, ptr %7, align 1, !tbaa !9
  %21 = zext i8 %20 to i32
  %22 = sub nsw i32 %21, 1
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [6 x i8], ptr @utf8_mask, i64 0, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !9
  store i8 %25, ptr %8, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 0
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %29 = sext i8 %28 to i32
  %30 = load i8, ptr %8, align 1, !tbaa !9
  %31 = zext i8 %30 to i32
  %32 = and i32 %29, %31
  store i32 %32, ptr %9, align 4, !tbaa !10
  store i32 1, ptr %6, align 4, !tbaa !10
  br label %33

33:                                               ; preds = %50, %15
  %34 = load i32, ptr %6, align 4, !tbaa !10
  %35 = load i8, ptr %7, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %37 = icmp slt i32 %34, %36
  br i1 %37, label %38, label %53

38:                                               ; preds = %33
  %39 = load i32, ptr %9, align 4, !tbaa !10
  %40 = shl i32 %39, 6
  store i32 %40, ptr %9, align 4, !tbaa !10
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = load i32, ptr %6, align 4, !tbaa !10
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i8, ptr %41, i64 %43
  %45 = load i8, ptr %44, align 1, !tbaa !9
  %46 = sext i8 %45 to i32
  %47 = and i32 %46, 63
  %48 = load i32, ptr %9, align 4, !tbaa !10
  %49 = or i32 %48, %47
  store i32 %49, ptr %9, align 4, !tbaa !10
  br label %50

50:                                               ; preds = %38
  %51 = load i32, ptr %6, align 4, !tbaa !10
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %6, align 4, !tbaa !10
  br label %33, !llvm.loop !12

53:                                               ; preds = %33
  %54 = load i32, ptr %9, align 4, !tbaa !10
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  store i32 %54, ptr %55, align 4, !tbaa !10
  %56 = load i8, ptr %7, align 1, !tbaa !9
  %57 = zext i8 %56 to i32
  store i32 %57, ptr %3, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %58

58:                                               ; preds = %53, %14
  %59 = load i32, ptr %3, align 4
  ret i32 %59
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tb_utf8_char_length(i8 noundef signext) #1

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 }

!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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !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"

@utf8_mask = external hidden local_unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @tb_utf8_char_to_unicode(ptr nocapture noundef writeonly %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = load i8, ptr %1, align 1, !tbaa !5
  %4 = icmp eq i8 %3, 0
  br i1 %4, label %45, label %5

5:                                                ; preds = %2
  %6 = tail call i32 @tb_utf8_char_length(i8 noundef signext %3)
  %7 = and i32 %6, 255
  %8 = add nsw i32 %7, -1
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [6 x i8], ptr @utf8_mask, i64 0, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = load i8, ptr %1, align 1, !tbaa !5
  %13 = and i8 %12, %11
  %14 = zext i8 %13 to i32
  %15 = icmp ugt i32 %7, 1
  br i1 %15, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %5
  %wide.trip.count = zext nneg i32 %7 to i64
  %16 = add nsw i64 %wide.trip.count, -2
  %xtraiter = and i64 %9, 3
  %17 = icmp ult i64 %16, 3
  br i1 %17, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %9, -4
  %invariant.gep = getelementptr inbounds i8, ptr %1, i64 1
  %invariant.gep24 = getelementptr inbounds i8, ptr %1, i64 2
  %invariant.gep26 = getelementptr inbounds i8, ptr %1, i64 3
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.019 = phi i32 [ %14, %.lr.ph.preheader.new ], [ %38, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %18 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %20 = and i8 %19, 63
  %21 = zext nneg i8 %20 to i32
  %22 = shl i32 %.019, 12
  %23 = shl nuw nsw i32 %21, 6
  %24 = or disjoint i32 %22, %23
  %gep = getelementptr inbounds i8, ptr %invariant.gep, i64 %indvars.iv
  %25 = load i8, ptr %gep, align 1, !tbaa !5
  %26 = and i8 %25, 63
  %27 = zext nneg i8 %26 to i32
  %28 = or disjoint i32 %24, %27
  %gep25 = getelementptr inbounds i8, ptr %invariant.gep24, i64 %indvars.iv
  %29 = load i8, ptr %gep25, align 1, !tbaa !5
  %30 = and i8 %29, 63
  %31 = zext nneg i8 %30 to i32
  %32 = shl i32 %28, 12
  %33 = shl nuw nsw i32 %31, 6
  %34 = or disjoint i32 %32, %33
  %gep27 = getelementptr inbounds i8, ptr %invariant.gep26, i64 %indvars.iv
  %35 = load i8, ptr %gep27, align 1, !tbaa !5
  %36 = and i8 %35, 63
  %37 = zext nneg i8 %36 to i32
  %38 = or disjoint i32 %34, %37
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !8

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %38, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %.019.unr = phi i32 [ %14, %.lr.ph.preheader ], [ %38, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %._crit_edge.loopexit.unr-lcssa ]
  %.019.epil = phi i32 [ %44, %.lr.ph.epil ], [ %.019.unr, %._crit_edge.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %._crit_edge.loopexit.unr-lcssa ]
  %39 = shl i32 %.019.epil, 6
  %40 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv.epil
  %41 = load i8, ptr %40, align 1, !tbaa !5
  %42 = and i8 %41, 63
  %43 = zext nneg i8 %42 to i32
  %44 = or disjoint i32 %39, %43
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge, label %.lr.ph.epil, !llvm.loop !10

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %.lr.ph.epil, %5
  %.0.lcssa = phi i32 [ %14, %5 ], [ %.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %44, %.lr.ph.epil ]
  store i32 %.0.lcssa, ptr %0, align 4, !tbaa !12
  br label %45

45:                                               ; preds = %2, %._crit_edge
  %.015 = phi i32 [ %7, %._crit_edge ], [ -1, %2 ]
  ret i32 %.015
}

; Function Attrs: nounwind uwtable
declare dso_local i32 @tb_utf8_char_length(i8 noundef signext) local_unnamed_addr #0

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

!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"}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.unroll.disable"}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !6, i64 0}


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