; LLVM IR from Morpho-lang/morpho :: src/support/format.ll (format_printtobuffer)

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

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 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 uwtable
define dso_local zeroext i1 @_format_printtobuffer(i64 noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca i1, align 1
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca [255 x i8], align 16
  %9 = alloca [255 x i8], align 16
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  store i64 %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  %12 = load i64, ptr %5, align 8, !tbaa !5
  %13 = and i64 %12, 9222246136947933184
  %14 = icmp ne i64 %13, 9222246136947933184
  br i1 %14, label %20, label %15

15:                                               ; preds = %3
  %16 = load i64, ptr %5, align 8, !tbaa !5
  %17 = and i64 %16, -703687441776640
  %18 = icmp eq i64 %17, 9222668349412999168
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  store i1 false, ptr %4, align 1
  br label %93

20:                                               ; preds = %15, %3
  call void @llvm.lifetime.start.p0(i64 255, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 255, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %21 = getelementptr inbounds [255 x i8], ptr %8, i64 0, i64 0
  store ptr %21, ptr %10, align 8, !tbaa !9
  %22 = load ptr, ptr %10, align 8, !tbaa !9
  store i8 37, ptr %22, align 1, !tbaa !11
  %23 = load ptr, ptr %10, align 8, !tbaa !9
  %24 = getelementptr inbounds i8, ptr %23, i32 1
  store ptr %24, ptr %10, align 8, !tbaa !9
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = getelementptr inbounds %struct.fformat, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 4, !tbaa !12
  %28 = icmp sge i32 %27, 0
  br i1 %28, label %29, label %44

29:                                               ; preds = %20
  %30 = load ptr, ptr %10, align 8, !tbaa !9
  %31 = getelementptr inbounds [255 x i8], ptr %8, i64 0, i64 0
  %32 = getelementptr inbounds i8, ptr %31, i64 255
  %33 = load ptr, ptr %10, align 8, !tbaa !9
  %34 = ptrtoint ptr %32 to i64
  %35 = ptrtoint ptr %33 to i64
  %36 = sub i64 %34, %35
  %37 = load ptr, ptr %6, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.fformat, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 4, !tbaa !12
  %40 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %30, i64 noundef %36, ptr noundef @.str, i32 noundef %39) #5
  %41 = load ptr, ptr %10, align 8, !tbaa !9
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds i8, ptr %41, i64 %42
  store ptr %43, ptr %10, align 8, !tbaa !9
  br label %44

44:                                               ; preds = %29, %20
  %45 = load ptr, ptr %6, align 8, !tbaa !9
  %46 = getelementptr inbounds %struct.fformat, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 4, !tbaa !15
  %48 = icmp sge i32 %47, 0
  br i1 %48, label %49, label %64

49:                                               ; preds = %44
  %50 = load ptr, ptr %10, align 8, !tbaa !9
  %51 = getelementptr inbounds [255 x i8], ptr %8, i64 0, i64 0
  %52 = getelementptr inbounds i8, ptr %51, i64 255
  %53 = load ptr, ptr %10, align 8, !tbaa !9
  %54 = ptrtoint ptr %52 to i64
  %55 = ptrtoint ptr %53 to i64
  %56 = sub i64 %54, %55
  %57 = load ptr, ptr %6, align 8, !tbaa !9
  %58 = getelementptr inbounds %struct.fformat, ptr %57, i32 0, i32 1
  %59 = load i32, ptr %58, align 4, !tbaa !15
  %60 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %50, i64 noundef %56, ptr noundef @.str.1, i32 noundef %59) #5
  %61 = load ptr, ptr %10, align 8, !tbaa !9
  %62 = sext i32 %60 to i64
  %63 = getelementptr inbounds i8, ptr %61, i64 %62
  store ptr %63, ptr %10, align 8, !tbaa !9
  br label %64

64:                                               ; preds = %49, %44
  %65 = load ptr, ptr %6, align 8, !tbaa !9
  %66 = getelementptr inbounds %struct.fformat, ptr %65, i32 0, i32 2
  %67 = load i8, ptr %66, align 4, !tbaa !16
  %68 = load ptr, ptr %10, align 8, !tbaa !9
  store i8 %67, ptr %68, align 1, !tbaa !11
  %69 = load ptr, ptr %10, align 8, !tbaa !9
  %70 = getelementptr inbounds i8, ptr %69, i32 1
  store ptr %70, ptr %10, align 8, !tbaa !9
  %71 = load ptr, ptr %10, align 8, !tbaa !9
  store i8 0, ptr %71, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #5
  store i32 0, ptr %11, align 4, !tbaa !17
  %72 = load i64, ptr %5, align 8, !tbaa !5
  %73 = and i64 %72, 9222246136947933184
  %74 = icmp ne i64 %73, 9222246136947933184
  br i1 %74, label %75, label %81

75:                                               ; preds = %64
  %76 = getelementptr inbounds [255 x i8], ptr %9, i64 0, i64 0
  %77 = getelementptr inbounds [255 x i8], ptr %8, i64 0, i64 0
  %78 = load i64, ptr %5, align 8, !tbaa !5
  %79 = call double @valuetodouble(i64 noundef %78)
  %80 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %76, i64 noundef 255, ptr noundef %77, double noundef %79) #5
  store i32 %80, ptr %11, align 4, !tbaa !17
  br label %88

81:                                               ; preds = %64
  %82 = getelementptr inbounds [255 x i8], ptr %9, i64 0, i64 0
  %83 = getelementptr inbounds [255 x i8], ptr %8, i64 0, i64 0
  %84 = load i64, ptr %5, align 8, !tbaa !5
  %85 = and i64 %84, 4294967295
  %86 = trunc i64 %85 to i32
  %87 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %82, i64 noundef 255, ptr noundef %83, i32 noundef %86) #5
  store i32 %87, ptr %11, align 4, !tbaa !17
  br label %88

88:                                               ; preds = %81, %75
  %89 = load ptr, ptr %7, align 8, !tbaa !9
  %90 = getelementptr inbounds [255 x i8], ptr %9, i64 0, i64 0
  %91 = load i32, ptr %11, align 4, !tbaa !17
  %92 = call zeroext i1 @varray_charadd(ptr noundef %89, ptr noundef %90, i32 noundef %91)
  store i1 true, ptr %4, align 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 255, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 255, ptr %8) #5
  br label %93

93:                                               ; preds = %88, %19
  %94 = load i1, ptr %4, align 1
  ret i1 %94
}

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

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

declare zeroext i1 @varray_charadd(ptr noundef, ptr noundef, i32 noundef) #4

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 "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 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 = { "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 = { 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 = !{!7, !7, i64 0}
!12 = !{!13, !14, i64 0}
!13 = !{!"fformat", !14, i64 0, !14, i64 4, !7, i64 8}
!14 = !{!"int", !7, i64 0}
!15 = !{!13, !14, i64 4}
!16 = !{!13, !7, i64 8}
!17 = !{!14, !14, 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"

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @_format_printtobuffer(i64 noundef %0, ptr nocapture noundef readonly %1, ptr noundef %2) local_unnamed_addr #1 {
  %4 = alloca [255 x i8], align 16
  %5 = alloca [255 x i8], align 16
  %6 = and i64 %0, 9222246136947933184
  %.not = icmp ne i64 %6, 9222246136947933184
  %7 = and i64 %0, -703687441776640
  %8 = icmp eq i64 %7, 9222668349412999168
  %or.cond = or i1 %.not, %8
  br i1 %or.cond, label %9, label %41

9:                                                ; preds = %3
  call void @llvm.lifetime.start.p0(i64 255, ptr nonnull %4) #5
  call void @llvm.lifetime.start.p0(i64 255, ptr nonnull %5) #5
  store i8 37, ptr %4, align 16, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %4, i64 1
  %11 = load i32, ptr %1, align 4, !tbaa !8
  %12 = icmp sgt i32 %11, -1
  br i1 %12, label %13, label %17

13:                                               ; preds = %9
  %14 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %10, i64 noundef 254, ptr noundef nonnull @.str, i32 noundef %11) #5
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds i8, ptr %10, i64 %15
  br label %17

17:                                               ; preds = %13, %9
  %.023 = phi ptr [ %16, %13 ], [ %10, %9 ]
  %18 = getelementptr inbounds i8, ptr %1, i64 4
  %19 = load i32, ptr %18, align 4, !tbaa !11
  %20 = icmp sgt i32 %19, -1
  br i1 %20, label %21, label %29

21:                                               ; preds = %17
  %22 = getelementptr inbounds i8, ptr %4, i64 255
  %23 = ptrtoint ptr %22 to i64
  %24 = ptrtoint ptr %.023 to i64
  %25 = sub i64 %23, %24
  %26 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull %.023, i64 noundef %25, ptr noundef nonnull @.str.1, i32 noundef %19) #5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i8, ptr %.023, i64 %27
  br label %29

29:                                               ; preds = %21, %17
  %.1 = phi ptr [ %28, %21 ], [ %.023, %17 ]
  %30 = getelementptr inbounds i8, ptr %1, i64 8
  %31 = load i8, ptr %30, align 4, !tbaa !12
  store i8 %31, ptr %.1, align 1, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %.1, i64 1
  store i8 0, ptr %32, align 1, !tbaa !5
  br i1 %.not, label %33, label %36

33:                                               ; preds = %29
  %34 = call double @valuetodouble(i64 noundef %0)
  %35 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %5, i64 noundef 255, ptr noundef nonnull %4, double noundef %34) #5
  br label %39

36:                                               ; preds = %29
  %37 = trunc i64 %0 to i32
  %38 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %5, i64 noundef 255, ptr noundef nonnull %4, i32 noundef %37) #5
  br label %39

39:                                               ; preds = %36, %33
  %.0 = phi i32 [ %35, %33 ], [ %38, %36 ]
  %40 = call zeroext i1 @varray_charadd(ptr noundef %2, ptr noundef nonnull %5, i32 noundef %.0) #5
  call void @llvm.lifetime.end.p0(i64 255, ptr nonnull %5) #5
  call void @llvm.lifetime.end.p0(i64 255, ptr nonnull %4) #5
  br label %41

41:                                               ; preds = %3, %39
  ret i1 %or.cond
}

; Function Attrs: nofree nounwind
declare noundef i32 @snprintf(ptr noalias nocapture noundef writeonly, i64 noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

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

declare zeroext i1 @varray_charadd(ptr noundef, ptr noundef, i32 noundef) local_unnamed_addr #4

attributes #0 = { mustprogress 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 = { 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 #3 = { 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 #4 = { "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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 0}
!9 = !{!"fformat", !10, i64 0, !10, i64 4, !6, i64 8}
!10 = !{!"int", !6, i64 0}
!11 = !{!9, !10, i64 4}
!12 = !{!9, !6, i64 8}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass