; LLVM IR from Stephane-D/SGDK :: src/ext (console)

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

@m_vsprintf_func = external hidden global ptr, align 8
@m_vsnprintf_func = external hidden global ptr, align 8
@m_consoleLineBufferSize = external hidden global i16, align 2

; 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
declare void @llvm.va_start.p0(ptr) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #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
define dso_local i32 @CON_write(ptr noundef %0, ...) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %6 = call ptr @consoleGetLineBuffer()
  store ptr %6, ptr %4, align 8, !tbaa !5
  %7 = load ptr, ptr %4, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  br i1 %8, label %9, label %38

9:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.va_start.p0(ptr %10)
  %11 = load ptr, ptr @m_vsnprintf_func, align 8, !tbaa !5
  %12 = icmp ne ptr %11, null
  br i1 %12, label %13, label %21

13:                                               ; preds = %9
  %14 = load ptr, ptr @m_vsnprintf_func, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = load i16, ptr @m_consoleLineBufferSize, align 2, !tbaa !11
  %17 = zext i16 %16 to i32
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = call i32 %14(ptr noundef %15, i32 noundef %17, ptr noundef %18, ptr noundef %19)
  store i32 %20, ptr %3, align 4, !tbaa !9
  br label %31

21:                                               ; preds = %9
  %22 = load ptr, ptr @m_vsprintf_func, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %30

24:                                               ; preds = %21
  %25 = load ptr, ptr @m_vsprintf_func, align 8, !tbaa !5
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = call i32 %25(ptr noundef %26, ptr noundef %27, ptr noundef %28)
  store i32 %29, ptr %3, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %24, %21
  br label %31

31:                                               ; preds = %30, %13
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.va_end.p0(ptr %32)
  %33 = load i32, ptr %3, align 4, !tbaa !9
  %34 = icmp ne i32 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %31
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  call void @consolePrint(ptr noundef %36)
  call void @consoleUploadFrameBuffer()
  br label %37

37:                                               ; preds = %35, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  br label %38

38:                                               ; preds = %37, %1
  %39 = load i32, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  ret i32 %39
}

; Function Attrs: nounwind uwtable
declare hidden void @consolePrint(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @consoleUploadFrameBuffer() #2

; Function Attrs: nounwind uwtable
declare hidden ptr @consoleGetLineBuffer() #2

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

@m_vsprintf_func = external hidden local_unnamed_addr global ptr, align 8
@m_vsnprintf_func = external hidden local_unnamed_addr global ptr, align 8
@m_consoleLineBufferSize = external hidden local_unnamed_addr global i16, align 2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_start.p0(ptr) #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.va_end.p0(ptr) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @CON_write(ptr noundef %0, ...) local_unnamed_addr #1 {
  %2 = tail call ptr @consoleGetLineBuffer()
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %15, label %3

3:                                                ; preds = %1
  tail call void @llvm.va_start.p0(ptr undef)
  %4 = load ptr, ptr @m_vsnprintf_func, align 8, !tbaa !5
  %.not14 = icmp eq ptr %4, null
  br i1 %.not14, label %9, label %5

5:                                                ; preds = %3
  %6 = load i16, ptr @m_consoleLineBufferSize, align 2, !tbaa !9
  %7 = zext i16 %6 to i32
  %8 = tail call i32 %4(ptr noundef nonnull %2, i32 noundef %7, ptr noundef %0, ptr noundef undef) #2
  br label %13

9:                                                ; preds = %3
  %10 = load ptr, ptr @m_vsprintf_func, align 8, !tbaa !5
  %.not15 = icmp eq ptr %10, null
  br i1 %.not15, label %.thread, label %11

.thread:                                          ; preds = %9
  tail call void @llvm.va_end.p0(ptr undef)
  br label %15

11:                                               ; preds = %9
  %12 = tail call i32 %10(ptr noundef nonnull %2, ptr noundef %0, ptr noundef undef) #2
  br label %13

13:                                               ; preds = %11, %5
  %.0 = phi i32 [ %8, %5 ], [ %12, %11 ]
  tail call void @llvm.va_end.p0(ptr undef)
  %.not16 = icmp eq i32 %.0, 0
  br i1 %.not16, label %15, label %14

14:                                               ; preds = %13
  tail call void @consolePrint(ptr noundef nonnull %2)
  tail call void @consoleUploadFrameBuffer()
  br label %15

15:                                               ; preds = %.thread, %13, %14, %1
  %.1 = phi i32 [ 0, %1 ], [ %.0, %14 ], [ 0, %13 ], [ 0, %.thread ]
  ret i32 %.1
}

; Function Attrs: nounwind uwtable
declare hidden void @consolePrint(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @consoleUploadFrameBuffer() local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden ptr @consoleGetLineBuffer() local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,SROAPass,SimplifyCFGPass,TailCallElimPass