; LLVM IR from fhanau/Efficient-Compression-Tool :: src/optipng (image)

; 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.opng_image = type { i32, i32, ptr, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr, ptr, %struct.png_color_16_struct, %struct.png_color_16_struct, ptr, ptr, %struct.png_color_8_struct, ptr }
%struct.png_color_16_struct = type { i8, i16, i16, i16, i16 }
%struct.png_color_8_struct = type { i8, i8, i8, i8, i8 }
%struct.png_unknown_chunk_t = type { [5 x i8], ptr, i64, i8 }

; Function Attrs: nounwind uwtable
define dso_local void @opng_clear_image(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = getelementptr inbounds %struct.opng_image, ptr %5, i32 0, i32 9
  %7 = load ptr, ptr %6, align 8, !tbaa !9
  %8 = icmp ne ptr %7, null
  br i1 %8, label %10, label %9

9:                                                ; preds = %1
  br label %65

10:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #3
  store i32 0, ptr %3, align 4, !tbaa !15
  br label %11

11:                                               ; preds = %26, %10
  %12 = load i32, ptr %3, align 4, !tbaa !15
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.opng_image, ptr %13, i32 0, i32 1
  %15 = load i32, ptr %14, align 4, !tbaa !16
  %16 = icmp ult i32 %12, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #3
  br label %29

18:                                               ; preds = %11
  %19 = load ptr, ptr %2, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.opng_image, ptr %19, i32 0, i32 9
  %21 = load ptr, ptr %20, align 8, !tbaa !9
  %22 = load i32, ptr %3, align 4, !tbaa !15
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds ptr, ptr %21, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  call void @free(ptr noundef %25) #3
  br label %26

26:                                               ; preds = %18
  %27 = load i32, ptr %3, align 4, !tbaa !15
  %28 = add i32 %27, 1
  store i32 %28, ptr %3, align 4, !tbaa !15
  br label %11, !llvm.loop !17

29:                                               ; preds = %17
  %30 = load ptr, ptr %2, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.opng_image, ptr %30, i32 0, i32 9
  %32 = load ptr, ptr %31, align 8, !tbaa !9
  call void @free(ptr noundef %32) #3
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.opng_image, ptr %33, i32 0, i32 18
  %35 = load ptr, ptr %34, align 8, !tbaa !19
  call void @free(ptr noundef %35) #3
  %36 = load ptr, ptr %2, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.opng_image, ptr %36, i32 0, i32 10
  %38 = load ptr, ptr %37, align 8, !tbaa !20
  call void @free(ptr noundef %38) #3
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.opng_image, ptr %39, i32 0, i32 2
  %41 = load ptr, ptr %40, align 8, !tbaa !21
  call void @free(ptr noundef %41) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !15
  br label %42

42:                                               ; preds = %58, %29
  %43 = load i32, ptr %4, align 4, !tbaa !15
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.opng_image, ptr %44, i32 0, i32 8
  %46 = load i32, ptr %45, align 4, !tbaa !22
  %47 = icmp slt i32 %43, %46
  br i1 %47, label %49, label %48

48:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %61

49:                                               ; preds = %42
  %50 = load ptr, ptr %2, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.opng_image, ptr %50, i32 0, i32 15
  %52 = load ptr, ptr %51, align 8, !tbaa !23
  %53 = load i32, ptr %4, align 4, !tbaa !15
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds %struct.png_unknown_chunk_t, ptr %52, i64 %54
  %56 = getelementptr inbounds %struct.png_unknown_chunk_t, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !24
  call void @free(ptr noundef %57) #3
  br label %58

58:                                               ; preds = %49
  %59 = load i32, ptr %4, align 4, !tbaa !15
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %4, align 4, !tbaa !15
  br label %42, !llvm.loop !27

61:                                               ; preds = %48
  %62 = load ptr, ptr %2, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.opng_image, ptr %62, i32 0, i32 15
  %64 = load ptr, ptr %63, align 8, !tbaa !23
  call void @free(ptr noundef %64) #3
  br label %65

65:                                               ; preds = %61, %9
  ret void
}

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

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

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

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 = { 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 40}
!10 = !{!"opng_image", !11, i64 0, !11, i64 4, !6, i64 8, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64, !12, i64 72, !12, i64 82, !6, i64 96, !6, i64 104, !14, i64 112, !6, i64 120}
!11 = !{!"int", !7, i64 0}
!12 = !{!"png_color_16_struct", !7, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8}
!13 = !{!"short", !7, i64 0}
!14 = !{!"png_color_8_struct", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 4}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!10, !6, i64 120}
!20 = !{!10, !6, i64 48}
!21 = !{!10, !6, i64 8}
!22 = !{!10, !11, i64 36}
!23 = !{!10, !6, i64 96}
!24 = !{!25, !6, i64 8}
!25 = !{!"png_unknown_chunk_t", !7, i64 0, !6, i64 8, !26, i64 16, !7, i64 24}
!26 = !{!"long", !7, i64 0}
!27 = distinct !{!27, !18}


; 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.png_unknown_chunk_t = type { [5 x i8], ptr, i64, i8 }

; Function Attrs: nounwind uwtable
define dso_local void @opng_clear_image(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 40
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %32, label %.preheader

.preheader:                                       ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !14
  %.not21 = icmp eq i32 %5, 0
  br i1 %.not21, label %._crit_edge, label %.lr.ph

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load ptr, ptr %2, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %6 = phi ptr [ %.pre, %._crit_edge.loopexit ], [ %3, %.preheader ]
  tail call void @free(ptr noundef %6) #2
  %7 = getelementptr inbounds i8, ptr %0, i64 120
  %8 = load ptr, ptr %7, align 8, !tbaa !15
  tail call void @free(ptr noundef %8) #2
  %9 = getelementptr inbounds i8, ptr %0, i64 48
  %10 = load ptr, ptr %9, align 8, !tbaa !16
  tail call void @free(ptr noundef %10) #2
  %11 = getelementptr inbounds i8, ptr %0, i64 8
  %12 = load ptr, ptr %11, align 8, !tbaa !17
  tail call void @free(ptr noundef %12) #2
  %13 = getelementptr inbounds i8, ptr %0, i64 36
  %14 = load i32, ptr %13, align 4, !tbaa !18
  %15 = icmp sgt i32 %14, 0
  br i1 %15, label %.lr.ph19, label %._crit_edge20

.lr.ph19:                                         ; preds = %._crit_edge
  %16 = getelementptr inbounds i8, ptr %0, i64 96
  br label %25

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader ]
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds ptr, ptr %17, i64 %indvars.iv
  %19 = load ptr, ptr %18, align 8, !tbaa !19
  tail call void @free(ptr noundef %19) #2
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %20 = load i32, ptr %4, align 4, !tbaa !14
  %21 = zext i32 %20 to i64
  %22 = icmp ult i64 %indvars.iv.next, %21
  br i1 %22, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !20

._crit_edge20:                                    ; preds = %25, %._crit_edge
  %23 = getelementptr inbounds i8, ptr %0, i64 96
  %24 = load ptr, ptr %23, align 8, !tbaa !22
  tail call void @free(ptr noundef %24) #2
  br label %32

25:                                               ; preds = %.lr.ph19, %25
  %indvars.iv23 = phi i64 [ 0, %.lr.ph19 ], [ %indvars.iv.next24, %25 ]
  %26 = load ptr, ptr %16, align 8, !tbaa !22
  %27 = getelementptr inbounds %struct.png_unknown_chunk_t, ptr %26, i64 %indvars.iv23, i32 1
  %28 = load ptr, ptr %27, align 8, !tbaa !23
  tail call void @free(ptr noundef %28) #2
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %29 = load i32, ptr %13, align 4, !tbaa !18
  %30 = sext i32 %29 to i64
  %31 = icmp slt i64 %indvars.iv.next24, %30
  br i1 %31, label %25, label %._crit_edge20, !llvm.loop !26

32:                                               ; preds = %1, %._crit_edge20
  ret void
}

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

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, !10, i64 40}
!6 = !{!"opng_image", !7, i64 0, !7, i64 4, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !11, i64 72, !11, i64 82, !10, i64 96, !10, i64 104, !13, i64 112, !10, i64 120}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"png_color_16_struct", !8, i64 0, !12, i64 2, !12, i64 4, !12, i64 6, !12, i64 8}
!12 = !{!"short", !8, i64 0}
!13 = !{!"png_color_8_struct", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !8, i64 4}
!14 = !{!6, !7, i64 4}
!15 = !{!6, !10, i64 120}
!16 = !{!6, !10, i64 48}
!17 = !{!6, !10, i64 8}
!18 = !{!6, !7, i64 36}
!19 = !{!10, !10, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!6, !10, i64 96}
!23 = !{!24, !10, i64 8}
!24 = !{!"png_unknown_chunk_t", !8, i64 0, !10, i64 8, !25, i64 16, !8, i64 24}
!25 = !{!"long", !8, i64 0}
!26 = distinct !{!26, !21}


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