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

; 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 @rotateImage(ptr noundef %0, i16 noundef signext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i16, align 2
  %7 = alloca ptr, align 8
  %8 = alloca i24, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  %12 = load i16, ptr %4, align 2, !tbaa !9
  %13 = sext i16 %12 to i32
  %14 = srem i32 %13, 4
  %15 = add nsw i32 %14, 4
  %16 = srem i32 %15, 4
  %17 = trunc i32 %16 to i16
  store i16 %17, ptr %5, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %6) #3
  store i16 0, ptr %6, align 2, !tbaa !9
  br label %18

18:                                               ; preds = %95, %2
  %19 = load i16, ptr %6, align 2, !tbaa !9
  %20 = zext i16 %19 to i32
  %21 = load i16, ptr %5, align 2, !tbaa !9
  %22 = zext i16 %21 to i32
  %23 = icmp slt i32 %20, %22
  br i1 %23, label %24, label %98

24:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.PPMImage, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = load ptr, ptr %3, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.PPMImage, ptr %28, i32 0, i32 1
  %30 = load i32, ptr %29, align 4, !tbaa !14
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %8, ptr align 1 @BLACK, i64 3, i1 false)
  %31 = load i24, ptr %8, align 4
  %32 = call ptr @createPPMImage(i32 noundef %27, i32 noundef %30, i24 %31)
  store ptr %32, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %9, align 4, !tbaa !15
  br label %33

33:                                               ; preds = %77, %24
  %34 = load i32, ptr %9, align 4, !tbaa !15
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.PPMImage, ptr %35, i32 0, i32 1
  %37 = load i32, ptr %36, align 4, !tbaa !14
  %38 = icmp ult i32 %34, %37
  br i1 %38, label %39, label %80

39:                                               ; preds = %33
  store i32 0, ptr %10, align 4, !tbaa !15
  br label %40

40:                                               ; preds = %73, %39
  %41 = load i32, ptr %10, align 4, !tbaa !15
  %42 = load ptr, ptr %3, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.PPMImage, ptr %42, i32 0, i32 0
  %44 = load i32, ptr %43, align 8, !tbaa !11
  %45 = icmp ult i32 %41, %44
  br i1 %45, label %46, label %76

46:                                               ; preds = %40
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.PPMImage, ptr %47, i32 0, i32 2
  %49 = load ptr, ptr %48, align 8, !tbaa !16
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.PPMImage, ptr %50, i32 0, i32 1
  %52 = load i32, ptr %51, align 4, !tbaa !14
  %53 = load i32, ptr %10, align 4, !tbaa !15
  %54 = add i32 %53, 1
  %55 = mul i32 %52, %54
  %56 = load i32, ptr %9, align 4, !tbaa !15
  %57 = sub i32 %55, %56
  %58 = sub i32 %57, 1
  %59 = zext i32 %58 to i64
  %60 = getelementptr inbounds %struct.PPMPixel, ptr %49, i64 %59
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.PPMImage, ptr %61, i32 0, i32 2
  %63 = load ptr, ptr %62, align 8, !tbaa !16
  %64 = load ptr, ptr %3, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.PPMImage, ptr %64, i32 0, i32 0
  %66 = load i32, ptr %65, align 8, !tbaa !11
  %67 = load i32, ptr %9, align 4, !tbaa !15
  %68 = mul i32 %66, %67
  %69 = load i32, ptr %10, align 4, !tbaa !15
  %70 = add i32 %68, %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 !17
  br label %73

73:                                               ; preds = %46
  %74 = load i32, ptr %10, align 4, !tbaa !15
  %75 = add i32 %74, 1
  store i32 %75, ptr %10, align 4, !tbaa !15
  br label %40, !llvm.loop !19

76:                                               ; preds = %40
  br label %77

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

80:                                               ; preds = %33
  %81 = load ptr, ptr %3, align 8, !tbaa !5
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  call void @copyPPMImage(ptr noundef %81, ptr noundef %82)
  %83 = load ptr, ptr %7, align 8, !tbaa !5
  call void @deletePPMImage(ptr noundef %83)
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %84 = load ptr, ptr %3, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.PPMImage, ptr %84, i32 0, i32 1
  %86 = load i32, ptr %85, align 4, !tbaa !14
  store i32 %86, ptr %11, align 4, !tbaa !15
  %87 = load ptr, ptr %3, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.PPMImage, ptr %87, i32 0, i32 0
  %89 = load i32, ptr %88, align 8, !tbaa !11
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.PPMImage, ptr %90, i32 0, i32 1
  store i32 %89, ptr %91, align 4, !tbaa !14
  %92 = load i32, ptr %11, align 4, !tbaa !15
  %93 = load ptr, ptr %3, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.PPMImage, ptr %93, i32 0, i32 0
  store i32 %92, ptr %94, align 8, !tbaa !11
  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 8, ptr %7) #3
  br label %95

95:                                               ; preds = %80
  %96 = load i16, ptr %6, align 2, !tbaa !9
  %97 = add i16 %96, 1
  store i16 %97, ptr %6, align 2, !tbaa !9
  br label %18, !llvm.loop !22

98:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 2, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #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 = !{!"short", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 4, !6, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!12, !13, i64 4}
!15 = !{!13, !13, i64 0}
!16 = !{!12, !6, i64 8}
!17 = !{i64 0, i64 1, !18, i64 1, i64 1, !18, i64 2, i64 1, !18}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = distinct !{!21, !20}
!22 = distinct !{!22, !20}


; 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 @rotateImage(ptr noundef %0, i16 noundef signext %1) local_unnamed_addr #0 {
  %3 = and i16 %1, 3
  %.not = icmp eq i16 %3, 0
  br i1 %.not, label %._crit_edge37, label %.lr.ph36

.lr.ph36:                                         ; preds = %2
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %.0.copyload = load i24, ptr @BLACK, align 1
  %5 = getelementptr inbounds i8, ptr %0, i64 8
  %.pre = load i32, ptr %0, align 8, !tbaa !5
  %.pre42 = load i32, ptr %4, align 4, !tbaa !11
  br label %6

6:                                                ; preds = %.lr.ph36, %._crit_edge33
  %7 = phi i32 [ %.pre42, %.lr.ph36 ], [ %37, %._crit_edge33 ]
  %8 = phi i32 [ %.pre, %.lr.ph36 ], [ %36, %._crit_edge33 ]
  %.034 = phi i16 [ 0, %.lr.ph36 ], [ %38, %._crit_edge33 ]
  %9 = tail call ptr @createPPMImage(i32 noundef %8, i32 noundef %7, i24 %.0.copyload)
  %10 = load i32, ptr %4, align 4, !tbaa !11
  %.not38 = icmp eq i32 %10, 0
  br i1 %.not38, label %._crit_edge33, label %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %6
  %11 = getelementptr inbounds i8, ptr %9, i64 8
  %12 = load i32, ptr %0, align 8, !tbaa !5
  %.not39 = icmp eq i32 %12, 0
  br i1 %.not39, label %._crit_edge33, label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %13 = phi i32 [ %32, %._crit_edge ], [ %10, %.preheader.lr.ph ]
  %14 = phi i32 [ %33, %._crit_edge ], [ %12, %.preheader.lr.ph ]
  %.02932 = phi i32 [ %34, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %.not40 = icmp eq i32 %14, 0
  br i1 %.not40, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader
  %15 = xor i32 %.02932, -1
  br label %16

16:                                               ; preds = %.lr.ph, %16
  %17 = phi i32 [ %14, %.lr.ph ], [ %30, %16 ]
  %.02831 = phi i32 [ 0, %.lr.ph ], [ %20, %16 ]
  %18 = load ptr, ptr %11, align 8, !tbaa !12
  %19 = load i32, ptr %4, align 4, !tbaa !11
  %20 = add nuw i32 %.02831, 1
  %21 = mul i32 %19, %20
  %22 = add i32 %21, %15
  %23 = zext i32 %22 to i64
  %24 = getelementptr inbounds %struct.PPMPixel, ptr %18, i64 %23
  %25 = load ptr, ptr %5, align 8, !tbaa !12
  %26 = mul i32 %17, %.02932
  %27 = add i32 %26, %.02831
  %28 = zext i32 %27 to i64
  %29 = getelementptr inbounds %struct.PPMPixel, ptr %25, i64 %28
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 1 dereferenceable(3) %24, ptr noundef nonnull align 1 dereferenceable(3) %29, i64 3, i1 false), !tbaa.struct !13
  %30 = load i32, ptr %0, align 8, !tbaa !5
  %31 = icmp ult i32 %20, %30
  br i1 %31, label %16, label %._crit_edge.loopexit, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %16
  %.pre43 = load i32, ptr %4, align 4, !tbaa !11
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %32 = phi i32 [ %.pre43, %._crit_edge.loopexit ], [ %13, %.preheader ]
  %33 = phi i32 [ %30, %._crit_edge.loopexit ], [ 0, %.preheader ]
  %34 = add nuw i32 %.02932, 1
  %35 = icmp ult i32 %34, %32
  br i1 %35, label %.preheader, label %._crit_edge33, !llvm.loop !17

._crit_edge33:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %6
  tail call void @copyPPMImage(ptr noundef nonnull %0, ptr noundef %9)
  tail call void @deletePPMImage(ptr noundef %9)
  %36 = load i32, ptr %4, align 4, !tbaa !11
  %37 = load i32, ptr %0, align 8, !tbaa !5
  store i32 %37, ptr %4, align 4, !tbaa !11
  store i32 %36, ptr %0, align 8, !tbaa !5
  %38 = add nuw nsw i16 %.034, 1
  %exitcond.not = icmp eq i16 %38, %3
  br i1 %exitcond.not, label %._crit_edge37, label %6, !llvm.loop !19

._crit_edge37:                                    ; preds = %._crit_edge33, %2
  ret void
}

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) }

!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, !18}
!18 = !{!"llvm.loop.unswitch.partial.disable"}
!19 = distinct !{!19, !16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass