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

; 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 }
%struct.NN = type { i64, i64, i64, i64, ptr, ptr, ptr, ptr, ptr }
%struct.Layer = type { ptr }

@stderr = external global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [82 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [100 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [58 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

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #2

declare ptr @mat_difmat(ptr noundef, ptr noundef) #3

declare ptr @xmat_traverse(ptr noundef, ptr noundef, ...) #3

declare ptr @mat_multmat(ptr noundef, ptr noundef) #3

declare ptr @mat_transpose(ptr noundef) #3

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

19:                                               ; preds = %4
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Matrix, ptr %20, i32 0, i32 1
  %22 = load i64, ptr %21, align 8, !tbaa !11
  %23 = icmp ne i64 %22, 1
  br i1 %23, label %24, label %27

24:                                               ; preds = %19, %4
  %25 = load ptr, ptr @stderr, align 8, !tbaa !5
  %26 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %25, ptr noundef @.str.13) #5
  call void @exit(i32 noundef 1) #6
  unreachable

27:                                               ; preds = %19
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.Matrix, ptr %28, i32 0, i32 0
  %30 = load i64, ptr %29, align 8, !tbaa !14
  %31 = load ptr, ptr %6, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Matrix, ptr %31, i32 0, i32 0
  %33 = load i64, ptr %32, align 8, !tbaa !14
  %34 = icmp ne i64 %30, %33
  br i1 %34, label %35, label %38

35:                                               ; preds = %27
  %36 = load ptr, ptr @stderr, align 8, !tbaa !5
  %37 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %36, ptr noundef @.str.14) #5
  call void @exit(i32 noundef 1) #6
  unreachable

38:                                               ; preds = %27
  %39 = load double, ptr %8, align 8, !tbaa !9
  %40 = fcmp ole double %39, 0.000000e+00
  br i1 %40, label %41, label %45

41:                                               ; preds = %38
  %42 = load ptr, ptr @stderr, align 8, !tbaa !5
  %43 = load double, ptr %8, align 8, !tbaa !9
  %44 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %42, ptr noundef @.str.15, double noundef %43) #5
  call void @exit(i32 noundef 1) #6
  unreachable

45:                                               ; preds = %38
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #5
  %46 = load ptr, ptr %5, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.NN, ptr %46, i32 0, i32 8
  %48 = load ptr, ptr %47, align 8, !tbaa !15
  %49 = load ptr, ptr %6, align 8, !tbaa !5
  %50 = load ptr, ptr %7, align 8, !tbaa !5
  %51 = call ptr %48(ptr noundef %49, ptr noundef %50)
  store ptr %51, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %52 = load ptr, ptr %9, align 8, !tbaa !5
  %53 = call ptr @mat_copy(ptr noundef %52)
  store ptr %53, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct.NN, ptr %54, i32 0, i32 3
  %56 = load i64, ptr %55, align 8, !tbaa !17
  %57 = add nsw i64 %56, 1
  store i64 %57, ptr %11, align 8, !tbaa !18
  br label %58

58:                                               ; preds = %114, %45
  %59 = load i64, ptr %11, align 8, !tbaa !18
  %60 = icmp sgt i64 %59, 0
  br i1 %60, label %62, label %61

61:                                               ; preds = %58
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  br label %117

62:                                               ; preds = %58
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %63 = load ptr, ptr %5, align 8, !tbaa !5
  %64 = getelementptr inbounds %struct.NN, ptr %63, i32 0, i32 5
  %65 = load ptr, ptr %64, align 8, !tbaa !19
  %66 = load i64, ptr %11, align 8, !tbaa !18
  %67 = sub nsw i64 %66, 1
  %68 = getelementptr inbounds ptr, ptr %65, i64 %67
  %69 = load ptr, ptr %68, align 8, !tbaa !5
  %70 = call ptr @mat_transpose(ptr noundef %69)
  store ptr %70, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %71 = load ptr, ptr %10, align 8, !tbaa !5
  %72 = call ptr @mat_transpose(ptr noundef %71)
  store ptr %72, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #5
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = load ptr, ptr %13, align 8, !tbaa !5
  %75 = call ptr @mat_multmat(ptr noundef %73, ptr noundef %74)
  store ptr %75, ptr %14, align 8, !tbaa !5
  %76 = load ptr, ptr %5, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.NN, ptr %76, i32 0, i32 4
  %78 = load ptr, ptr %77, align 8, !tbaa !20
  %79 = load i64, ptr %11, align 8, !tbaa !18
  %80 = getelementptr inbounds ptr, ptr %78, i64 %79
  %81 = load ptr, ptr %80, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Layer, ptr %81, i32 0, i32 0
  %83 = load ptr, ptr %82, align 8, !tbaa !21
  %84 = load ptr, ptr %14, align 8, !tbaa !5
  %85 = load double, ptr %8, align 8, !tbaa !9
  %86 = call ptr @mat_multscal(ptr noundef %84, double noundef %85)
  %87 = call ptr @mat_difmat(ptr noundef %83, ptr noundef %86)
  %88 = load ptr, ptr %5, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.NN, ptr %88, i32 0, i32 4
  %90 = load ptr, ptr %89, align 8, !tbaa !20
  %91 = load i64, ptr %11, align 8, !tbaa !18
  %92 = getelementptr inbounds ptr, ptr %90, i64 %91
  %93 = load ptr, ptr %92, align 8, !tbaa !5
  %94 = getelementptr inbounds %struct.Layer, ptr %93, i32 0, i32 0
  store ptr %87, ptr %94, align 8, !tbaa !21
  %95 = load i64, ptr %11, align 8, !tbaa !18
  %96 = icmp sgt i64 %95, 1
  br i1 %96, label %97, label %113

97:                                               ; preds = %62
  %98 = load ptr, ptr %5, align 8, !tbaa !5
  %99 = getelementptr inbounds %struct.NN, ptr %98, i32 0, i32 4
  %100 = load ptr, ptr %99, align 8, !tbaa !20
  %101 = load i64, ptr %11, align 8, !tbaa !18
  %102 = getelementptr inbounds ptr, ptr %100, i64 %101
  %103 = load ptr, ptr %102, align 8, !tbaa !5
  %104 = getelementptr inbounds %struct.Layer, ptr %103, i32 0, i32 0
  %105 = load ptr, ptr %104, align 8, !tbaa !21
  %106 = load ptr, ptr %10, align 8, !tbaa !5
  %107 = call ptr @mat_multmat(ptr noundef %105, ptr noundef %106)
  store ptr %107, ptr %10, align 8, !tbaa !5
  %108 = load ptr, ptr %10, align 8, !tbaa !5
  %109 = load ptr, ptr %5, align 8, !tbaa !5
  %110 = getelementptr inbounds %struct.NN, ptr %109, i32 0, i32 7
  %111 = load ptr, ptr %110, align 8, !tbaa !23
  %112 = call ptr (ptr, ptr, ...) @xmat_traverse(ptr noundef %108, ptr noundef %111, i32 noundef 0)
  store ptr %112, ptr %10, align 8, !tbaa !5
  br label %113

113:                                              ; preds = %97, %62
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %114

114:                                              ; preds = %113
  %115 = load i64, ptr %11, align 8, !tbaa !18
  %116 = add nsw i64 %115, -1
  store i64 %116, ptr %11, align 8, !tbaa !18
  br label %58, !llvm.loop !24

117:                                              ; preds = %61
  %118 = load ptr, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #5
  ret ptr %118
}

declare ptr @mat_copy(ptr noundef) #3

declare ptr @mat_multscal(ptr noundef, double noundef) #3

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 = { noreturn 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 = { "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 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 #5 = { nounwind }
attributes #6 = { noreturn 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, !13, i64 8}
!12 = !{!"", !13, i64 0, !13, i64 8, !6, i64 16}
!13 = !{!"long long", !7, i64 0}
!14 = !{!12, !13, i64 0}
!15 = !{!16, !6, i64 64}
!16 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !6, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !6, i64 64}
!17 = !{!16, !13, i64 24}
!18 = !{!13, !13, i64 0}
!19 = !{!16, !6, i64 40}
!20 = !{!16, !6, i64 32}
!21 = !{!22, !6, i64 0}
!22 = !{!"", !6, i64 0}
!23 = !{!16, !6, i64 56}
!24 = distinct !{!24, !25}
!25 = !{!"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"

@stderr = external local_unnamed_addr global ptr, align 8
@.str.13 = external hidden unnamed_addr constant [82 x i8], align 1
@.str.14 = external hidden unnamed_addr constant [100 x i8], align 1
@.str.15 = external hidden unnamed_addr constant [58 x i8], align 1

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

declare ptr @mat_difmat(ptr noundef, ptr noundef) local_unnamed_addr #2

declare ptr @xmat_traverse(ptr noundef, ptr noundef, ...) local_unnamed_addr #2

declare ptr @mat_multmat(ptr noundef, ptr noundef) local_unnamed_addr #2

declare ptr @mat_transpose(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @nn_backward(ptr noundef readonly returned %0, ptr noundef %1, ptr noundef %2, double noundef %3) local_unnamed_addr #3 {
  %5 = getelementptr inbounds i8, ptr %1, i64 8
  %6 = load i64, ptr %5, align 8, !tbaa !5
  %.not = icmp eq i64 %6, 1
  br i1 %.not, label %7, label %10

7:                                                ; preds = %4
  %8 = getelementptr inbounds i8, ptr %2, i64 8
  %9 = load i64, ptr %8, align 8, !tbaa !5
  %.not33 = icmp eq i64 %9, 1
  br i1 %.not33, label %13, label %10

10:                                               ; preds = %7, %4
  %11 = load ptr, ptr @stderr, align 8, !tbaa !11
  %12 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %11, ptr noundef nonnull @.str.13) #4
  tail call void @exit(i32 noundef 1) #5
  unreachable

13:                                               ; preds = %7
  %14 = load i64, ptr %2, align 8, !tbaa !12
  %15 = load i64, ptr %1, align 8, !tbaa !12
  %.not34 = icmp eq i64 %14, %15
  br i1 %.not34, label %19, label %16

16:                                               ; preds = %13
  %17 = load ptr, ptr @stderr, align 8, !tbaa !11
  %18 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %17, ptr noundef nonnull @.str.14) #4
  tail call void @exit(i32 noundef 1) #5
  unreachable

19:                                               ; preds = %13
  %20 = fcmp ugt double %3, 0.000000e+00
  br i1 %20, label %24, label %21

21:                                               ; preds = %19
  %22 = load ptr, ptr @stderr, align 8, !tbaa !11
  %23 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str.15, double noundef %3) #4
  tail call void @exit(i32 noundef 1) #5
  unreachable

24:                                               ; preds = %19
  %25 = getelementptr inbounds i8, ptr %0, i64 64
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = tail call ptr %26(ptr noundef nonnull %1, ptr noundef nonnull %2) #6
  %28 = tail call ptr @mat_copy(ptr noundef %27) #6
  %29 = getelementptr inbounds i8, ptr %0, i64 24
  %30 = load i64, ptr %29, align 8, !tbaa !15
  %31 = icmp sgt i64 %30, -1
  br i1 %31, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %24
  %32 = add nuw nsw i64 %30, 1
  %33 = getelementptr inbounds i8, ptr %0, i64 40
  %34 = getelementptr inbounds i8, ptr %0, i64 32
  %35 = getelementptr inbounds i8, ptr %0, i64 56
  br label %36

._crit_edge:                                      ; preds = %36, %24
  ret ptr %0

36:                                               ; preds = %53, %.lr.ph
  %.037 = phi ptr [ %28, %.lr.ph ], [ %58, %53 ]
  %.03036 = phi i64 [ %32, %.lr.ph ], [ %59, %53 ]
  %37 = load ptr, ptr %33, align 8, !tbaa !16
  %38 = getelementptr ptr, ptr %37, i64 %.03036
  %39 = getelementptr i8, ptr %38, i64 -8
  %40 = load ptr, ptr %39, align 8, !tbaa !11
  %41 = tail call ptr @mat_transpose(ptr noundef %40) #6
  %42 = tail call ptr @mat_transpose(ptr noundef %.037) #6
  %43 = tail call ptr @mat_multmat(ptr noundef %41, ptr noundef %42) #6
  %44 = load ptr, ptr %34, align 8, !tbaa !17
  %45 = getelementptr inbounds ptr, ptr %44, i64 %.03036
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = load ptr, ptr %46, align 8, !tbaa !18
  %48 = tail call ptr @mat_multscal(ptr noundef %43, double noundef %3) #6
  %49 = tail call ptr @mat_difmat(ptr noundef %47, ptr noundef %48) #6
  %50 = load ptr, ptr %34, align 8, !tbaa !17
  %51 = getelementptr inbounds ptr, ptr %50, i64 %.03036
  %52 = load ptr, ptr %51, align 8, !tbaa !11
  store ptr %49, ptr %52, align 8, !tbaa !18
  %.not35 = icmp eq i64 %.03036, 1
  br i1 %.not35, label %._crit_edge, label %53

53:                                               ; preds = %36
  %54 = load ptr, ptr %51, align 8, !tbaa !11
  %55 = load ptr, ptr %54, align 8, !tbaa !18
  %56 = tail call ptr @mat_multmat(ptr noundef %55, ptr noundef %.037) #6
  %57 = load ptr, ptr %35, align 8, !tbaa !20
  %58 = tail call ptr (ptr, ptr, ...) @xmat_traverse(ptr noundef %56, ptr noundef %57, i32 noundef 0) #6
  %59 = add nsw i64 %.03036, -1
  br label %36, !llvm.loop !21
}

declare ptr @mat_copy(ptr noundef) local_unnamed_addr #2

declare ptr @mat_multscal(ptr noundef, double noundef) local_unnamed_addr #2

attributes #0 = { 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 #1 = { noreturn 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 = { "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 = { noreturn nounwind }
attributes #6 = { 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 8}
!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 = !{!10, !10, i64 0}
!12 = !{!6, !7, i64 0}
!13 = !{!14, !10, i64 64}
!14 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64}
!15 = !{!14, !7, i64 24}
!16 = !{!14, !10, i64 40}
!17 = !{!14, !10, i64 32}
!18 = !{!19, !10, i64 0}
!19 = !{!"", !10, i64 0}
!20 = !{!14, !10, i64 56}
!21 = distinct !{!21, !22}
!22 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass