; LLVM IR from wojciech-graj/TermGL :: termgl (tgl_puts)

; 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.TGLPixFmt = type { %struct.TGLFmt, %struct.TGLFmt }
%struct.TGLFmt = type { i8, %union.TGLFmtColor }
%union.TGLFmtColor = type { %struct.TGLRGB }
%struct.TGLRGB = type { i8, i8, i8 }
%struct.Pixel = type { i8, %struct.TGLPixFmt }
%struct.TGL = type { i32, i32, i32, i32, i32, ptr, ptr, ptr, i32, i8, i8 }

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #2

; Function Attrs: inlinehint nounwind uwtable
declare hidden void @itgl_clip(ptr noundef, ptr noundef, ptr noundef) #3

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

14:                                               ; preds = %53, %27, %5
  %15 = load ptr, ptr %10, align 8, !tbaa !5
  %16 = load i8, ptr %15, align 1, !tbaa !11
  %17 = icmp ne i8 %16, 0
  br i1 %17, label %18, label %58

18:                                               ; preds = %14
  %19 = load ptr, ptr %10, align 8, !tbaa !5
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp eq i32 %21, 10
  %23 = zext i1 %22 to i32
  %24 = sext i32 %23 to i64
  %25 = call i64 @llvm.expect.i64(i64 %24, i64 0)
  %26 = icmp ne i64 %25, 0
  br i1 %26, label %27, label %33

27:                                               ; preds = %18
  %28 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %28, ptr %11, align 4, !tbaa !9
  %29 = load i32, ptr %9, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %9, align 4, !tbaa !9
  %31 = load ptr, ptr %10, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i32 1
  store ptr %32, ptr %10, align 8, !tbaa !5
  br label %14, !llvm.loop !12

33:                                               ; preds = %18
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  call void @itgl_clip(ptr noundef %34, ptr noundef %11, ptr noundef %9)
  br label %35

35:                                               ; preds = %33
  %36 = load ptr, ptr %7, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.TGL, ptr %36, i32 0, i32 5
  %38 = load ptr, ptr %37, align 8, !tbaa !14
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = load ptr, ptr %7, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.TGL, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !17
  %43 = mul i32 %39, %42
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = add i32 %43, %44
  %46 = zext i32 %45 to i64
  %47 = getelementptr inbounds %struct.Pixel, ptr %38, i64 %46
  %48 = getelementptr inbounds %struct.Pixel, ptr %12, i32 0, i32 0
  %49 = load ptr, ptr %10, align 8, !tbaa !5
  %50 = load i8, ptr %49, align 1, !tbaa !11
  store i8 %50, ptr %48, align 1, !tbaa !18
  %51 = getelementptr inbounds %struct.Pixel, ptr %12, i32 0, i32 1
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %51, ptr align 1 %6, i64 8, i1 false), !tbaa.struct !22
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %47, ptr align 1 %12, i64 9, i1 false), !tbaa.struct !23
  br label %52

52:                                               ; preds = %35
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %11, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %11, align 4, !tbaa !9
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds i8, ptr %56, i32 1
  store ptr %57, ptr %10, align 8, !tbaa !5
  br label %14, !llvm.loop !12

58:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #5
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(none) }
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 = { 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 #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 = !{!"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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 24}
!15 = !{!"TGL", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !6, i64 24, !6, i64 32, !6, i64 40, !10, i64 48, !16, i64 52, !7, i64 53}
!16 = !{!"_Bool", !7, i64 0}
!17 = !{!15, !10, i64 0}
!18 = !{!19, !7, i64 0}
!19 = !{!"Pixel", !7, i64 0, !20, i64 1}
!20 = !{!"TGLPixFmt", !21, i64 0, !21, i64 4}
!21 = !{!"TGLFmt", !7, i64 0, !7, i64 1}
!22 = !{i64 0, i64 1, !11, i64 1, i64 3, !11, i64 4, i64 1, !11, i64 5, i64 3, !11}
!23 = !{i64 0, i64 1, !11, i64 1, i64 1, !11, i64 2, i64 3, !11, i64 5, i64 1, !11, i64 6, i64 3, !11}


; 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.Pixel = type { i8, %struct.TGLPixFmt }
%struct.TGLPixFmt = type { %struct.TGLFmt, %struct.TGLFmt }
%struct.TGLFmt = type { i8, %union.TGLFmtColor }
%union.TGLFmtColor = type { %struct.TGLRGB }
%struct.TGLRGB = type { i8, i8, i8 }

; 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: inlinehint nounwind uwtable
declare hidden void @itgl_clip(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @tgl_puts(ptr noundef %0, i32 noundef %1, i32 noundef %2, ptr nocapture noundef readonly %3, i64 %4) local_unnamed_addr #2 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %2, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %7) #3
  store i32 %1, ptr %7, align 4, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %0, i64 24
  br label %9

9:                                                ; preds = %.backedge, %5
  %.0 = phi ptr [ %3, %5 ], [ %.0.be, %.backedge ]
  %10 = load i8, ptr %.0, align 1, !tbaa !9
  switch i8 %10, label %14 [
    i8 0, label %26
    i8 10, label %11
  ], !prof !10

11:                                               ; preds = %9
  store i32 %1, ptr %7, align 4, !tbaa !5
  %12 = load i32, ptr %6, align 4, !tbaa !5
  %13 = add nsw i32 %12, 1
  store i32 %13, ptr %6, align 4, !tbaa !5
  br label %.backedge

.backedge:                                        ; preds = %11, %14
  %.0.be = getelementptr inbounds i8, ptr %.0, i64 1
  br label %9, !llvm.loop !11

14:                                               ; preds = %9
  call void @itgl_clip(ptr noundef %0, ptr noundef nonnull %7, ptr noundef nonnull %6)
  %15 = load ptr, ptr %8, align 8, !tbaa !13
  %16 = load i32, ptr %6, align 4, !tbaa !5
  %17 = load i32, ptr %0, align 8, !tbaa !17
  %18 = mul i32 %17, %16
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = add i32 %18, %19
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds %struct.Pixel, ptr %15, i64 %21
  %23 = load i8, ptr %.0, align 1, !tbaa !9
  store i8 %23, ptr %22, align 1, !tbaa !9
  %.sroa.2.0..sroa_idx = getelementptr inbounds i8, ptr %22, i64 1
  store i64 %4, ptr %.sroa.2.0..sroa_idx, align 1
  %24 = load i32, ptr %7, align 4, !tbaa !5
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %7, align 4, !tbaa !5
  br label %.backedge

26:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %7) #3
  ret void
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!"branch_weights", i32 2000, i32 2001, i32 1}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !15, i64 24}
!14 = !{!"TGL", !6, i64 0, !6, i64 4, !6, i64 8, !6, i64 12, !6, i64 16, !15, i64 24, !15, i64 32, !15, i64 40, !6, i64 48, !16, i64 52, !7, i64 53}
!15 = !{!"any pointer", !7, i64 0}
!16 = !{!"_Bool", !7, i64 0}
!17 = !{!14, !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopSimplifyPass,LowerExpectIntrinsicPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass