; LLVM IR from tuplex/tuplex :: tuplex/utils/src/third_party/ryu (d2s)

; 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.floating_decimal_64 = type { i64, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @d2s_buffered_n(double noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca double, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i8, align 1
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca %struct.floating_decimal_64, align 8
  %12 = alloca i8, align 1
  %13 = alloca i64, align 8
  %14 = alloca i32, align 4
  %15 = alloca %struct.floating_decimal_64, align 8
  store double %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %16 = load double, ptr %4, align 8, !tbaa !5
  %17 = call i64 @double_to_bits(double noundef %16)
  store i64 %17, ptr %6, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %7) #4
  %18 = load i64, ptr %6, align 8, !tbaa !11
  %19 = lshr i64 %18, 63
  %20 = and i64 %19, 1
  %21 = icmp ne i64 %20, 0
  %22 = zext i1 %21 to i8
  store i8 %22, ptr %7, align 1, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %23 = load i64, ptr %6, align 8, !tbaa !11
  %24 = and i64 %23, 4503599627370495
  store i64 %24, ptr %8, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %25 = load i64, ptr %6, align 8, !tbaa !11
  %26 = lshr i64 %25, 52
  %27 = and i64 %26, 2047
  %28 = trunc i64 %27 to i32
  store i32 %28, ptr %9, align 4, !tbaa !15
  %29 = load i32, ptr %9, align 4, !tbaa !15
  %30 = icmp eq i32 %29, 2047
  br i1 %30, label %37, label %31

31:                                               ; preds = %2
  %32 = load i32, ptr %9, align 4, !tbaa !15
  %33 = icmp eq i32 %32, 0
  br i1 %33, label %34, label %46

34:                                               ; preds = %31
  %35 = load i64, ptr %8, align 8, !tbaa !11
  %36 = icmp eq i64 %35, 0
  br i1 %36, label %37, label %46

37:                                               ; preds = %34, %2
  %38 = load ptr, ptr %5, align 8, !tbaa !9
  %39 = load i8, ptr %7, align 1, !tbaa !13, !range !17, !noundef !18
  %40 = trunc i8 %39 to i1
  %41 = load i32, ptr %9, align 4, !tbaa !15
  %42 = icmp ne i32 %41, 0
  %43 = load i64, ptr %8, align 8, !tbaa !11
  %44 = icmp ne i64 %43, 0
  %45 = call i32 @copy_special_str(ptr noundef %38, i1 noundef zeroext %40, i1 noundef zeroext %42, i1 noundef zeroext %44)
  store i32 %45, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %95

46:                                               ; preds = %34, %31
  call void @llvm.lifetime.start.p0(i64 16, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #4
  %47 = load i64, ptr %8, align 8, !tbaa !11
  %48 = load i32, ptr %9, align 4, !tbaa !15
  %49 = call zeroext i1 @d2d_small_int(i64 noundef %47, i32 noundef %48, ptr noundef %11)
  %50 = zext i1 %49 to i8
  store i8 %50, ptr %12, align 1, !tbaa !13
  %51 = load i8, ptr %12, align 1, !tbaa !13, !range !17, !noundef !18
  %52 = trunc i8 %51 to i1
  br i1 %52, label %53, label %78

53:                                               ; preds = %46
  br label %54

54:                                               ; preds = %76, %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %55 = getelementptr inbounds %struct.floating_decimal_64, ptr %11, i32 0, i32 0
  %56 = load i64, ptr %55, align 8, !tbaa !19
  %57 = call i64 @div10(i64 noundef %56)
  store i64 %57, ptr %13, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %58 = getelementptr inbounds %struct.floating_decimal_64, ptr %11, i32 0, i32 0
  %59 = load i64, ptr %58, align 8, !tbaa !19
  %60 = trunc i64 %59 to i32
  %61 = load i64, ptr %13, align 8, !tbaa !11
  %62 = trunc i64 %61 to i32
  %63 = mul i32 10, %62
  %64 = sub i32 %60, %63
  store i32 %64, ptr %14, align 4, !tbaa !15
  %65 = load i32, ptr %14, align 4, !tbaa !15
  %66 = icmp ne i32 %65, 0
  br i1 %66, label %67, label %68

67:                                               ; preds = %54
  store i32 2, ptr %10, align 4
  br label %74

68:                                               ; preds = %54
  %69 = load i64, ptr %13, align 8, !tbaa !11
  %70 = getelementptr inbounds %struct.floating_decimal_64, ptr %11, i32 0, i32 0
  store i64 %69, ptr %70, align 8, !tbaa !19
  %71 = getelementptr inbounds %struct.floating_decimal_64, ptr %11, i32 0, i32 1
  %72 = load i32, ptr %71, align 8, !tbaa !21
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %71, align 8, !tbaa !21
  store i32 0, ptr %10, align 4
  br label %74

74:                                               ; preds = %68, %67
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  %75 = load i32, ptr %10, align 4
  switch i32 %75, label %97 [
    i32 0, label %76
    i32 2, label %77
  ]

76:                                               ; preds = %74
  br label %54

77:                                               ; preds = %74
  br label %86

78:                                               ; preds = %46
  call void @llvm.lifetime.start.p0(i64 16, ptr %15) #4
  %79 = load i64, ptr %8, align 8, !tbaa !11
  %80 = load i32, ptr %9, align 4, !tbaa !15
  %81 = call { i64, i32 } @d2d(i64 noundef %79, i32 noundef %80)
  %82 = getelementptr inbounds { i64, i32 }, ptr %15, i32 0, i32 0
  %83 = extractvalue { i64, i32 } %81, 0
  store i64 %83, ptr %82, align 8
  %84 = getelementptr inbounds { i64, i32 }, ptr %15, i32 0, i32 1
  %85 = extractvalue { i64, i32 } %81, 1
  store i32 %85, ptr %84, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %11, ptr align 8 %15, i64 16, i1 false), !tbaa.struct !22
  call void @llvm.lifetime.end.p0(i64 16, ptr %15) #4
  br label %86

86:                                               ; preds = %78, %77
  %87 = load i8, ptr %7, align 1, !tbaa !13, !range !17, !noundef !18
  %88 = trunc i8 %87 to i1
  %89 = load ptr, ptr %5, align 8, !tbaa !9
  %90 = getelementptr inbounds { i64, i32 }, ptr %11, i32 0, i32 0
  %91 = load i64, ptr %90, align 8
  %92 = getelementptr inbounds { i64, i32 }, ptr %11, i32 0, i32 1
  %93 = load i32, ptr %92, align 8
  %94 = call i32 @to_chars(i64 %91, i32 %93, i1 noundef zeroext %88, ptr noundef %89)
  store i32 %94, ptr %3, align 4
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 16, ptr %11) #4
  br label %95

95:                                               ; preds = %86, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 1, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %96 = load i32, ptr %3, align 4
  ret i32 %96

97:                                               ; preds = %74
  unreachable
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @double_to_bits(double noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @copy_special_str(ptr noundef, i1 noundef zeroext, i1 noundef zeroext, i1 noundef zeroext) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @d2d_small_int(i64 noundef, i32 noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @div10(i64 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden { i64, i32 } @d2d(i64 noundef, i32 noundef) #2

; 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) #3

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @to_chars(i64, i32, i1 noundef zeroext, ptr noundef) #2

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"int", !7, i64 0}
!17 = !{i8 0, i8 2}
!18 = !{}
!19 = !{!20, !12, i64 0}
!20 = !{!"floating_decimal_64", !12, i64 0, !16, i64 8}
!21 = !{!20, !16, i64 8}
!22 = !{i64 0, i64 8, !11, i64 8, i64 4, !15}


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

%struct.floating_decimal_64 = type { i64, i32 }

; Function Attrs: nounwind uwtable
define dso_local i32 @d2s_buffered_n(double noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = alloca %struct.floating_decimal_64, align 8
  %4 = tail call i64 @double_to_bits(double noundef %0)
  %5 = icmp slt i64 %4, 0
  %6 = and i64 %4, 4503599627370495
  %7 = lshr i64 %4, 52
  %8 = trunc nuw nsw i64 %7 to i32
  %9 = and i32 %8, 2047
  %10 = icmp eq i32 %9, 2047
  br i1 %10, label %14, label %11

11:                                               ; preds = %2
  %12 = icmp eq i32 %9, 0
  %13 = icmp eq i64 %6, 0
  %or.cond = and i1 %13, %12
  br i1 %or.cond, label %14, label %18

14:                                               ; preds = %11, %2
  %15 = icmp ne i32 %9, 0
  %16 = icmp ne i64 %6, 0
  %17 = tail call i32 @copy_special_str(ptr noundef %1, i1 noundef zeroext %5, i1 noundef zeroext %15, i1 noundef zeroext %16)
  br label %44

18:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  %19 = call zeroext i1 @d2d_small_int(i64 noundef %6, i32 noundef %9, ptr noundef nonnull %3)
  br i1 %19, label %.preheader, label %36

.preheader:                                       ; preds = %18
  %20 = load i64, ptr %3, align 8, !tbaa !5
  %21 = call i64 @div10(i64 noundef %20)
  %22 = load i64, ptr %3, align 8
  %23 = trunc i64 %22 to i32
  %24 = trunc i64 %21 to i32
  %.neg27 = mul i32 %24, -10
  %25 = sub i32 0, %23
  %.not28 = icmp eq i32 %.neg27, %25
  br i1 %.not28, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %.preheader
  %26 = getelementptr inbounds i8, ptr %3, i64 8
  br label %27

27:                                               ; preds = %.lr.ph, %27
  %28 = phi i64 [ %21, %.lr.ph ], [ %31, %27 ]
  store i64 %28, ptr %3, align 8, !tbaa !5
  %29 = load i32, ptr %26, align 8, !tbaa !11
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %26, align 8, !tbaa !11
  %31 = call i64 @div10(i64 noundef %28)
  %32 = load i64, ptr %3, align 8
  %33 = trunc i64 %32 to i32
  %34 = trunc i64 %31 to i32
  %.neg = mul i32 %34, -10
  %35 = sub i32 0, %33
  %.not = icmp eq i32 %.neg, %35
  br i1 %.not, label %27, label %.thread

36:                                               ; preds = %18
  %37 = call { i64, i32 } @d2d(i64 noundef %6, i32 noundef %9)
  %38 = extractvalue { i64, i32 } %37, 0
  %39 = extractvalue { i64, i32 } %37, 1
  store i64 %38, ptr %3, align 8, !tbaa !12
  %.sroa.4.0..sroa_idx = getelementptr inbounds i8, ptr %3, i64 8
  store i32 %39, ptr %.sroa.4.0..sroa_idx, align 8, !tbaa !13
  br label %.thread

.thread:                                          ; preds = %27, %.preheader, %36
  %40 = phi i64 [ %22, %.preheader ], [ %38, %36 ], [ %32, %27 ]
  %41 = getelementptr inbounds i8, ptr %3, i64 8
  %42 = load i32, ptr %41, align 8
  %43 = call i32 @to_chars(i64 %40, i32 %42, i1 noundef zeroext %5, ptr noundef %1)
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  br label %44

44:                                               ; preds = %.thread, %14
  %.0 = phi i32 [ %17, %14 ], [ %43, %.thread ]
  ret i32 %.0
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @double_to_bits(double noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @copy_special_str(ptr noundef, i1 noundef zeroext, i1 noundef zeroext, i1 noundef zeroext) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden zeroext i1 @d2d_small_int(i64 noundef, i32 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i64 @div10(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare hidden { i64, i32 } @d2d(i64 noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden i32 @to_chars(i64, i32, i1 noundef zeroext, ptr noundef) local_unnamed_addr #2

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 = { 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, !7, i64 0}
!6 = !{!"floating_decimal_64", !7, i64 0, !10, i64 8}
!7 = !{!"long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = !{!7, !7, i64 0}
!13 = !{!10, !10, i64 0}


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