; LLVM IR from ybj366533/YY-Camera :: yy_videolib/src/main/jni/png/contrib/gregbook (readppm)

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

@image_data = external dso_local global ptr, align 8
@saved_infile = external dso_local global ptr, align 8
@channels = external dso_local global i32, align 4
@width = external dso_local global i64, align 8
@height = external dso_local global i64, align 8

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

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

23:                                               ; preds = %3
  %24 = load ptr, ptr @image_data, align 8, !tbaa !9
  %25 = load i64, ptr %8, align 8, !tbaa !13
  %26 = load i64, ptr @height, align 8, !tbaa !13
  %27 = mul i64 %25, %26
  %28 = load ptr, ptr @saved_infile, align 8, !tbaa !9
  %29 = call i64 @fread(ptr noundef %24, i64 noundef 1, i64 noundef %27, ptr noundef %28)
  %30 = load i64, ptr %8, align 8, !tbaa !13
  %31 = load i64, ptr @height, align 8, !tbaa !13
  %32 = mul i64 %30, %31
  %33 = icmp ult i64 %29, %32
  br i1 %33, label %34, label %36

34:                                               ; preds = %23
  %35 = load ptr, ptr @image_data, align 8, !tbaa !9
  call void @free(ptr noundef %35) #5
  store ptr null, ptr @image_data, align 8, !tbaa !9
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %38

36:                                               ; preds = %23
  %37 = load ptr, ptr @image_data, align 8, !tbaa !9
  store ptr %37, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %38

38:                                               ; preds = %36, %34, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %39 = load ptr, ptr %4, align 8
  ret ptr %39
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

declare i64 @fread(ptr noundef, i64 noundef, i64 noundef, ptr noundef) #3

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

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

!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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !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"

@image_data = external dso_local local_unnamed_addr global ptr, align 8
@saved_infile = external dso_local local_unnamed_addr global ptr, align 8
@channels = external dso_local local_unnamed_addr global i32, align 4
@width = external dso_local local_unnamed_addr global i64, align 8
@height = external dso_local local_unnamed_addr global i64, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @readpng_get_image(double noundef %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = load i32, ptr @channels, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = load i64, ptr @width, align 8, !tbaa !9
  %7 = mul i64 %6, %5
  store i64 %7, ptr %2, align 8, !tbaa !9
  store i32 %4, ptr %1, align 4, !tbaa !5
  %8 = load i64, ptr @height, align 8, !tbaa !9
  %9 = mul i64 %8, %7
  %10 = tail call noalias ptr @malloc(i64 noundef %9) #4
  store ptr %10, ptr @image_data, align 8, !tbaa !11
  %11 = icmp eq ptr %10, null
  br i1 %11, label %20, label %12

12:                                               ; preds = %3
  %13 = load ptr, ptr @saved_infile, align 8, !tbaa !11
  %14 = tail call i64 @fread(ptr noundef nonnull %10, i64 noundef 1, i64 noundef %9, ptr noundef %13)
  %15 = load i64, ptr @height, align 8, !tbaa !9
  %16 = mul i64 %15, %7
  %17 = icmp ult i64 %14, %16
  %18 = load ptr, ptr @image_data, align 8, !tbaa !11
  br i1 %17, label %19, label %20

19:                                               ; preds = %12
  tail call void @free(ptr noundef %18) #5
  store ptr null, ptr @image_data, align 8, !tbaa !11
  br label %20

20:                                               ; preds = %12, %3, %19
  %.0 = phi ptr [ null, %19 ], [ null, %3 ], [ %18, %12 ]
  ret ptr %.0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i64 @fread(ptr nocapture noundef, i64 noundef, i64 noundef, ptr nocapture noundef) local_unnamed_addr #2

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

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 nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { nofree 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 = { 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 #4 = { nounwind allocsize(0) }
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 = !{!"int", !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 = !{!12, !12, i64 0}
!12 = !{!"any pointer", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass