; LLVM IR from xboot/libcg :: src (cg)

; 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.cg_ctx_t = type { ptr, ptr, ptr, ptr, ptr, %struct.cg_rect_t, ptr, i64 }
%struct.cg_rect_t = type { double, double, double, double }
%struct.cg_state_t = type { ptr, %struct.cg_paint_t, %struct.cg_matrix_t, i32, %struct.cg_stroke_data_t, i32, double, ptr }
%struct.cg_paint_t = type { i32, %struct.cg_color_t, %struct.cg_gradient_t, %struct.cg_texture_t }
%struct.cg_color_t = type { double, double, double, double }
%struct.cg_gradient_t = type { i32, i32, %struct.cg_matrix_t, [6 x double], double, %struct.anon }
%struct.anon = type { ptr, i32, i32 }
%struct.cg_texture_t = type { i32, ptr, %struct.cg_matrix_t, double }
%struct.cg_matrix_t = type { double, double, double, double, double, double }
%struct.cg_stroke_data_t = type { double, double, i32, i32, ptr }

; 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 @cg_surface_destroy(ptr noundef) #1

; Function Attrs: nounwind
declare void @free(ptr noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @cg_destroy(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = icmp ne ptr %4, null
  br i1 %5, label %6, label %45

6:                                                ; preds = %1
  br label %7

7:                                                ; preds = %12, %6
  %8 = load ptr, ptr %2, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.cg_ctx_t, ptr %8, i32 0, i32 1
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %22

12:                                               ; preds = %7
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.cg_ctx_t, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !9
  store ptr %15, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.cg_state_t, ptr %16, i32 0, i32 7
  %18 = load ptr, ptr %17, align 8, !tbaa !14
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.cg_ctx_t, ptr %19, i32 0, i32 1
  store ptr %18, ptr %20, align 8, !tbaa !9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  call void @cg_state_destroy(ptr noundef %21)
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  br label %7, !llvm.loop !24

22:                                               ; preds = %7
  %23 = load ptr, ptr %2, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.cg_ctx_t, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !26
  call void @cg_surface_destroy(ptr noundef %25)
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.cg_ctx_t, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !27
  call void @cg_path_destroy(ptr noundef %28)
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.cg_ctx_t, ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !28
  call void @cg_rle_destroy(ptr noundef %31)
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.cg_ctx_t, ptr %32, i32 0, i32 4
  %34 = load ptr, ptr %33, align 8, !tbaa !29
  call void @cg_rle_destroy(ptr noundef %34)
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.cg_ctx_t, ptr %35, i32 0, i32 6
  %37 = load ptr, ptr %36, align 8, !tbaa !30
  %38 = icmp ne ptr %37, null
  br i1 %38, label %39, label %43

39:                                               ; preds = %22
  %40 = load ptr, ptr %2, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.cg_ctx_t, ptr %40, i32 0, i32 6
  %42 = load ptr, ptr %41, align 8, !tbaa !30
  call void @free(ptr noundef %42) #3
  br label %43

43:                                               ; preds = %39, %22
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  call void @free(ptr noundef %44) #3
  br label %45

45:                                               ; preds = %43, %1
  ret void
}

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

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

; Function Attrs: nounwind uwtable
declare hidden void @cg_rle_destroy(ptr 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 "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, !6, i64 8}
!10 = !{!"cg_ctx_t", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !11, i64 40, !6, i64 72, !13, i64 80}
!11 = !{!"cg_rect_t", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!12 = !{!"double", !7, i64 0}
!13 = !{!"long", !7, i64 0}
!14 = !{!15, !6, i64 352}
!15 = !{!"cg_state_t", !6, i64 0, !16, i64 8, !20, i64 248, !17, i64 296, !23, i64 304, !17, i64 336, !12, i64 344, !6, i64 352}
!16 = !{!"cg_paint_t", !17, i64 0, !18, i64 8, !19, i64 40, !22, i64 168}
!17 = !{!"int", !7, i64 0}
!18 = !{!"cg_color_t", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24}
!19 = !{!"cg_gradient_t", !17, i64 0, !17, i64 4, !20, i64 8, !7, i64 56, !12, i64 104, !21, i64 112}
!20 = !{!"cg_matrix_t", !12, i64 0, !12, i64 8, !12, i64 16, !12, i64 24, !12, i64 32, !12, i64 40}
!21 = !{!"", !6, i64 0, !17, i64 8, !17, i64 12}
!22 = !{!"cg_texture_t", !17, i64 0, !6, i64 8, !20, i64 16, !12, i64 64}
!23 = !{!"cg_stroke_data_t", !12, i64 0, !12, i64 8, !17, i64 16, !17, i64 20, !6, i64 24}
!24 = distinct !{!24, !25}
!25 = !{!"llvm.loop.mustprogress"}
!26 = !{!10, !6, i64 0}
!27 = !{!10, !6, i64 16}
!28 = !{!10, !6, i64 24}
!29 = !{!10, !6, i64 32}
!30 = !{!10, !6, i64 72}


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

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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @cg_destroy(ptr noundef %0) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %19, label %.preheader

.preheader:                                       ; preds = %1
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not1416 = icmp eq ptr %3, null
  br i1 %.not1416, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %4 = phi ptr [ %7, %.lr.ph ], [ %3, %.preheader ]
  %5 = getelementptr inbounds i8, ptr %4, i64 352
  %6 = load ptr, ptr %5, align 8, !tbaa !13
  store ptr %6, ptr %2, align 8, !tbaa !5
  tail call void @cg_state_destroy(ptr noundef nonnull %4)
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %.not14 = icmp eq ptr %7, null
  br i1 %.not14, label %._crit_edge, label %.lr.ph, !llvm.loop !23

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %8 = load ptr, ptr %0, align 8, !tbaa !25
  tail call void @cg_surface_destroy(ptr noundef %8)
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !26
  tail call void @cg_path_destroy(ptr noundef %10)
  %11 = getelementptr inbounds i8, ptr %0, i64 24
  %12 = load ptr, ptr %11, align 8, !tbaa !27
  tail call void @cg_rle_destroy(ptr noundef %12)
  %13 = getelementptr inbounds i8, ptr %0, i64 32
  %14 = load ptr, ptr %13, align 8, !tbaa !28
  tail call void @cg_rle_destroy(ptr noundef %14)
  %15 = getelementptr inbounds i8, ptr %0, i64 72
  %16 = load ptr, ptr %15, align 8, !tbaa !29
  %.not15 = icmp eq ptr %16, null
  br i1 %.not15, label %18, label %17

17:                                               ; preds = %._crit_edge
  tail call void @free(ptr noundef nonnull %16) #2
  br label %18

18:                                               ; preds = %17, %._crit_edge
  tail call void @free(ptr noundef nonnull %0) #2
  br label %19

19:                                               ; preds = %18, %1
  ret void
}

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

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

; Function Attrs: nounwind uwtable
declare hidden void @cg_rle_destroy(ptr 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" }
attributes #1 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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, !7, i64 8}
!6 = !{!"cg_ctx_t", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !10, i64 40, !7, i64 72, !12, i64 80}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"cg_rect_t", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!11 = !{!"double", !8, i64 0}
!12 = !{!"long", !8, i64 0}
!13 = !{!14, !7, i64 352}
!14 = !{!"cg_state_t", !7, i64 0, !15, i64 8, !19, i64 248, !16, i64 296, !22, i64 304, !16, i64 336, !11, i64 344, !7, i64 352}
!15 = !{!"cg_paint_t", !16, i64 0, !17, i64 8, !18, i64 40, !21, i64 168}
!16 = !{!"int", !8, i64 0}
!17 = !{!"cg_color_t", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24}
!18 = !{!"cg_gradient_t", !16, i64 0, !16, i64 4, !19, i64 8, !8, i64 56, !11, i64 104, !20, i64 112}
!19 = !{!"cg_matrix_t", !11, i64 0, !11, i64 8, !11, i64 16, !11, i64 24, !11, i64 32, !11, i64 40}
!20 = !{!"", !7, i64 0, !16, i64 8, !16, i64 12}
!21 = !{!"cg_texture_t", !16, i64 0, !7, i64 8, !19, i64 16, !11, i64 64}
!22 = !{!"cg_stroke_data_t", !11, i64 0, !11, i64 8, !16, i64 16, !16, i64 20, !7, i64 24}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}
!25 = !{!6, !7, i64 0}
!26 = !{!6, !7, i64 16}
!27 = !{!6, !7, i64 24}
!28 = !{!6, !7, i64 32}
!29 = !{!6, !7, i64 72}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass