; LLVM IR from facebook/redex :: libresource (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"

@stderr = external global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [52 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
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

; Function Attrs: noreturn nounwind
declare void @abort() #2

; Function Attrs: mustprogress uwtable
define dso_local i64 @utf8_to_utf16_length(ptr noundef %0, i64 noundef %1, i1 noundef zeroext %2) #3 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  %14 = zext i1 %2 to i8
  store i8 %14, ptr %7, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #6
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr %6, align 8, !tbaa !9
  %17 = getelementptr inbounds i8, ptr %15, i64 %16
  store ptr %17, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #6
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %18, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %19

19:                                               ; preds = %72, %3
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = icmp ult ptr %20, %21
  br i1 %22, label %23, label %73

23:                                               ; preds = %19
  %24 = load i64, ptr %10, align 8, !tbaa !9
  %25 = add i64 %24, 1
  store i64 %25, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #6
  %26 = load ptr, ptr %9, align 8, !tbaa !5
  %27 = load i8, ptr %26, align 1, !tbaa !13
  %28 = call noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext %27)
  %29 = trunc i64 %28 to i32
  store i32 %29, ptr %11, align 4, !tbaa !14
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = load i32, ptr %11, align 4, !tbaa !14
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = getelementptr inbounds i8, ptr %33, i64 -1
  %35 = load ptr, ptr %8, align 8, !tbaa !5
  %36 = icmp uge ptr %34, %35
  br i1 %36, label %37, label %55

37:                                               ; preds = %23
  %38 = load i8, ptr %7, align 1, !tbaa !11, !range !16, !noundef !17
  %39 = trunc i8 %38 to i1
  br i1 %39, label %40, label %53

40:                                               ; preds = %37
  br label %41

41:                                               ; preds = %40
  br label %42

42:                                               ; preds = %41
  %43 = load ptr, ptr @stderr, align 8, !tbaa !5
  %44 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %43, ptr noundef @.str.4) #6
  %45 = load ptr, ptr @stderr, align 8, !tbaa !5
  %46 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %45, ptr noundef @.str.10) #6
  %47 = load ptr, ptr @stderr, align 8, !tbaa !5
  %48 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %47, ptr noundef @.str.3) #6
  br label %49

49:                                               ; preds = %42
  br label %50

50:                                               ; preds = %49
  call void @abort() #7
  unreachable

51:                                               ; No predecessors!
  br label %52

52:                                               ; preds = %51
  br label %54

53:                                               ; preds = %37
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %70

54:                                               ; preds = %52
  br label %55

55:                                               ; preds = %54, %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #6
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !14
  %58 = sext i32 %57 to i64
  %59 = call noundef i32 @_ZL23utf8_to_utf32_codepointPKhm(ptr noundef %56, i64 noundef %58)
  store i32 %59, ptr %13, align 4, !tbaa !14
  %60 = load i32, ptr %13, align 4, !tbaa !14
  %61 = icmp ugt i32 %60, 65535
  br i1 %61, label %62, label %65

62:                                               ; preds = %55
  %63 = load i64, ptr %10, align 8, !tbaa !9
  %64 = add i64 %63, 1
  store i64 %64, ptr %10, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %62, %55
  %66 = load i32, ptr %11, align 4, !tbaa !14
  %67 = load ptr, ptr %9, align 8, !tbaa !5
  %68 = sext i32 %66 to i64
  %69 = getelementptr inbounds i8, ptr %67, i64 %68
  store ptr %69, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #6
  store i32 0, ptr %12, align 4
  br label %70

70:                                               ; preds = %65, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #6
  %71 = load i32, ptr %12, align 4
  switch i32 %71, label %80 [
    i32 0, label %72
  ]

72:                                               ; preds = %70
  br label %19, !llvm.loop !18

73:                                               ; preds = %19
  %74 = load ptr, ptr %9, align 8, !tbaa !5
  %75 = load ptr, ptr %8, align 8, !tbaa !5
  %76 = icmp ne ptr %74, %75
  br i1 %76, label %77, label %78

77:                                               ; preds = %73
  store i64 -1, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %80

78:                                               ; preds = %73
  %79 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %79, ptr %4, align 8
  store i32 1, ptr %12, align 4
  br label %80

80:                                               ; preds = %78, %77, %70
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #6
  %81 = load i64, ptr %4, align 8
  ret i64 %81
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext) #4

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i32 @_ZL23utf8_to_utf32_codepointPKhm(ptr noundef, i64 noundef) #5

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind "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 = { noreturn nounwind "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 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 = { 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 #5 = { inlinehint 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 #6 = { nounwind }
attributes #7 = { noreturn 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 = !{!"long", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"bool", !7, i64 0}
!13 = !{!7, !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"int", !7, i64 0}
!16 = !{i8 0, i8 2}
!17 = !{}
!18 = distinct !{!18, !19}
!19 = !{!"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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [52 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @abort() local_unnamed_addr #1

; Function Attrs: mustprogress uwtable
define dso_local i64 @utf8_to_utf16_length(ptr noundef %0, i64 noundef %1, i1 noundef zeroext %2) local_unnamed_addr #2 {
  %4 = getelementptr inbounds i8, ptr %0, i64 %1
  %5 = icmp sgt i64 %1, 0
  br i1 %5, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3, %19
  %.02235 = phi ptr [ %9, %19 ], [ %0, %3 ]
  %.02434 = phi i64 [ %spec.select, %19 ], [ 0, %3 ]
  %6 = load i8, ptr %.02235, align 1, !tbaa !5
  %7 = tail call noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext %6)
  %sext = shl i64 %7, 32
  %8 = ashr exact i64 %sext, 32
  %9 = getelementptr inbounds i8, ptr %.02235, i64 %8
  %10 = getelementptr inbounds i8, ptr %9, i64 -1
  %.not29 = icmp ult ptr %10, %4
  br i1 %.not29, label %19, label %11

11:                                               ; preds = %.lr.ph
  br i1 %2, label %12, label %.thread

12:                                               ; preds = %11
  %13 = load ptr, ptr @stderr, align 8, !tbaa !8
  %14 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %13, ptr noundef nonnull @.str.4) #5
  %15 = load ptr, ptr @stderr, align 8, !tbaa !8
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.10) #5
  %17 = load ptr, ptr @stderr, align 8, !tbaa !8
  %18 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef nonnull @.str.3) #5
  tail call void @abort() #6
  unreachable

19:                                               ; preds = %.lr.ph
  %20 = tail call noundef i32 @_ZL23utf8_to_utf32_codepointPKhm(ptr noundef nonnull %.02235, i64 noundef %8)
  %21 = icmp ugt i32 %20, 65535
  %spec.select.v = select i1 %21, i64 2, i64 1
  %spec.select = add i64 %spec.select.v, %.02434
  %22 = icmp ult ptr %9, %4
  br i1 %22, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %19, %3
  %.024.lcssa = phi i64 [ 0, %3 ], [ %spec.select, %19 ]
  %.022.lcssa = phi ptr [ %0, %3 ], [ %9, %19 ]
  %.not = icmp eq ptr %.022.lcssa, %4
  %.024. = select i1 %.not, i64 %.024.lcssa, i64 -1
  br label %.thread

.thread:                                          ; preds = %11, %._crit_edge
  %.2 = phi i64 [ %.024., %._crit_edge ], [ -1, %11 ]
  ret i64 %.2
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress uwtable
declare hidden noundef i32 @_ZL23utf8_to_utf32_codepointPKhm(ptr noundef, i64 noundef) local_unnamed_addr #4

attributes #0 = { nofree nounwind "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 = { noreturn nounwind "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 = { 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 #4 = { inlinehint 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 #5 = { cold nounwind }
attributes #6 = { noreturn 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 = !{!"any pointer", !6, i64 0}


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