; LLVM IR from nickmqb/muon :: bootstrap/mu.ll_CGenerator (writeUnescapedString__)

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

@mu_____string393 = external dso_local global %struct.string__, align 8
@mu_____string394 = external dso_local global %struct.string__, align 8
@mu_____string395 = external dso_local global %struct.string__, align 8
@mu_____string397 = external dso_local global %struct.string__, align 8
@top_____abandonFn__ = external dso_local thread_local global ptr, align 8

; 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 speculatable willreturn memory(none)
declare nonnull ptr @llvm.threadlocal.address.p0(ptr nonnull) #1

; 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
declare dso_local void @StringBuilder__write__(ptr noundef, ptr, i32) #2

; Function Attrs: nounwind uwtable
declare dso_local void @StringBuilder__writeChar__(ptr noundef, i8 noundef signext) #2

; Function Attrs: nounwind uwtable
declare dso_local { ptr, i32 } @StringBuilder__toString__(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @CGenerator__writeByteHexValue__(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local { ptr, i32 } @CGenerator__writeUnescapedString__(ptr noundef %0, ptr %1, i32 %2) #2 {
  %4 = alloca %struct.string__, align 8
  %5 = alloca %struct.string__, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  %12 = alloca i8, align 1
  %13 = getelementptr inbounds { ptr, i32 }, ptr %5, i32 0, i32 0
  store ptr %1, ptr %13, align 8
  %14 = getelementptr inbounds { ptr, i32 }, ptr %5, i32 0, i32 1
  store i32 %2, ptr %14, align 8
  store ptr %0, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  br label %15

15:                                               ; preds = %84, %3
  %16 = load i32, ptr %7, align 4, !tbaa !9
  %17 = getelementptr inbounds %struct.string__, ptr %5, i32 0, i32 1
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp slt i32 %16, %18
  br i1 %19, label %20, label %87

20:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr %5, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %21 = load i32, ptr %7, align 4, !tbaa !9
  store i32 %21, ptr %9, align 4, !tbaa !9
  %22 = load i32, ptr %9, align 4, !tbaa !9
  %23 = load ptr, ptr %8, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.string__, ptr %23, i32 0, i32 1
  %25 = load i32, ptr %24, align 8, !tbaa !11
  %26 = icmp ult i32 %22, %25
  br i1 %26, label %30, label %27

27:                                               ; preds = %20
  %28 = call align 8 ptr @llvm.threadlocal.address.p0(ptr align 8 @top_____abandonFn__)
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  call void %29(i32 noundef -3)
  br label %30

30:                                               ; preds = %27, %20
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #3
  %31 = load ptr, ptr %8, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.string__, ptr %31, i32 0, i32 0
  %33 = load ptr, ptr %32, align 8, !tbaa !13
  %34 = load i32, ptr %9, align 4, !tbaa !9
  %35 = zext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %33, i64 %35
  %37 = load i8, ptr %36, align 1, !tbaa !14
  store i8 %37, ptr %10, align 1, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %38 = load i8, ptr %10, align 1, !tbaa !14
  %39 = zext i8 %38 to i32
  store i32 %39, ptr %11, align 4, !tbaa !9
  %40 = load i8, ptr %10, align 1, !tbaa !14
  %41 = sext i8 %40 to i32
  %42 = icmp eq i32 %41, 34
  br i1 %42, label %43, label %47

43:                                               ; preds = %30
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = load ptr, ptr @mu_____string393, align 8
  %46 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr @mu_____string393, i32 0, i32 1), align 8
  call void @StringBuilder__write__(ptr noundef %44, ptr %45, i32 %46)
  br label %84

47:                                               ; preds = %30
  %48 = load i8, ptr %10, align 1, !tbaa !14
  %49 = sext i8 %48 to i32
  %50 = icmp eq i32 %49, 92
  br i1 %50, label %51, label %55

51:                                               ; preds = %47
  %52 = load ptr, ptr %6, align 8, !tbaa !5
  %53 = load ptr, ptr @mu_____string394, align 8
  %54 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr @mu_____string394, i32 0, i32 1), align 8
  call void @StringBuilder__write__(ptr noundef %52, ptr %53, i32 %54)
  br label %83

55:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #3
  %56 = load i32, ptr %11, align 4, !tbaa !9
  %57 = icmp sle i32 32, %56
  %58 = zext i1 %57 to i32
  %59 = trunc i32 %58 to i8
  store i8 %59, ptr %12, align 1, !tbaa !14
  %60 = load i8, ptr %12, align 1, !tbaa !14
  %61 = icmp ne i8 %60, 0
  br i1 %61, label %62, label %67

62:                                               ; preds = %55
  %63 = load i32, ptr %11, align 4, !tbaa !9
  %64 = icmp slt i32 %63, 127
  %65 = zext i1 %64 to i32
  %66 = trunc i32 %65 to i8
  store i8 %66, ptr %12, align 1, !tbaa !14
  br label %67

67:                                               ; preds = %62, %55
  %68 = load i8, ptr %12, align 1, !tbaa !14
  %69 = icmp ne i8 %68, 0
  br i1 %69, label %70, label %73

70:                                               ; preds = %67
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = load i8, ptr %10, align 1, !tbaa !14
  call void @StringBuilder__writeChar__(ptr noundef %71, i8 noundef signext %72)
  br label %82

73:                                               ; preds = %67
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = load ptr, ptr @mu_____string395, align 8
  %76 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr @mu_____string395, i32 0, i32 1), align 8
  call void @StringBuilder__write__(ptr noundef %74, ptr %75, i32 %76)
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = load i32, ptr %11, align 4, !tbaa !9
  call void @CGenerator__writeByteHexValue__(ptr noundef %77, i32 noundef %78)
  %79 = load ptr, ptr %6, align 8, !tbaa !5
  %80 = load ptr, ptr @mu_____string397, align 8
  %81 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr @mu_____string397, i32 0, i32 1), align 8
  call void @StringBuilder__write__(ptr noundef %79, ptr %80, i32 %81)
  br label %82

82:                                               ; preds = %73, %70
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #3
  br label %83

83:                                               ; preds = %82, %51
  br label %84

84:                                               ; preds = %83, %43
  %85 = load i32, ptr %7, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %15, !llvm.loop !15

87:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = call { ptr, i32 } @StringBuilder__toString__(ptr noundef %88)
  %90 = getelementptr inbounds { ptr, i32 }, ptr %4, i32 0, i32 0
  %91 = extractvalue { ptr, i32 } %89, 0
  store ptr %91, ptr %90, align 8
  %92 = getelementptr inbounds { ptr, i32 }, ptr %4, i32 0, i32 1
  %93 = extractvalue { ptr, i32 } %89, 1
  store i32 %93, ptr %92, align 8
  %94 = load { ptr, i32 }, ptr %4, align 8
  ret { ptr, i32 } %94
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 8}
!12 = !{!"", !6, i64 0, !10, i64 8}
!13 = !{!12, !6, i64 0}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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"

%struct.string__ = type { ptr, i32 }

@mu_____string393 = external dso_local local_unnamed_addr global %struct.string__, align 8
@mu_____string394 = external dso_local local_unnamed_addr global %struct.string__, align 8
@mu_____string395 = external dso_local local_unnamed_addr global %struct.string__, align 8
@mu_____string397 = external dso_local local_unnamed_addr global %struct.string__, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @StringBuilder__write__(ptr noundef, ptr, i32) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @StringBuilder__writeChar__(ptr noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local { ptr, i32 } @StringBuilder__toString__(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @CGenerator__writeByteHexValue__(ptr noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local { ptr, i32 } @CGenerator__writeUnescapedString__(ptr noundef %0, ptr nocapture readonly %1, i32 %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %2, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %2 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %22
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %22 ]
  %5 = getelementptr inbounds i8, ptr %1, i64 %indvars.iv
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %7 = zext i8 %6 to i32
  switch i8 %6, label %14 [
    i8 34, label %8
    i8 92, label %11
  ]

8:                                                ; preds = %.lr.ph
  %9 = load ptr, ptr @mu_____string393, align 8
  %10 = load i32, ptr getelementptr inbounds (i8, ptr @mu_____string393, i64 8), align 8
  tail call void @StringBuilder__write__(ptr noundef %0, ptr %9, i32 %10)
  br label %22

11:                                               ; preds = %.lr.ph
  %12 = load ptr, ptr @mu_____string394, align 8
  %13 = load i32, ptr getelementptr inbounds (i8, ptr @mu_____string394, i64 8), align 8
  tail call void @StringBuilder__write__(ptr noundef %0, ptr %12, i32 %13)
  br label %22

14:                                               ; preds = %.lr.ph
  %15 = add i8 %6, -127
  %narrow = icmp ult i8 %15, -95
  br i1 %narrow, label %17, label %16

16:                                               ; preds = %14
  tail call void @StringBuilder__writeChar__(ptr noundef %0, i8 noundef signext %6)
  br label %22

17:                                               ; preds = %14
  %18 = load ptr, ptr @mu_____string395, align 8
  %19 = load i32, ptr getelementptr inbounds (i8, ptr @mu_____string395, i64 8), align 8
  tail call void @StringBuilder__write__(ptr noundef %0, ptr %18, i32 %19)
  tail call void @CGenerator__writeByteHexValue__(ptr noundef %0, i32 noundef %7)
  %20 = load ptr, ptr @mu_____string397, align 8
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @mu_____string397, i64 8), align 8
  tail call void @StringBuilder__write__(ptr noundef %0, ptr %20, i32 %21)
  br label %22

22:                                               ; preds = %16, %17, %11, %8
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %22, %3
  %23 = tail call { ptr, i32 } @StringBuilder__toString__(ptr noundef %0)
  ret { ptr, i32 } %23
}

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


; Effective O3 passes: ConstraintEliminationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass