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

; 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.2 = external hidden unnamed_addr constant [5 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @_format_parse(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @_format_printtobuffer(i64 noundef, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i1 @format_printtobuffer(i64 noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca i1, align 1
  %5 = alloca i64, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca %struct.fformat, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  store ptr @.str.2, ptr %8, 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 %15, label %16

15:                                               ; preds = %3
  store ptr @.str.3, ptr %8, align 8, !tbaa !9
  br label %16

16:                                               ; preds = %15, %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %17 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %17, ptr %9, align 8, !tbaa !9
  br label %18

18:                                               ; preds = %50, %16
  %19 = load ptr, ptr %9, align 8, !tbaa !9
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %18
  store i32 2, ptr %10, align 4
  br label %51

24:                                               ; preds = %18
  %25 = load ptr, ptr %9, align 8, !tbaa !9
  %26 = load i8, ptr %25, align 1, !tbaa !11
  %27 = sext i8 %26 to i32
  %28 = icmp ne i32 %27, 37
  br i1 %28, label %29, label %36

29:                                               ; preds = %24
  %30 = load ptr, ptr %7, align 8, !tbaa !9
  %31 = load ptr, ptr %9, align 8, !tbaa !9
  %32 = load i8, ptr %31, align 1, !tbaa !11
  %33 = call i32 @varray_charwrite(ptr noundef %30, i8 noundef signext %32)
  %34 = load ptr, ptr %9, align 8, !tbaa !9
  %35 = getelementptr inbounds i8, ptr %34, i32 1
  store ptr %35, ptr %9, align 8, !tbaa !9
  br label %50

36:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 12, ptr %11) #3
  %37 = load ptr, ptr %9, align 8, !tbaa !9
  %38 = load ptr, ptr %8, align 8, !tbaa !9
  %39 = call zeroext i1 @_format_parse(ptr noundef %37, ptr noundef %38, ptr noundef %9, ptr noundef %11)
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  store i1 false, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %47

41:                                               ; preds = %36
  %42 = load i64, ptr %5, align 8, !tbaa !5
  %43 = load ptr, ptr %7, align 8, !tbaa !9
  %44 = call zeroext i1 @_format_printtobuffer(i64 noundef %42, ptr noundef %11, ptr noundef %43)
  br i1 %44, label %46, label %45

45:                                               ; preds = %41
  store i1 false, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %47

46:                                               ; preds = %41
  store i32 0, ptr %10, align 4
  br label %47

47:                                               ; preds = %46, %45, %40
  call void @llvm.lifetime.end.p0(i64 12, ptr %11) #3
  %48 = load i32, ptr %10, align 4
  switch i32 %48, label %51 [
    i32 0, label %49
  ]

49:                                               ; preds = %47
  br label %50

50:                                               ; preds = %49, %29
  br label %18, !llvm.loop !12

51:                                               ; preds = %47, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  %52 = load i32, ptr %10, align 4
  switch i32 %52, label %54 [
    i32 2, label %53
  ]

53:                                               ; preds = %51
  store i1 true, ptr %4, align 1
  store i32 1, ptr %10, align 4
  br label %54

54:                                               ; preds = %53, %51
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %55 = load i1, ptr %4, align 1
  ret i1 %55
}

declare i32 @varray_charwrite(ptr noundef, i8 noundef signext) #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 = { "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 = !{!"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 = distinct !{!12, !13}
!13 = !{!"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.fformat = type { i32, i32, i8 }

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

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @_format_parse(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @_format_printtobuffer(i64 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef zeroext i1 @format_printtobuffer(i64 noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca ptr, align 8
  %5 = alloca %struct.fformat, align 4
  %6 = and i64 %0, 9222246136947933184
  %.not = icmp eq i64 %6, 9222246136947933184
  %spec.store.select = select i1 %.not, ptr @.str.2, ptr @.str.3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  store ptr %1, ptr %4, align 8, !tbaa !5
  %7 = load i8, ptr %1, align 1, !tbaa !9
  %.not1220 = icmp eq i8 %7, 0
  br i1 %.not1220, label %.critedge15, label %.lr.ph

.lr.ph:                                           ; preds = %3, %18
  %8 = phi i8 [ %20, %18 ], [ %7, %3 ]
  %9 = phi ptr [ %19, %18 ], [ %1, %3 ]
  %.not13 = icmp eq i8 %8, 37
  br i1 %.not13, label %14, label %10

10:                                               ; preds = %.lr.ph
  %11 = call i32 @varray_charwrite(ptr noundef %2, i8 noundef signext %8) #3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 1
  store ptr %13, ptr %4, align 8, !tbaa !5
  br label %18

14:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %5) #3
  %15 = call zeroext i1 @_format_parse(ptr noundef nonnull %9, ptr noundef nonnull %spec.store.select, ptr noundef nonnull %4, ptr noundef nonnull %5)
  br i1 %15, label %16, label %.critedge

16:                                               ; preds = %14
  %17 = call zeroext i1 @_format_printtobuffer(i64 noundef %0, ptr noundef nonnull %5, ptr noundef %2)
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %5) #3
  br i1 %17, label %._crit_edge, label %.loopexit

._crit_edge:                                      ; preds = %16
  %.pre = load ptr, ptr %4, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %._crit_edge, %10
  %19 = phi ptr [ %.pre, %._crit_edge ], [ %13, %10 ]
  %20 = load i8, ptr %19, align 1, !tbaa !9
  %.not12 = icmp eq i8 %20, 0
  br i1 %.not12, label %.critedge15, label %.lr.ph, !llvm.loop !10

.critedge:                                        ; preds = %14
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %5) #3
  br label %.loopexit

.loopexit:                                        ; preds = %16, %.critedge
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  br label %21

.critedge15:                                      ; preds = %18, %3
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  br label %21

21:                                               ; preds = %.loopexit, %.critedge15
  %.not1218 = phi i1 [ false, %.loopexit ], [ true, %.critedge15 ]
  ret i1 %.not1218
}

declare i32 @varray_charwrite(ptr noundef, i8 noundef signext) 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 = { "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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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