; LLVM IR from YanzhenHuang/c-nn :: nn (softMax)

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

@stderr = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [37 x i8], align 1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare double @mat_read(ptr noundef, i64 noundef, i64 noundef) #1

declare void @mat_write(ptr noundef, i64 noundef, i64 noundef, double noundef) #1

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

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
define dso_local ptr @softMax(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca double, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.Matrix, ptr %7, i32 0, i32 0
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = icmp ne i64 %9, 1
  br i1 %10, label %11, label %19

11:                                               ; preds = %1
  %12 = load ptr, ptr %2, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.Matrix, ptr %12, i32 0, i32 1
  %14 = load i64, ptr %13, align 8, !tbaa !12
  %15 = icmp ne i64 %14, 1
  br i1 %15, label %16, label %19

16:                                               ; preds = %11
  %17 = load ptr, ptr @stderr, align 8, !tbaa !5
  %18 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef @.str.2) #4
  br label %19

19:                                               ; preds = %16, %11, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = call double @mat_elemSum(ptr noundef %20)
  store double %21, ptr %3, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  store i64 0, ptr %4, align 8, !tbaa !15
  br label %22

22:                                               ; preds = %52, %19
  %23 = load i64, ptr %4, align 8, !tbaa !15
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.Matrix, ptr %24, i32 0, i32 0
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = icmp slt i64 %23, %26
  br i1 %27, label %29, label %28

28:                                               ; preds = %22
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  br label %55

29:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  store i64 0, ptr %6, align 8, !tbaa !15
  br label %30

30:                                               ; preds = %48, %29
  %31 = load i64, ptr %6, align 8, !tbaa !15
  %32 = load ptr, ptr %2, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Matrix, ptr %32, i32 0, i32 1
  %34 = load i64, ptr %33, align 8, !tbaa !12
  %35 = icmp slt i64 %31, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %30
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %51

37:                                               ; preds = %30
  %38 = load ptr, ptr %2, align 8, !tbaa !5
  %39 = load i64, ptr %4, align 8, !tbaa !15
  %40 = load i64, ptr %6, align 8, !tbaa !15
  %41 = load ptr, ptr %2, align 8, !tbaa !5
  %42 = load i64, ptr %4, align 8, !tbaa !15
  %43 = load i64, ptr %6, align 8, !tbaa !15
  %44 = call double @mat_read(ptr noundef %41, i64 noundef %42, i64 noundef %43)
  %45 = call double @exp(double noundef %44) #4
  %46 = load double, ptr %3, align 8, !tbaa !13
  %47 = fdiv double %45, %46
  call void @mat_write(ptr noundef %38, i64 noundef %39, i64 noundef %40, double noundef %47)
  br label %48

48:                                               ; preds = %37
  %49 = load i64, ptr %6, align 8, !tbaa !15
  %50 = add nsw i64 %49, 1
  store i64 %50, ptr %6, align 8, !tbaa !15
  br label %30, !llvm.loop !16

51:                                               ; preds = %36
  br label %52

52:                                               ; preds = %51
  %53 = load i64, ptr %4, align 8, !tbaa !15
  %54 = add nsw i64 %53, 1
  store i64 %54, ptr %4, align 8, !tbaa !15
  br label %22, !llvm.loop !18

55:                                               ; preds = %28
  %56 = load ptr, ptr %2, align 8, !tbaa !5
  store i32 1, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret ptr %56
}

declare double @mat_elemSum(ptr noundef) #1

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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [37 x i8], align 1

declare double @mat_read(ptr noundef, i64 noundef, i64 noundef) local_unnamed_addr #0

declare void @mat_write(ptr noundef, i64 noundef, i64 noundef, double noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @exp(double noundef) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @softMax(ptr noundef returned %0) local_unnamed_addr #3 {
  %2 = load i64, ptr %0, align 8, !tbaa !5
  %.not = icmp eq i64 %2, 1
  br i1 %.not, label %9, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 8
  %5 = load i64, ptr %4, align 8, !tbaa !11
  %.not17 = icmp eq i64 %5, 1
  br i1 %.not17, label %9, label %6

6:                                                ; preds = %3
  %7 = load ptr, ptr @stderr, align 8, !tbaa !12
  %8 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %7, ptr noundef nonnull @.str.2) #4
  br label %9

9:                                                ; preds = %6, %3, %1
  %10 = tail call double @mat_elemSum(ptr noundef nonnull %0) #5
  %11 = load i64, ptr %0, align 8, !tbaa !5
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %.preheader.lr.ph, label %._crit_edge20

.preheader.lr.ph:                                 ; preds = %9
  %13 = getelementptr inbounds i8, ptr %0, i64 8
  %14 = load i64, ptr %13, align 8, !tbaa !11
  %15 = icmp sgt i64 %14, 0
  br i1 %15, label %.preheader, label %._crit_edge20

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %16 = phi i64 [ %19, %._crit_edge ], [ %11, %.preheader.lr.ph ]
  %17 = phi i64 [ %20, %._crit_edge ], [ %14, %.preheader.lr.ph ]
  %.01619 = phi i64 [ %21, %._crit_edge ], [ 0, %.preheader.lr.ph ]
  %18 = icmp sgt i64 %17, 0
  br i1 %18, label %.lr.ph, label %._crit_edge

._crit_edge20:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %9
  ret ptr %0

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre = load i64, ptr %0, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %19 = phi i64 [ %.pre, %._crit_edge.loopexit ], [ %16, %.preheader ]
  %20 = phi i64 [ %27, %._crit_edge.loopexit ], [ %17, %.preheader ]
  %21 = add nuw nsw i64 %.01619, 1
  %22 = icmp slt i64 %21, %19
  br i1 %22, label %.preheader, label %._crit_edge20, !llvm.loop !13

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.018 = phi i64 [ %26, %.lr.ph ], [ 0, %.preheader ]
  %23 = tail call double @mat_read(ptr noundef nonnull %0, i64 noundef %.01619, i64 noundef %.018) #5
  %24 = tail call double @exp(double noundef %23) #5
  %25 = fdiv double %24, %10
  tail call void @mat_write(ptr noundef nonnull %0, i64 noundef %.01619, i64 noundef %.018, double noundef %25) #5
  %26 = add nuw nsw i64 %.018, 1
  %27 = load i64, ptr %13, align 8, !tbaa !11
  %28 = icmp slt i64 %26, %27
  br i1 %28, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !16
}

declare double @mat_elemSum(ptr noundef) local_unnamed_addr #0

attributes #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 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 #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 = { 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 #4 = { cold nounwind }
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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 8, !10, i64 16}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 8}
!12 = !{!10, !10, i64 0}
!13 = distinct !{!13, !14, !15}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!"llvm.loop.unswitch.partial.disable"}
!16 = distinct !{!16, !14}


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