; 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.13 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [14 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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext) #3

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

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

19:                                               ; preds = %4
  %20 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %20, ptr %5, align 8
  br label %109

21:                                               ; preds = %4
  br label %22

22:                                               ; preds = %21
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = icmp ugt i64 %23, 9223372036854775807
  br i1 %24, label %25, label %43

25:                                               ; preds = %22
  br label %26

26:                                               ; preds = %25
  %27 = load ptr, ptr @stderr, align 8, !tbaa !5
  %28 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef @.str.4) #6
  %29 = load ptr, ptr @stderr, align 8, !tbaa !5
  %30 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %29, ptr noundef @.str.13) #6
  %31 = load ptr, ptr @stderr, align 8, !tbaa !5
  %32 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %31, ptr noundef @.str.3) #6
  br label %33

33:                                               ; preds = %26
  br label %34

34:                                               ; preds = %33
  %35 = load ptr, ptr @stderr, align 8, !tbaa !5
  %36 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef @.str.4) #6
  %37 = load ptr, ptr @stderr, align 8, !tbaa !5
  %38 = load i64, ptr %9, align 8, !tbaa !9
  %39 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %37, ptr noundef @.str.14, i64 noundef %38) #6
  %40 = load ptr, ptr @stderr, align 8, !tbaa !5
  %41 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %40, ptr noundef @.str.3) #6
  br label %42

42:                                               ; preds = %34
  call void @abort() #7
  unreachable

43:                                               ; preds = %22
  br label %44

44:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #6
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load i64, ptr %7, align 8, !tbaa !9
  %47 = getelementptr inbounds i8, ptr %45, i64 %46
  store ptr %47, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #6
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %48, ptr %11, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #6
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  %50 = load i64, ptr %9, align 8, !tbaa !9
  %51 = getelementptr inbounds i16, ptr %49, i64 %50
  store ptr %51, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #6
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %52, ptr %13, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %105, %44
  %54 = load ptr, ptr %11, align 8, !tbaa !5
  %55 = load ptr, ptr %10, align 8, !tbaa !5
  %56 = icmp ult ptr %54, %55
  br i1 %56, label %57, label %61

57:                                               ; preds = %53
  %58 = load ptr, ptr %13, align 8, !tbaa !5
  %59 = load ptr, ptr %12, align 8, !tbaa !5
  %60 = icmp ult ptr %58, %59
  br label %61

61:                                               ; preds = %57, %53
  %62 = phi i1 [ false, %53 ], [ %60, %57 ]
  br i1 %62, label %63, label %106

63:                                               ; preds = %61
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #6
  %64 = load ptr, ptr %11, align 8, !tbaa !5
  %65 = load i8, ptr %64, align 1, !tbaa !11
  %66 = call noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext %65)
  store i64 %66, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #6
  %67 = load ptr, ptr %11, align 8, !tbaa !5
  %68 = load i64, ptr %14, align 8, !tbaa !9
  %69 = call noundef i32 @_ZL23utf8_to_utf32_codepointPKhm(ptr noundef %67, i64 noundef %68)
  store i32 %69, ptr %15, align 4, !tbaa !12
  %70 = load i32, ptr %15, align 4, !tbaa !12
  %71 = icmp ule i32 %70, 65535
  br i1 %71, label %72, label %77

72:                                               ; preds = %63
  %73 = load i32, ptr %15, align 4, !tbaa !12
  %74 = trunc i32 %73 to i16
  %75 = load ptr, ptr %13, align 8, !tbaa !5
  %76 = getelementptr inbounds i16, ptr %75, i32 1
  store ptr %76, ptr %13, align 8, !tbaa !5
  store i16 %74, ptr %75, align 2, !tbaa !14
  br label %99

77:                                               ; preds = %63
  %78 = load i32, ptr %15, align 4, !tbaa !12
  %79 = sub i32 %78, 65536
  store i32 %79, ptr %15, align 4, !tbaa !12
  %80 = load i32, ptr %15, align 4, !tbaa !12
  %81 = lshr i32 %80, 10
  %82 = add i32 %81, 55296
  %83 = trunc i32 %82 to i16
  %84 = load ptr, ptr %13, align 8, !tbaa !5
  %85 = getelementptr inbounds i16, ptr %84, i32 1
  store ptr %85, ptr %13, align 8, !tbaa !5
  store i16 %83, ptr %84, align 2, !tbaa !14
  %86 = load ptr, ptr %13, align 8, !tbaa !5
  %87 = load ptr, ptr %12, align 8, !tbaa !5
  %88 = icmp uge ptr %86, %87
  br i1 %88, label %89, label %92

89:                                               ; preds = %77
  %90 = load ptr, ptr %13, align 8, !tbaa !5
  %91 = getelementptr inbounds i16, ptr %90, i64 -1
  store ptr %91, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %103

92:                                               ; preds = %77
  %93 = load i32, ptr %15, align 4, !tbaa !12
  %94 = and i32 %93, 1023
  %95 = add i32 %94, 56320
  %96 = trunc i32 %95 to i16
  %97 = load ptr, ptr %13, align 8, !tbaa !5
  %98 = getelementptr inbounds i16, ptr %97, i32 1
  store ptr %98, ptr %13, align 8, !tbaa !5
  store i16 %96, ptr %97, align 2, !tbaa !14
  br label %99

99:                                               ; preds = %92, %72
  %100 = load i64, ptr %14, align 8, !tbaa !9
  %101 = load ptr, ptr %11, align 8, !tbaa !5
  %102 = getelementptr inbounds i8, ptr %101, i64 %100
  store ptr %102, ptr %11, align 8, !tbaa !5
  store i32 0, ptr %16, align 4
  br label %103

103:                                              ; preds = %99, %89
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #6
  %104 = load i32, ptr %16, align 4
  switch i32 %104, label %108 [
    i32 0, label %105
  ]

105:                                              ; preds = %103
  br label %53, !llvm.loop !16

106:                                              ; preds = %61
  %107 = load ptr, ptr %13, align 8, !tbaa !5
  store ptr %107, ptr %5, align 8
  store i32 1, ptr %16, align 4
  br label %108

108:                                              ; preds = %106, %103
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #6
  br label %109

109:                                              ; preds = %108, %19
  %110 = load ptr, ptr %5, align 8
  ret ptr %110
}

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 = { 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 = { 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 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = !{!15, !15, i64 0}
!15 = !{!"char16_t", !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"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.13 = external hidden unnamed_addr constant [30 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [14 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: inlinehint mustprogress nounwind uwtable
declare hidden noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext) local_unnamed_addr #2

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

; Function Attrs: mustprogress uwtable
define dso_local ptr @utf8_to_utf16_no_null_terminator(ptr noundef %0, i64 noundef %1, ptr noundef writeonly %2, i64 noundef %3) local_unnamed_addr #4 {
  %5 = icmp eq i64 %3, 0
  br i1 %5, label %.thread, label %6

6:                                                ; preds = %4
  %7 = icmp slt i64 %3, 0
  br i1 %7, label %8, label %21

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

21:                                               ; preds = %6
  %22 = getelementptr inbounds i8, ptr %0, i64 %1
  %23 = getelementptr inbounds i16, ptr %2, i64 %3
  %24 = icmp sgt i64 %1, 0
  br i1 %24, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %21, %43
  %.03349 = phi ptr [ %.134, %43 ], [ %2, %21 ]
  %.03648 = phi ptr [ %44, %43 ], [ %0, %21 ]
  %25 = load i8, ptr %.03648, align 1, !tbaa !9
  %26 = tail call noundef i64 @_ZL18utf8_codepoint_lenh(i8 noundef zeroext %25)
  %27 = tail call noundef i32 @_ZL23utf8_to_utf32_codepointPKhm(ptr noundef nonnull %.03648, i64 noundef %26)
  %28 = icmp ult i32 %27, 65536
  br i1 %28, label %29, label %32

29:                                               ; preds = %.lr.ph
  %30 = trunc nuw i32 %27 to i16
  %31 = getelementptr inbounds i8, ptr %.03349, i64 2
  store i16 %30, ptr %.03349, align 2, !tbaa !10
  br label %43

32:                                               ; preds = %.lr.ph
  %33 = add i32 %27, 67043328
  %34 = lshr i32 %33, 10
  %35 = trunc i32 %34 to i16
  %36 = add i16 %35, -10240
  %37 = getelementptr inbounds i8, ptr %.03349, i64 2
  store i16 %36, ptr %.03349, align 2, !tbaa !10
  %.not = icmp ult ptr %37, %23
  br i1 %.not, label %38, label %.thread

38:                                               ; preds = %32
  %39 = trunc i32 %27 to i16
  %40 = and i16 %39, 1023
  %41 = or disjoint i16 %40, -9216
  %42 = getelementptr inbounds i8, ptr %.03349, i64 4
  store i16 %41, ptr %37, align 2, !tbaa !10
  br label %43

43:                                               ; preds = %29, %38
  %.134 = phi ptr [ %31, %29 ], [ %42, %38 ]
  %44 = getelementptr inbounds i8, ptr %.03648, i64 %26
  %45 = icmp ult ptr %44, %22
  %46 = icmp ult ptr %.134, %23
  %47 = select i1 %45, i1 %46, i1 false
  br i1 %47, label %.lr.ph, label %.thread, !llvm.loop !12

.thread:                                          ; preds = %43, %32, %21, %4
  %.3 = phi ptr [ %2, %4 ], [ %2, %21 ], [ %.134, %43 ], [ %.03349, %32 ]
  ret ptr %.3
}

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 = { 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 #3 = { 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 #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"char16_t", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass