; LLVM IR from arasgungore/filters-and-fractals :: main (resizeImage)

; 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.PPMPixel = type { i8, i8, i8 }
%struct.PPMImage = type { i32, i32, ptr }

@BLACK = external dso_local constant %struct.PPMPixel, align 1

; 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 ptr @createPPMImage(i32 noundef, i32 noundef, i24) #1

; 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) #2

; Function Attrs: nounwind uwtable
declare dso_local void @deletePPMImage(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @copyPPMImage(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @resizeImage(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i24, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.PPMImage, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = shl i32 %17, 16
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = udiv i32 %18, %19
  %21 = add i32 %20, 1
  store i32 %21, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.PPMImage, ptr %22, i32 0, i32 1
  %24 = load i32, ptr %23, align 4, !tbaa !13
  %25 = shl i32 %24, 16
  %26 = load i32, ptr %6, align 4, !tbaa !9
  %27 = udiv i32 %25, %26
  %28 = add i32 %27, 1
  store i32 %28, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = load i32, ptr %6, align 4, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %14, ptr align 1 @BLACK, i64 3, i1 false)
  %31 = load i24, ptr %14, align 4
  %32 = call ptr @createPPMImage(i32 noundef %29, i32 noundef %30, i24 %31)
  store ptr %32, ptr %13, align 8, !tbaa !5
  store i32 0, ptr %11, align 4, !tbaa !9
  br label %33

33:                                               ; preds = %77, %3
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = icmp ult i32 %34, %35
  br i1 %36, label %37, label %80

37:                                               ; preds = %33
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %73, %37
  %39 = load i32, ptr %12, align 4, !tbaa !9
  %40 = load i32, ptr %5, align 4, !tbaa !9
  %41 = icmp ult i32 %39, %40
  br i1 %41, label %42, label %76

42:                                               ; preds = %38
  %43 = load i32, ptr %12, align 4, !tbaa !9
  %44 = load i32, ptr %7, align 4, !tbaa !9
  %45 = mul i32 %43, %44
  %46 = lshr i32 %45, 16
  store i32 %46, ptr %9, align 4, !tbaa !9
  %47 = load i32, ptr %11, align 4, !tbaa !9
  %48 = load i32, ptr %8, align 4, !tbaa !9
  %49 = mul i32 %47, %48
  %50 = lshr i32 %49, 16
  store i32 %50, ptr %10, align 4, !tbaa !9
  %51 = load ptr, ptr %13, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.PPMImage, ptr %51, i32 0, i32 2
  %53 = load ptr, ptr %52, align 8, !tbaa !14
  %54 = load i32, ptr %12, align 4, !tbaa !9
  %55 = load i32, ptr %11, align 4, !tbaa !9
  %56 = load i32, ptr %5, align 4, !tbaa !9
  %57 = mul i32 %55, %56
  %58 = add i32 %54, %57
  %59 = zext i32 %58 to i64
  %60 = getelementptr inbounds %struct.PPMPixel, ptr %53, i64 %59
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.PPMImage, ptr %61, i32 0, i32 2
  %63 = load ptr, ptr %62, align 8, !tbaa !14
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = load i32, ptr %10, align 4, !tbaa !9
  %66 = load ptr, ptr %4, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.PPMImage, ptr %66, i32 0, i32 0
  %68 = load i32, ptr %67, align 8, !tbaa !11
  %69 = mul i32 %65, %68
  %70 = add i32 %64, %69
  %71 = zext i32 %70 to i64
  %72 = getelementptr inbounds %struct.PPMPixel, ptr %63, i64 %71
  call void @llvm.memcpy.p0.p0.i64(ptr align 1 %60, ptr align 1 %72, i64 3, i1 false), !tbaa.struct !15
  br label %73

73:                                               ; preds = %42
  %74 = load i32, ptr %12, align 4, !tbaa !9
  %75 = add i32 %74, 1
  store i32 %75, ptr %12, align 4, !tbaa !9
  br label %38, !llvm.loop !17

76:                                               ; preds = %38
  br label %77

77:                                               ; preds = %76
  %78 = load i32, ptr %11, align 4, !tbaa !9
  %79 = add i32 %78, 1
  store i32 %79, ptr %11, align 4, !tbaa !9
  br label %33, !llvm.loop !19

80:                                               ; preds = %33
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = load ptr, ptr %13, align 8, !tbaa !5
  call void @copyPPMImage(ptr noundef %81, ptr noundef %82)
  %83 = load ptr, ptr %13, align 8, !tbaa !5
  call void @deletePPMImage(ptr noundef %83)
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !6, i64 8}
!15 = !{i64 0, i64 1, !16, i64 1, i64 1, !16, i64 2, i64 1, !16}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !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.PPMPixel = type { i8, i8, i8 }

@BLACK = external dso_local local_unnamed_addr constant %struct.PPMPixel, align 1

; Function Attrs: nounwind uwtable
declare dso_local ptr @createPPMImage(i32 noundef, i32 noundef, i24) local_unnamed_addr #0

; Function Attrs: mustprogress 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: nounwind uwtable
declare dso_local void @deletePPMImage(ptr noundef) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @resizeImage(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = load i32, ptr %0, align 8, !tbaa !5
  %5 = shl i32 %4, 16
  %6 = udiv i32 %5, %1
  %7 = add nuw i32 %6, 1
  %8 = getelementptr inbounds i8, ptr %0, i64 4
  %9 = load i32, ptr %8, align 4, !tbaa !11
  %10 = shl i32 %9, 16
  %11 = udiv i32 %10, %2
  %12 = add nuw i32 %11, 1
  %.0.copyload = load i24, ptr @BLACK, align 1
  %13 = tail call ptr @createPPMImage(i32 noundef %1, i32 noundef %2, i24 %.0.copyload)
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = getelementptr inbounds i8, ptr %0, i64 8
  %umax = tail call i32 @llvm.umax.i32(i32 %1, i32 1)
  %umax32 = tail call i32 @llvm.umax.i32(i32 %2, i32 1)
  %wide.trip.count = zext i32 %umax to i64
  br label %.preheader

.preheader:                                       ; preds = %3, %33
  %.030 = phi i32 [ 0, %3 ], [ %34, %33 ]
  %16 = mul i32 %.030, %12
  %17 = lshr i32 %16, 16
  %18 = mul i32 %.030, %1
  br label %19

19:                                               ; preds = %.preheader, %19
  %indvars.iv = phi i64 [ 0, %.preheader ], [ %indvars.iv.next, %19 ]
  %20 = trunc nuw i64 %indvars.iv to i32
  %21 = mul i32 %7, %20
  %22 = lshr i32 %21, 16
  %23 = load ptr, ptr %14, align 8, !tbaa !12
  %24 = add i32 %18, %20
  %25 = zext i32 %24 to i64
  %26 = getelementptr inbounds %struct.PPMPixel, ptr %23, i64 %25
  %27 = load ptr, ptr %15, align 8, !tbaa !12
  %28 = load i32, ptr %0, align 8, !tbaa !5
  %29 = mul i32 %28, %17
  %30 = add i32 %29, %22
  %31 = zext i32 %30 to i64
  %32 = getelementptr inbounds %struct.PPMPixel, ptr %27, i64 %31
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(3) %26, ptr noundef nonnull align 1 dereferenceable(3) %32, i64 3, i1 false), !tbaa.struct !13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %33, label %19, !llvm.loop !15

33:                                               ; preds = %19
  %34 = add nuw i32 %.030, 1
  %exitcond33.not = icmp eq i32 %34, %umax32
  br i1 %exitcond33.not, label %35, label %.preheader, !llvm.loop !17

35:                                               ; preds = %33
  tail call void @copyPPMImage(ptr noundef nonnull %0, ptr noundef nonnull %13)
  tail call void @deletePPMImage(ptr noundef nonnull %13)
  ret void
}

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

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 nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 0}
!6 = !{!"", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !10, i64 8}
!13 = !{i64 0, i64 1, !14, i64 1, i64 1, !14, i64 2, i64 1, !14}
!14 = !{!8, !8, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LICMPass,LoopRotatePass,LoopSimplifyPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass