; 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 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL27utf32_codepoint_utf8_lengthDi(i32 noundef zeroext) #1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: mustprogress uwtable
define dso_local i64 @utf16_to_utf8_length(ptr noundef %0, i64 noundef %1) #3 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i64 %1, ptr %5, align 8, !tbaa !9
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %15, label %12

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

15:                                               ; preds = %12, %2
  store i64 -1, ptr %3, align 8
  br label %76

16:                                               ; preds = %12
  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
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = load i64, ptr %5, align 8, !tbaa !9
  %19 = getelementptr inbounds i16, ptr %17, i64 %18
  store ptr %19, ptr %7, align 8, !tbaa !5
  br label %20

20:                                               ; preds = %72, %16
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  %23 = icmp ult ptr %21, %22
  br i1 %23, label %24, label %73

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = load i16, ptr %25, align 2, !tbaa !11
  %27 = zext i16 %26 to i32
  %28 = and i32 %27, 64512
  %29 = icmp eq i32 %28, 55296
  br i1 %29, label %30, label %45

30:                                               ; preds = %24
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds i16, ptr %31, i64 1
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = icmp ult ptr %32, %33
  br i1 %34, label %35, label %45

35:                                               ; preds = %30
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  %37 = getelementptr inbounds i16, ptr %36, i64 1
  %38 = load i16, ptr %37, align 2, !tbaa !11
  %39 = zext i16 %38 to i32
  %40 = and i32 %39, 64512
  %41 = icmp eq i32 %40, 56320
  br i1 %41, label %42, label %45

42:                                               ; preds = %35
  store i64 4, ptr %8, align 8, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds i16, ptr %43, i64 2
  store ptr %44, ptr %4, align 8, !tbaa !5
  br label %51

45:                                               ; preds = %35, %30, %24
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds i16, ptr %46, i32 1
  store ptr %47, ptr %4, align 8, !tbaa !5
  %48 = load i16, ptr %46, align 2, !tbaa !11
  %49 = zext i16 %48 to i32
  %50 = call noundef i64 @_ZL27utf32_codepoint_utf8_lengthDi(i32 noundef zeroext %49)
  store i64 %50, ptr %8, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %45, %42
  %52 = load i64, ptr %8, align 8, !tbaa !9
  %53 = sub i64 9223372036854775807, %52
  %54 = load i64, ptr %6, align 8, !tbaa !9
  %55 = icmp ult i64 %53, %54
  br i1 %55, label %56, label %66

56:                                               ; preds = %51
  br label %57

57:                                               ; preds = %56
  %58 = load ptr, ptr @stderr, align 8, !tbaa !5
  %59 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %58, ptr noundef @.str) #4
  %60 = load ptr, ptr @stderr, align 8, !tbaa !5
  %61 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %60, ptr noundef @.str.1, i32 noundef 1397638484, ptr noundef @.str.2) #4
  %62 = load ptr, ptr @stderr, align 8, !tbaa !5
  %63 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %62, ptr noundef @.str.3) #4
  br label %64

64:                                               ; preds = %57
  br label %65

65:                                               ; preds = %64
  store i64 -1, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %70

66:                                               ; preds = %51
  %67 = load i64, ptr %8, align 8, !tbaa !9
  %68 = load i64, ptr %6, align 8, !tbaa !9
  %69 = add i64 %68, %67
  store i64 %69, ptr %6, align 8, !tbaa !9
  store i32 0, ptr %9, align 4
  br label %70

70:                                               ; preds = %66, %65
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %71 = load i32, ptr %9, align 4
  switch i32 %71, label %75 [
    i32 0, label %72
  ]

72:                                               ; preds = %70
  br label %20, !llvm.loop !13

73:                                               ; preds = %20
  %74 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %74, ptr %3, align 8
  store i32 1, ptr %9, align 4
  br label %75

75:                                               ; preds = %73, %70
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %76

76:                                               ; preds = %75, %15
  %77 = load i64, ptr %3, align 8
  ret i64 %77
}

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 = { 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 = { 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 = !{!"char16_t", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 = external hidden unnamed_addr constant [8 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [29 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL27utf32_codepoint_utf8_lengthDi(i32 noundef zeroext) local_unnamed_addr #0

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local i64 @utf16_to_utf8_length(ptr noundef readonly %0, i64 noundef %1) local_unnamed_addr #2 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq i64 %1, 0
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %.loopexit, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i16, ptr %0, i64 %1
  %7 = icmp sgt i64 %1, 0
  br i1 %7, label %.lr.ph, label %.loopexit

8:                                                ; preds = %27
  %9 = add i64 %.019, %.02132
  %10 = icmp ult ptr %.124, %6
  br i1 %10, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5, %8
  %.02132 = phi i64 [ %9, %8 ], [ 0, %5 ]
  %.02331 = phi ptr [ %.124, %8 ], [ %0, %5 ]
  %11 = load i16, ptr %.02331, align 2, !tbaa !5
  %12 = and i16 %11, -1024
  %13 = icmp eq i16 %12, -10240
  br i1 %13, label %14, label %23

14:                                               ; preds = %.lr.ph
  %15 = getelementptr inbounds i8, ptr %.02331, i64 2
  %16 = icmp ult ptr %15, %6
  br i1 %16, label %17, label %23

17:                                               ; preds = %14
  %18 = load i16, ptr %15, align 2, !tbaa !5
  %19 = and i16 %18, -1024
  %20 = icmp eq i16 %19, -9216
  br i1 %20, label %21, label %23

21:                                               ; preds = %17
  %22 = getelementptr inbounds i8, ptr %.02331, i64 4
  br label %27

23:                                               ; preds = %17, %14, %.lr.ph
  %24 = getelementptr inbounds i8, ptr %.02331, i64 2
  %25 = zext i16 %11 to i32
  %26 = tail call noundef i64 @_ZL27utf32_codepoint_utf8_lengthDi(i32 noundef zeroext %25)
  br label %27

27:                                               ; preds = %23, %21
  %.124 = phi ptr [ %22, %21 ], [ %24, %23 ]
  %.019 = phi i64 [ 4, %21 ], [ %26, %23 ]
  %28 = sub i64 9223372036854775807, %.019
  %.not = icmp ult i64 %28, %.02132
  br i1 %.not, label %.thread, label %8

.thread:                                          ; preds = %27
  %29 = load ptr, ptr @stderr, align 8, !tbaa !9
  %30 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef nonnull @.str) #3
  %31 = load ptr, ptr @stderr, align 8, !tbaa !9
  %32 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %31, ptr noundef nonnull @.str.1, i32 noundef 1397638484, ptr noundef nonnull @.str.2) #3
  %33 = load ptr, ptr @stderr, align 8, !tbaa !9
  %34 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef nonnull @.str.3) #3
  br label %.loopexit

.loopexit:                                        ; preds = %8, %5, %.thread, %2
  %.3 = phi i64 [ -1, %2 ], [ -1, %.thread ], [ 0, %5 ], [ %9, %8 ]
  ret i64 %.3
}

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 = { 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 #2 = { 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 #3 = { cold 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 = !{!"char16_t", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}


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