; LLVM IR from nickmqb/muon :: bootstrap/mu.ll_ulong (writeHexTo__)

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

@mu_____string1 = external dso_local global %struct.string__, 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 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) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @ulong__writeHexTo__(i64 noundef %0, ptr noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !9
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = icmp eq i64 %7, 0
  br i1 %8, label %9, label %13

9:                                                ; preds = %2
  %10 = load ptr, ptr %4, align 8, !tbaa !9
  %11 = load ptr, ptr @mu_____string1, align 8
  %12 = load i32, ptr getelementptr inbounds ({ ptr, i32 }, ptr @mu_____string1, i32 0, i32 1), align 8
  call void @StringBuilder__write__(ptr noundef %10, ptr %11, i32 %12)
  br label %49

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %14 = load ptr, ptr %4, align 8, !tbaa !9
  %15 = getelementptr inbounds %struct.tag_____StringBuilder__, ptr %14, i32 0, i32 1
  %16 = load i32, ptr %15, align 8, !tbaa !11
  store i32 %16, ptr %5, align 4, !tbaa !14
  br label %17

17:                                               ; preds = %40, %13
  br label %18

18:                                               ; preds = %17
  %19 = load i64, ptr %3, align 8, !tbaa !5
  %20 = icmp ugt i64 %19, 0
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  br label %43

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = and i64 %23, 15
  store i64 %24, ptr %6, align 8, !tbaa !5
  %25 = load i64, ptr %6, align 8, !tbaa !5
  %26 = icmp ult i64 %25, 10
  br i1 %26, label %27, label %32

27:                                               ; preds = %22
  %28 = load ptr, ptr %4, align 8, !tbaa !9
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = add i64 48, %29
  %31 = trunc i64 %30 to i8
  call void @StringBuilder__writeChar__(ptr noundef %28, i8 noundef signext %31)
  br label %40

32:                                               ; preds = %22
  %33 = load ptr, ptr %4, align 8, !tbaa !9
  %34 = load i64, ptr %6, align 8, !tbaa !5
  %35 = add i64 97, %34
  %36 = trunc i64 %35 to i8
  %37 = sext i8 %36 to i32
  %38 = sub nsw i32 %37, 10
  %39 = trunc i32 %38 to i8
  call void @StringBuilder__writeChar__(ptr noundef %33, i8 noundef signext %39)
  br label %40

40:                                               ; preds = %32, %27
  %41 = load i64, ptr %3, align 8, !tbaa !5
  %42 = lshr i64 %41, 4
  store i64 %42, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %17

43:                                               ; preds = %21
  %44 = load ptr, ptr %4, align 8, !tbaa !9
  %45 = load i32, ptr %5, align 4, !tbaa !14
  %46 = load ptr, ptr %4, align 8, !tbaa !9
  %47 = getelementptr inbounds %struct.tag_____StringBuilder__, ptr %46, i32 0, i32 1
  %48 = load i32, ptr %47, align 8, !tbaa !11
  call void @StringBuilder__reverseSlice__(ptr noundef %44, i32 noundef %45, i32 noundef %48)
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %49

49:                                               ; preds = %43, %9
  ret void
}

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

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 }

!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 = !{!"long", !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, !13, i64 8}
!12 = !{!"tag_____StringBuilder__", !10, i64 0, !13, i64 8, !13, i64 12}
!13 = !{!"int", !7, i64 0}
!14 = !{!13, !13, i64 0}


; 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_____string1 = 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
define dso_local void @ulong__writeHexTo__(i64 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i64 %0, 0
  br i1 %3, label %4, label %7

4:                                                ; preds = %2
  %5 = load ptr, ptr @mu_____string1, align 8
  %6 = load i32, ptr getelementptr inbounds (i8, ptr @mu_____string1, i64 8), align 8
  tail call void @StringBuilder__write__(ptr noundef %1, ptr %5, i32 %6)
  br label %19

7:                                                ; preds = %2
  %8 = getelementptr inbounds i8, ptr %1, i64 8
  %9 = load i32, ptr %8, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %7, %10
  %.015 = phi i64 [ %0, %7 ], [ %16, %10 ]
  %11 = and i64 %.015, 15
  %12 = icmp ult i64 %11, 10
  %13 = trunc nuw nsw i64 %11 to i8
  %14 = add nuw nsw i8 %13, 87
  %15 = or disjoint i8 %13, 48
  %.sink = select i1 %12, i8 %15, i8 %14
  tail call void @StringBuilder__writeChar__(ptr noundef %1, i8 noundef signext %.sink)
  %16 = lshr i64 %.015, 4
  %.not = icmp ult i64 %.015, 16
  br i1 %.not, label %17, label %10

17:                                               ; preds = %10
  %18 = load i32, ptr %8, align 8, !tbaa !5
  tail call void @StringBuilder__reverseSlice__(ptr noundef %1, i32 noundef %9, i32 noundef %18)
  br label %19

19:                                               ; preds = %17, %4
  ret void
}

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

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, !10, i64 8}
!6 = !{!"tag_____StringBuilder__", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,SROAPass,SimplifyCFGPass,TailCallElimPass