; LLVM IR from haytham-hammioui/CS50x-2024 :: Week 5 Data Structures/Problem set 5/speller (speller)

; 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.rusage = type { %struct.timeval, %struct.timeval, %union.anon, %union.anon.0, %union.anon.1, %union.anon.2, %union.anon.3, %union.anon.4, %union.anon.5, %union.anon.6, %union.anon.7, %union.anon.8, %union.anon.9, %union.anon.10, %union.anon.11, %union.anon.12 }
%struct.timeval = type { i64, i64 }
%union.anon = type { i64 }
%union.anon.0 = type { i64 }
%union.anon.1 = type { i64 }
%union.anon.2 = type { i64 }
%union.anon.3 = type { i64 }
%union.anon.4 = type { i64 }
%union.anon.5 = type { i64 }
%union.anon.6 = type { i64 }
%union.anon.7 = type { i64 }
%union.anon.8 = type { i64 }
%union.anon.9 = type { i64 }
%union.anon.10 = type { i64 }
%union.anon.11 = type { i64 }
%union.anon.12 = type { i64 }

; Function Attrs: nounwind uwtable
define dso_local double @calculate(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca double, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %11, label %8

8:                                                ; preds = %2
  %9 = load ptr, ptr %5, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %8, %2
  store double 0.000000e+00, ptr %3, align 8
  br label %58

12:                                               ; preds = %8
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = getelementptr inbounds %struct.rusage, ptr %13, i32 0, i32 0
  %15 = getelementptr inbounds %struct.timeval, ptr %14, i32 0, i32 0
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = mul nsw i64 %16, 1000000
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.rusage, ptr %18, i32 0, i32 0
  %20 = getelementptr inbounds %struct.timeval, ptr %19, i32 0, i32 1
  %21 = load i64, ptr %20, align 8, !tbaa !13
  %22 = add nsw i64 %17, %21
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.rusage, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds %struct.timeval, ptr %24, i32 0, i32 0
  %26 = load i64, ptr %25, align 8, !tbaa !9
  %27 = mul nsw i64 %26, 1000000
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.rusage, ptr %28, i32 0, i32 0
  %30 = getelementptr inbounds %struct.timeval, ptr %29, i32 0, i32 1
  %31 = load i64, ptr %30, align 8, !tbaa !13
  %32 = add nsw i64 %27, %31
  %33 = sub nsw i64 %22, %32
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.rusage, ptr %34, i32 0, i32 1
  %36 = getelementptr inbounds %struct.timeval, ptr %35, i32 0, i32 0
  %37 = load i64, ptr %36, align 8, !tbaa !14
  %38 = mul nsw i64 %37, 1000000
  %39 = load ptr, ptr %5, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.rusage, ptr %39, i32 0, i32 1
  %41 = getelementptr inbounds %struct.timeval, ptr %40, i32 0, i32 1
  %42 = load i64, ptr %41, align 8, !tbaa !15
  %43 = add nsw i64 %38, %42
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.rusage, ptr %44, i32 0, i32 1
  %46 = getelementptr inbounds %struct.timeval, ptr %45, i32 0, i32 0
  %47 = load i64, ptr %46, align 8, !tbaa !14
  %48 = mul nsw i64 %47, 1000000
  %49 = load ptr, ptr %4, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.rusage, ptr %49, i32 0, i32 1
  %51 = getelementptr inbounds %struct.timeval, ptr %50, i32 0, i32 1
  %52 = load i64, ptr %51, align 8, !tbaa !15
  %53 = add nsw i64 %48, %52
  %54 = sub nsw i64 %43, %53
  %55 = add nsw i64 %33, %54
  %56 = sitofp i64 %55 to double
  %57 = fdiv double %56, 1.000000e+06
  store double %57, ptr %3, align 8
  br label %58

58:                                               ; preds = %12, %11
  %59 = load double, ptr %3, align 8
  ret double %59
}

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

!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, !12, i64 0}
!10 = !{!"rusage", !11, i64 0, !11, i64 16, !7, i64 32, !7, i64 40, !7, i64 48, !7, i64 56, !7, i64 64, !7, i64 72, !7, i64 80, !7, i64 88, !7, i64 96, !7, i64 104, !7, i64 112, !7, i64 120, !7, i64 128, !7, i64 136}
!11 = !{!"timeval", !12, i64 0, !12, i64 8}
!12 = !{!"long", !7, i64 0}
!13 = !{!10, !12, i64 8}
!14 = !{!10, !12, i64 16}
!15 = !{!10, !12, i64 24}


; 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 norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local double @calculate(ptr noundef readonly %0, ptr noundef readonly %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  %4 = icmp eq ptr %1, null
  %or.cond = or i1 %3, %4
  br i1 %or.cond, label %20, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds i8, ptr %1, i64 16
  %7 = getelementptr inbounds i8, ptr %0, i64 16
  %8 = load <2 x i64>, ptr %1, align 8, !tbaa !5
  %9 = load <2 x i64>, ptr %0, align 8, !tbaa !5
  %10 = load <2 x i64>, ptr %6, align 8, !tbaa !5
  %11 = load <2 x i64>, ptr %7, align 8, !tbaa !5
  %12 = add <2 x i64> %8, %10
  %13 = add <2 x i64> %9, %11
  %14 = sub <2 x i64> %12, %13
  %15 = extractelement <2 x i64> %14, i64 0
  %reass.mul = mul i64 %15, 1000000
  %16 = extractelement <2 x i64> %14, i64 1
  %17 = add i64 %16, %reass.mul
  %18 = sitofp i64 %17 to double
  %19 = fdiv double %18, 1.000000e+06
  br label %20

20:                                               ; preds = %2, %5
  %.0 = phi double [ %19, %5 ], [ 0.000000e+00, %2 ]
  ret double %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) 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" }

!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 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass