; LLVM IR from rdguez-mariano/fast_imas_IPOL :: io_png (io_png)

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

; Function Attrs: nounwind uwtable
declare hidden ptr @read_png_raw(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 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: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #2

; 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
define dso_local ptr @read_png_u8_gray(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca ptr, align 8
  %15 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = call ptr @read_png_raw(ptr noundef %16, ptr noundef %17, ptr noundef %18, ptr noundef %8, i32 noundef 2, i32 noundef 1)
  store ptr %19, ptr %9, align 8, !tbaa !5
  %20 = load ptr, ptr %9, align 8, !tbaa !5
  %21 = icmp eq ptr null, %20
  br i1 %21, label %22, label %23

22:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %91

23:                                               ; preds = %3
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = icmp eq i64 1, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %23
  %27 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %27, ptr %4, align 8
  store i32 1, ptr %10, align 4
  br label %91

28:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #3
  %29 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %29, ptr %11, align 8, !tbaa !5
  %30 = load ptr, ptr %9, align 8, !tbaa !5
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = load ptr, ptr %7, align 8, !tbaa !5
  %34 = load i64, ptr %33, align 8, !tbaa !9
  %35 = mul i64 %32, %34
  %36 = getelementptr inbounds i8, ptr %30, i64 %35
  store ptr %36, ptr %12, align 8, !tbaa !5
  %37 = load ptr, ptr %9, align 8, !tbaa !5
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load i64, ptr %38, align 8, !tbaa !9
  %40 = mul i64 2, %39
  %41 = load ptr, ptr %7, align 8, !tbaa !5
  %42 = load i64, ptr %41, align 8, !tbaa !9
  %43 = mul i64 %40, %42
  %44 = getelementptr inbounds i8, ptr %37, i64 %43
  store ptr %44, ptr %13, align 8, !tbaa !5
  %45 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %45, ptr %14, align 8, !tbaa !5
  %46 = load ptr, ptr %14, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = load i64, ptr %49, align 8, !tbaa !9
  %51 = mul i64 %48, %50
  %52 = getelementptr inbounds i8, ptr %46, i64 %51
  store ptr %52, ptr %15, align 8, !tbaa !5
  br label %53

53:                                               ; preds = %57, %28
  %54 = load ptr, ptr %14, align 8, !tbaa !5
  %55 = load ptr, ptr %15, align 8, !tbaa !5
  %56 = icmp ult ptr %54, %55
  br i1 %56, label %57, label %81

57:                                               ; preds = %53
  %58 = load ptr, ptr %11, align 8, !tbaa !5
  %59 = getelementptr inbounds i8, ptr %58, i32 1
  store ptr %59, ptr %11, align 8, !tbaa !5
  %60 = load i8, ptr %58, align 1, !tbaa !11
  %61 = zext i8 %60 to i32
  %62 = mul nsw i32 6969, %61
  %63 = load ptr, ptr %12, align 8, !tbaa !5
  %64 = getelementptr inbounds i8, ptr %63, i32 1
  store ptr %64, ptr %12, align 8, !tbaa !5
  %65 = load i8, ptr %63, align 1, !tbaa !11
  %66 = zext i8 %65 to i32
  %67 = mul nsw i32 23434, %66
  %68 = add nsw i32 %62, %67
  %69 = load ptr, ptr %13, align 8, !tbaa !5
  %70 = getelementptr inbounds i8, ptr %69, i32 1
  store ptr %70, ptr %13, align 8, !tbaa !5
  %71 = load i8, ptr %69, align 1, !tbaa !11
  %72 = zext i8 %71 to i32
  %73 = mul nsw i32 2365, %72
  %74 = add nsw i32 %68, %73
  %75 = trunc i32 %74 to i8
  %76 = zext i8 %75 to i32
  %77 = sdiv i32 %76, 32768
  %78 = trunc i32 %77 to i8
  %79 = load ptr, ptr %14, align 8, !tbaa !5
  %80 = getelementptr inbounds i8, ptr %79, i32 1
  store ptr %80, ptr %14, align 8, !tbaa !5
  store i8 %78, ptr %79, align 1, !tbaa !11
  br label %53, !llvm.loop !12

81:                                               ; preds = %53
  %82 = load ptr, ptr %9, align 8, !tbaa !5
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = load i64, ptr %83, align 8, !tbaa !9
  %85 = load ptr, ptr %7, align 8, !tbaa !5
  %86 = load i64, ptr %85, align 8, !tbaa !9
  %87 = mul i64 %84, %86
  %88 = mul i64 %87, 1
  %89 = call ptr @realloc(ptr noundef %82, i64 noundef %88) #4
  store ptr %89, ptr %9, align 8, !tbaa !5
  %90 = load ptr, ptr %9, align 8, !tbaa !5
  store ptr %90, ptr %4, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  br label %91

91:                                               ; preds = %81, %26, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %92 = load ptr, ptr %4, align 8
  ret ptr %92
}

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 allocsize(1) "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 }
attributes #4 = { nounwind allocsize(1) }

!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 = !{!"long", !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"

; Function Attrs: nounwind uwtable
declare hidden ptr @read_png_raw(ptr noundef, ptr noundef, ptr noundef, ptr noundef, i32 noundef, i32 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 nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #2

; 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
define dso_local ptr @read_png_u8_gray(ptr noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #0 {
  %4 = alloca i64, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #5
  %5 = call ptr @read_png_raw(ptr noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef nonnull %4, i32 noundef 2, i32 noundef 1)
  %6 = ptrtoint ptr %5 to i64
  %7 = icmp eq ptr %5, null
  br i1 %7, label %20, label %8

8:                                                ; preds = %3
  %9 = load i64, ptr %4, align 8, !tbaa !5
  %10 = icmp eq i64 %9, 1
  br i1 %10, label %20, label %11

11:                                               ; preds = %8
  %12 = load i64, ptr %1, align 8, !tbaa !5
  %13 = load i64, ptr %2, align 8, !tbaa !5
  %14 = mul i64 %13, %12
  %15 = icmp sgt i64 %14, 0
  br i1 %15, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %11
  %16 = add i64 %14, %6
  %17 = add i64 %6, 1
  %umax = call i64 @llvm.umax.i64(i64 %16, i64 %17)
  %18 = sub i64 %umax, %6
  call void @llvm.memset.p0.i64(ptr nonnull align 1 %5, i8 0, i64 %18, i1 false), !tbaa !9
  %.pre = load i64, ptr %1, align 8, !tbaa !5
  %.pre33 = load i64, ptr %2, align 8, !tbaa !5
  %.pre34 = mul i64 %.pre33, %.pre
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.preheader, %11
  %.pre-phi = phi i64 [ %.pre34, %.lr.ph.preheader ], [ %14, %11 ]
  %19 = call ptr @realloc(ptr noundef nonnull %5, i64 noundef %.pre-phi) #6
  br label %20

20:                                               ; preds = %8, %3, %._crit_edge
  %.0 = phi ptr [ %19, %._crit_edge ], [ null, %3 ], [ %5, %8 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #5
  ret ptr %.0
}

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #4

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 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) 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 #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { nounwind allocsize(1) }

!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 = !{!7, !7, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SROAPass,SimplifyCFGPass