; LLVM IR from Sentdex/NNfSiX :: C (p006-Softmax-Activation)

; 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.layer_dense_t = type { ptr, ptr, ptr, i32, i32, ptr }

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

; Function Attrs: nounwind uwtable
define dso_local void @activation_softmax(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  store double 0.000000e+00, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store double 0.000000e+00, 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
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.layer_dense_t, ptr %6, i32 0, i32 2
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %9 = getelementptr inbounds double, ptr %8, i64 0
  %10 = load double, ptr %9, align 8, !tbaa !9
  store double %10, ptr %4, align 8, !tbaa !9
  store i32 1, ptr %5, align 4, !tbaa !11
  br label %11

11:                                               ; preds = %36, %1
  %12 = load i32, ptr %5, align 4, !tbaa !11
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.layer_dense_t, ptr %13, i32 0, i32 4
  %15 = load i32, ptr %14, align 4, !tbaa !15
  %16 = icmp slt i32 %12, %15
  br i1 %16, label %17, label %39

17:                                               ; preds = %11
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.layer_dense_t, ptr %18, i32 0, i32 2
  %20 = load ptr, ptr %19, align 8, !tbaa !13
  %21 = load i32, ptr %5, align 4, !tbaa !11
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds double, ptr %20, i64 %22
  %24 = load double, ptr %23, align 8, !tbaa !9
  %25 = load double, ptr %4, align 8, !tbaa !9
  %26 = fcmp ogt double %24, %25
  br i1 %26, label %27, label %35

27:                                               ; preds = %17
  %28 = load ptr, ptr %2, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.layer_dense_t, ptr %28, i32 0, i32 2
  %30 = load ptr, ptr %29, align 8, !tbaa !13
  %31 = load i32, ptr %5, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds double, ptr %30, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !9
  store double %34, ptr %4, align 8, !tbaa !9
  br label %35

35:                                               ; preds = %27, %17
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %5, align 4, !tbaa !11
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %5, align 4, !tbaa !11
  br label %11, !llvm.loop !16

39:                                               ; preds = %11
  store i32 0, ptr %5, align 4, !tbaa !11
  br label %40

40:                                               ; preds = %72, %39
  %41 = load i32, ptr %5, align 4, !tbaa !11
  %42 = load ptr, ptr %2, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.layer_dense_t, ptr %42, i32 0, i32 4
  %44 = load i32, ptr %43, align 4, !tbaa !15
  %45 = icmp slt i32 %41, %44
  br i1 %45, label %46, label %75

46:                                               ; preds = %40
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.layer_dense_t, ptr %47, i32 0, i32 2
  %49 = load ptr, ptr %48, align 8, !tbaa !13
  %50 = load i32, ptr %5, align 4, !tbaa !11
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds double, ptr %49, i64 %51
  %53 = load double, ptr %52, align 8, !tbaa !9
  %54 = load double, ptr %4, align 8, !tbaa !9
  %55 = fsub double %53, %54
  %56 = call double @exp(double noundef %55) #3
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.layer_dense_t, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !13
  %60 = load i32, ptr %5, align 4, !tbaa !11
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds double, ptr %59, i64 %61
  store double %56, ptr %62, align 8, !tbaa !9
  %63 = load ptr, ptr %2, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.layer_dense_t, ptr %63, i32 0, i32 2
  %65 = load ptr, ptr %64, align 8, !tbaa !13
  %66 = load i32, ptr %5, align 4, !tbaa !11
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds double, ptr %65, i64 %67
  %69 = load double, ptr %68, align 8, !tbaa !9
  %70 = load double, ptr %3, align 8, !tbaa !9
  %71 = fadd double %70, %69
  store double %71, ptr %3, align 8, !tbaa !9
  br label %72

72:                                               ; preds = %46
  %73 = load i32, ptr %5, align 4, !tbaa !11
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %5, align 4, !tbaa !11
  br label %40, !llvm.loop !18

75:                                               ; preds = %40
  store i32 0, ptr %5, align 4, !tbaa !11
  br label %76

76:                                               ; preds = %98, %75
  %77 = load i32, ptr %5, align 4, !tbaa !11
  %78 = load ptr, ptr %2, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.layer_dense_t, ptr %78, i32 0, i32 4
  %80 = load i32, ptr %79, align 4, !tbaa !15
  %81 = icmp slt i32 %77, %80
  br i1 %81, label %82, label %101

82:                                               ; preds = %76
  %83 = load ptr, ptr %2, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.layer_dense_t, ptr %83, i32 0, i32 2
  %85 = load ptr, ptr %84, align 8, !tbaa !13
  %86 = load i32, ptr %5, align 4, !tbaa !11
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds double, ptr %85, i64 %87
  %89 = load double, ptr %88, align 8, !tbaa !9
  %90 = load double, ptr %3, align 8, !tbaa !9
  %91 = fdiv double %89, %90
  %92 = load ptr, ptr %2, align 8, !tbaa !5
  %93 = getelementptr inbounds %struct.layer_dense_t, ptr %92, i32 0, i32 2
  %94 = load ptr, ptr %93, align 8, !tbaa !13
  %95 = load i32, ptr %5, align 4, !tbaa !11
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds double, ptr %94, i64 %96
  store double %91, ptr %97, align 8, !tbaa !9
  br label %98

98:                                               ; preds = %82
  %99 = load i32, ptr %5, align 4, !tbaa !11
  %100 = add nsw i32 %99, 1
  store i32 %100, ptr %5, align 4, !tbaa !11
  br label %76, !llvm.loop !19

101:                                              ; preds = %76
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #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, !6, i64 16}
!14 = !{!"", !6, i64 0, !6, i64 8, !6, i64 16, !12, i64 24, !12, i64 28, !6, i64 32}
!15 = !{!14, !12, i64 28}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17}
!19 = distinct !{!19, !17}


; 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: mustprogress nofree nounwind willreturn memory(write)
declare double @exp(double noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind memory(readwrite, inaccessiblemem: write) uwtable
define dso_local void @activation_softmax(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = load double, ptr %3, align 8, !tbaa !11
  %5 = getelementptr inbounds i8, ptr %0, i64 28
  %6 = load i32, ptr %5, align 4, !tbaa !13
  %7 = icmp sgt i32 %6, 1
  br i1 %7, label %.lr.ph.preheader, label %.preheader31

.lr.ph.preheader:                                 ; preds = %1
  %wide.trip.count = zext nneg i32 %6 to i64
  %8 = add nsw i64 %wide.trip.count, -1
  %9 = add nsw i64 %wide.trip.count, -2
  %xtraiter = and i64 %8, 3
  %10 = icmp ult i64 %9, 3
  br i1 %10, label %.preheader31.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %8, -4
  %invariant.gep = getelementptr inbounds i8, ptr %3, i64 8
  %invariant.gep64 = getelementptr inbounds i8, ptr %3, i64 16
  %invariant.gep66 = getelementptr inbounds i8, ptr %3, i64 24
  br label %.lr.ph

.preheader31.loopexit.unr-lcssa:                  ; preds = %.lr.ph, %.lr.ph.preheader
  %.128.lcssa.ph = phi double [ poison, %.lr.ph.preheader ], [ %.128.3, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %indvars.iv.next.3, %.lr.ph ]
  %.02733.unr = phi double [ %4, %.lr.ph.preheader ], [ %.128.3, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader31, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader31.loopexit.unr-lcssa, %.lr.ph.epil
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.preheader31.loopexit.unr-lcssa ]
  %.02733.epil = phi double [ %.128.epil, %.lr.ph.epil ], [ %.02733.unr, %.preheader31.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.preheader31.loopexit.unr-lcssa ]
  %11 = getelementptr inbounds double, ptr %3, i64 %indvars.iv.epil
  %12 = load double, ptr %11, align 8, !tbaa !11
  %13 = fcmp ogt double %12, %.02733.epil
  %.128.epil = select i1 %13, double %12, double %.02733.epil
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.preheader31, label %.lr.ph.epil, !llvm.loop !14

.preheader31:                                     ; preds = %.preheader31.loopexit.unr-lcssa, %.lr.ph.epil, %1
  %.027.lcssa = phi double [ %4, %1 ], [ %.128.lcssa.ph, %.preheader31.loopexit.unr-lcssa ], [ %.128.epil, %.lr.ph.epil ]
  %14 = icmp sgt i32 %6, 0
  br i1 %14, label %.lr.ph37, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ 1, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %.02733 = phi double [ %4, %.lr.ph.preheader.new ], [ %.128.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %15 = getelementptr inbounds double, ptr %3, i64 %indvars.iv
  %16 = load double, ptr %15, align 8, !tbaa !11
  %17 = fcmp ogt double %16, %.02733
  %.128 = select i1 %17, double %16, double %.02733
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv
  %18 = load double, ptr %gep, align 8, !tbaa !11
  %19 = fcmp ogt double %18, %.128
  %.128.1 = select i1 %19, double %18, double %.128
  %gep65 = getelementptr inbounds double, ptr %invariant.gep64, i64 %indvars.iv
  %20 = load double, ptr %gep65, align 8, !tbaa !11
  %21 = fcmp ogt double %20, %.128.1
  %.128.2 = select i1 %21, double %20, double %.128.1
  %gep67 = getelementptr inbounds double, ptr %invariant.gep66, i64 %indvars.iv
  %22 = load double, ptr %gep67, align 8, !tbaa !11
  %23 = fcmp ogt double %22, %.128.2
  %.128.3 = select i1 %23, double %22, double %.128.2
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.preheader31.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !16

.preheader:                                       ; preds = %.lr.ph37
  %24 = icmp sgt i32 %36, 0
  br i1 %24, label %.lr.ph41, label %._crit_edge

.lr.ph41:                                         ; preds = %.preheader
  %wide.trip.count51 = zext nneg i32 %36 to i64
  %min.iters.check = icmp eq i32 %36, 1
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph41
  %indvars.iv48.ph = phi i64 [ 0, %.lr.ph41 ], [ %n.vec, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.lr.ph41
  %n.vec = and i64 %wide.trip.count51, 2147483646
  %broadcast.splatinsert = insertelement <2 x double> poison, double %35, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <2 x double> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %25 = getelementptr inbounds double, ptr %33, i64 %index
  %wide.load = load <2 x double>, ptr %25, align 8, !tbaa !11
  %26 = fdiv <2 x double> %wide.load, %broadcast.splat
  store <2 x double> %26, ptr %25, align 8, !tbaa !11
  %index.next = add nuw i64 %index, 2
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !18

middle.block:                                     ; preds = %vector.body
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count51
  br i1 %cmp.n, label %._crit_edge, label %scalar.ph.preheader

.lr.ph37:                                         ; preds = %.preheader31, %.lr.ph37
  %28 = phi ptr [ %33, %.lr.ph37 ], [ %3, %.preheader31 ]
  %indvars.iv45 = phi i64 [ %indvars.iv.next46, %.lr.ph37 ], [ 0, %.preheader31 ]
  %.02935 = phi double [ %35, %.lr.ph37 ], [ 0.000000e+00, %.preheader31 ]
  %29 = getelementptr inbounds double, ptr %28, i64 %indvars.iv45
  %30 = load double, ptr %29, align 8, !tbaa !11
  %31 = fsub double %30, %.027.lcssa
  %32 = tail call double @exp(double noundef %31) #2
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = getelementptr inbounds double, ptr %33, i64 %indvars.iv45
  store double %32, ptr %34, align 8, !tbaa !11
  %35 = fadd double %.02935, %32
  %indvars.iv.next46 = add nuw nsw i64 %indvars.iv45, 1
  %36 = load i32, ptr %5, align 4, !tbaa !13
  %37 = sext i32 %36 to i64
  %38 = icmp slt i64 %indvars.iv.next46, %37
  br i1 %38, label %.lr.ph37, label %.preheader, !llvm.loop !21

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv48 = phi i64 [ %indvars.iv.next49, %scalar.ph ], [ %indvars.iv48.ph, %scalar.ph.preheader ]
  %39 = getelementptr inbounds double, ptr %33, i64 %indvars.iv48
  %40 = load double, ptr %39, align 8, !tbaa !11
  %41 = fdiv double %40, %35
  store double %41, ptr %39, align 8, !tbaa !11
  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
  %exitcond52.not = icmp eq i64 %indvars.iv.next49, %wide.trip.count51
  br i1 %exitcond52.not, label %._crit_edge, label %scalar.ph, !llvm.loop !22

._crit_edge:                                      ; preds = %scalar.ph, %middle.block, %.preheader31, %.preheader
  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 16}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !10, i64 24, !10, i64 28, !7, i64 32}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !8, i64 0}
!13 = !{!6, !10, i64 28}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.unroll.disable"}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = distinct !{!18, !17, !19, !20}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !17}
!22 = distinct !{!22, !17, !20, !19}


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