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

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

; 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
declare double @pow(double noundef, double noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @gammaCorrection(ptr noundef %0, double noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store double %1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !11
  br label %6

6:                                                ; preds = %80, %2
  %7 = load i32, ptr %5, align 4, !tbaa !11
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.PPMImage, ptr %8, i32 0, i32 0
  %10 = load i32, ptr %9, align 8, !tbaa !13
  %11 = load ptr, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.PPMImage, ptr %11, i32 0, i32 1
  %13 = load i32, ptr %12, align 4, !tbaa !15
  %14 = mul i32 %10, %13
  %15 = icmp ult i32 %7, %14
  br i1 %15, label %16, label %83

16:                                               ; preds = %6
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.PPMImage, ptr %17, i32 0, i32 2
  %19 = load ptr, ptr %18, align 8, !tbaa !16
  %20 = load i32, ptr %5, align 4, !tbaa !11
  %21 = zext i32 %20 to i64
  %22 = getelementptr inbounds %struct.PPMPixel, ptr %19, i64 %21
  %23 = getelementptr inbounds %struct.PPMPixel, ptr %22, i32 0, i32 0
  %24 = load i8, ptr %23, align 1, !tbaa !17
  %25 = uitofp i8 %24 to double
  %26 = fdiv double %25, 2.550000e+02
  %27 = load double, ptr %4, align 8, !tbaa !9
  %28 = call double @pow(double noundef %26, double noundef %27) #3
  %29 = fmul double 2.550000e+02, %28
  %30 = fptoui double %29 to i8
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.PPMImage, ptr %31, i32 0, i32 2
  %33 = load ptr, ptr %32, align 8, !tbaa !16
  %34 = load i32, ptr %5, align 4, !tbaa !11
  %35 = zext i32 %34 to i64
  %36 = getelementptr inbounds %struct.PPMPixel, ptr %33, i64 %35
  %37 = getelementptr inbounds %struct.PPMPixel, ptr %36, i32 0, i32 0
  store i8 %30, ptr %37, align 1, !tbaa !17
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.PPMImage, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !16
  %41 = load i32, ptr %5, align 4, !tbaa !11
  %42 = zext i32 %41 to i64
  %43 = getelementptr inbounds %struct.PPMPixel, ptr %40, i64 %42
  %44 = getelementptr inbounds %struct.PPMPixel, ptr %43, i32 0, i32 1
  %45 = load i8, ptr %44, align 1, !tbaa !19
  %46 = uitofp i8 %45 to double
  %47 = fdiv double %46, 2.550000e+02
  %48 = load double, ptr %4, align 8, !tbaa !9
  %49 = call double @pow(double noundef %47, double noundef %48) #3
  %50 = fmul double 2.550000e+02, %49
  %51 = fptoui double %50 to i8
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.PPMImage, ptr %52, i32 0, i32 2
  %54 = load ptr, ptr %53, align 8, !tbaa !16
  %55 = load i32, ptr %5, align 4, !tbaa !11
  %56 = zext i32 %55 to i64
  %57 = getelementptr inbounds %struct.PPMPixel, ptr %54, i64 %56
  %58 = getelementptr inbounds %struct.PPMPixel, ptr %57, i32 0, i32 1
  store i8 %51, ptr %58, align 1, !tbaa !19
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct.PPMImage, ptr %59, i32 0, i32 2
  %61 = load ptr, ptr %60, align 8, !tbaa !16
  %62 = load i32, ptr %5, align 4, !tbaa !11
  %63 = zext i32 %62 to i64
  %64 = getelementptr inbounds %struct.PPMPixel, ptr %61, i64 %63
  %65 = getelementptr inbounds %struct.PPMPixel, ptr %64, i32 0, i32 2
  %66 = load i8, ptr %65, align 1, !tbaa !20
  %67 = uitofp i8 %66 to double
  %68 = fdiv double %67, 2.550000e+02
  %69 = load double, ptr %4, align 8, !tbaa !9
  %70 = call double @pow(double noundef %68, double noundef %69) #3
  %71 = fmul double 2.550000e+02, %70
  %72 = fptoui double %71 to i8
  %73 = load ptr, ptr %3, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.PPMImage, ptr %73, i32 0, i32 2
  %75 = load ptr, ptr %74, align 8, !tbaa !16
  %76 = load i32, ptr %5, align 4, !tbaa !11
  %77 = zext i32 %76 to i64
  %78 = getelementptr inbounds %struct.PPMPixel, ptr %75, i64 %77
  %79 = getelementptr inbounds %struct.PPMPixel, ptr %78, i32 0, i32 2
  store i8 %72, ptr %79, align 1, !tbaa !20
  br label %80

80:                                               ; preds = %16
  %81 = load i32, ptr %5, align 4, !tbaa !11
  %82 = add i32 %81, 1
  store i32 %82, ptr %5, align 4, !tbaa !11
  br label %6, !llvm.loop !21

83:                                               ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #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 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!14, !12, i64 0}
!14 = !{!"", !12, i64 0, !12, i64 4, !6, i64 8}
!15 = !{!14, !12, i64 4}
!16 = !{!14, !6, i64 8}
!17 = !{!18, !7, i64 0}
!18 = !{!"", !7, i64 0, !7, i64 1, !7, i64 2}
!19 = !{!18, !7, i64 1}
!20 = !{!18, !7, i64 2}
!21 = distinct !{!21, !22}
!22 = !{!"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"

%struct.PPMPixel = type { i8, i8, i8 }

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @pow(double noundef, double noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: write) uwtable
define dso_local void @gammaCorrection(ptr nocapture noundef readonly %0, double noundef %1) local_unnamed_addr #1 {
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = load i32, ptr %0, align 8, !tbaa !5
  %5 = load i32, ptr %3, align 4, !tbaa !11
  %6 = mul i32 %5, %4
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %7 = getelementptr inbounds i8, ptr %0, i64 8
  %.pre = load ptr, ptr %7, align 8, !tbaa !12
  br label %8

8:                                                ; preds = %.lr.ph, %8
  %9 = phi ptr [ %.pre, %.lr.ph ], [ %35, %8 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %8 ]
  %10 = getelementptr inbounds %struct.PPMPixel, ptr %9, i64 %indvars.iv
  %11 = load i8, ptr %10, align 1, !tbaa !13
  %12 = uitofp i8 %11 to double
  %13 = fdiv double %12, 2.550000e+02
  %14 = tail call double @pow(double noundef %13, double noundef %1) #2
  %15 = fmul double %14, 2.550000e+02
  %16 = fptoui double %15 to i8
  %17 = load ptr, ptr %7, align 8, !tbaa !12
  %18 = getelementptr inbounds %struct.PPMPixel, ptr %17, i64 %indvars.iv
  store i8 %16, ptr %18, align 1, !tbaa !13
  %19 = getelementptr inbounds %struct.PPMPixel, ptr %17, i64 %indvars.iv, i32 1
  %20 = load i8, ptr %19, align 1, !tbaa !15
  %21 = uitofp i8 %20 to double
  %22 = fdiv double %21, 2.550000e+02
  %23 = tail call double @pow(double noundef %22, double noundef %1) #2
  %24 = fmul double %23, 2.550000e+02
  %25 = fptoui double %24 to i8
  %26 = load ptr, ptr %7, align 8, !tbaa !12
  %27 = getelementptr inbounds %struct.PPMPixel, ptr %26, i64 %indvars.iv, i32 1
  store i8 %25, ptr %27, align 1, !tbaa !15
  %28 = getelementptr inbounds %struct.PPMPixel, ptr %26, i64 %indvars.iv, i32 2
  %29 = load i8, ptr %28, align 1, !tbaa !16
  %30 = uitofp i8 %29 to double
  %31 = fdiv double %30, 2.550000e+02
  %32 = tail call double @pow(double noundef %31, double noundef %1) #2
  %33 = fmul double %32, 2.550000e+02
  %34 = fptoui double %33 to i8
  %35 = load ptr, ptr %7, align 8, !tbaa !12
  %36 = getelementptr inbounds %struct.PPMPixel, ptr %35, i64 %indvars.iv, i32 2
  store i8 %34, ptr %36, align 1, !tbaa !16
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %37 = load i32, ptr %0, align 8, !tbaa !5
  %38 = load i32, ptr %3, align 4, !tbaa !11
  %39 = mul i32 %38, %37
  %40 = zext i32 %39 to i64
  %41 = icmp ult i64 %indvars.iv.next, %40
  br i1 %41, label %8, label %._crit_edge, !llvm.loop !17

._crit_edge:                                      ; preds = %8, %2
  ret void
}

attributes #0 = { mustprogress nofree nounwind willreturn memory(write) "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 = { nofree nounwind memory(readwrite, inaccessiblemem: write) 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 }

!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 = !{!14, !8, i64 0}
!14 = !{!"", !8, i64 0, !8, i64 1, !8, i64 2}
!15 = !{!14, !8, i64 1}
!16 = !{!14, !8, i64 2}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


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