; LLVM IR from rantala/string-sorting :: src/util (timing)

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

@startclock = external hidden global %struct.rusage, align 8
@stopclock = external hidden global %struct.rusage, align 8

; 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 uwtable
define dso_local double @gettime_sys() #1 {
  %1 = alloca %struct.timeval, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr %1) #2
  br label %2

2:                                                ; preds = %0
  %3 = load i64, ptr getelementptr inbounds (%struct.rusage, ptr @stopclock, i32 0, i32 1), align 8, !tbaa !5
  %4 = load i64, ptr getelementptr inbounds (%struct.rusage, ptr @startclock, i32 0, i32 1), align 8, !tbaa !5
  %5 = sub nsw i64 %3, %4
  %6 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 0
  store i64 %5, ptr %6, align 8, !tbaa !11
  %7 = load i64, ptr getelementptr inbounds (%struct.timeval, ptr getelementptr inbounds (%struct.rusage, ptr @stopclock, i32 0, i32 1), i32 0, i32 1), align 8, !tbaa !12
  %8 = load i64, ptr getelementptr inbounds (%struct.timeval, ptr getelementptr inbounds (%struct.rusage, ptr @startclock, i32 0, i32 1), i32 0, i32 1), align 8, !tbaa !12
  %9 = sub nsw i64 %7, %8
  %10 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 1
  store i64 %9, ptr %10, align 8, !tbaa !13
  %11 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 1
  %12 = load i64, ptr %11, align 8, !tbaa !13
  %13 = icmp slt i64 %12, 0
  br i1 %13, label %14, label %21

14:                                               ; preds = %2
  %15 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 0
  %16 = load i64, ptr %15, align 8, !tbaa !11
  %17 = add nsw i64 %16, -1
  store i64 %17, ptr %15, align 8, !tbaa !11
  %18 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 1
  %19 = load i64, ptr %18, align 8, !tbaa !13
  %20 = add nsw i64 %19, 1000000
  store i64 %20, ptr %18, align 8, !tbaa !13
  br label %21

21:                                               ; preds = %14, %2
  br label %22

22:                                               ; preds = %21
  br label %23

23:                                               ; preds = %22
  %24 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !11
  %26 = mul nsw i64 %25, 1000
  %27 = sitofp i64 %26 to double
  %28 = getelementptr inbounds %struct.timeval, ptr %1, i32 0, i32 1
  %29 = load i64, ptr %28, align 8, !tbaa !13
  %30 = sitofp i64 %29 to double
  %31 = fdiv double %30, 1.000000e+03
  %32 = fadd double %27, %31
  call void @llvm.lifetime.end.p0(i64 16, ptr %1) #2
  ret double %32
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !8, i64 16}
!6 = !{!"rusage", !7, i64 0, !7, i64 16, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !9, i64 72, !9, i64 80, !9, i64 88, !9, i64 96, !9, i64 104, !9, i64 112, !9, i64 120, !9, i64 128, !9, i64 136}
!7 = !{!"timeval", !8, i64 0, !8, i64 8}
!8 = !{!"long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!7, !8, i64 0}
!12 = !{!6, !8, i64 24}
!13 = !{!7, !8, i64 8}


; 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.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 }

@startclock = external hidden local_unnamed_addr global %struct.rusage, align 8
@stopclock = external hidden local_unnamed_addr global %struct.rusage, align 8

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local double @gettime_sys() local_unnamed_addr #0 {
  %1 = load i64, ptr getelementptr inbounds (i8, ptr @stopclock, i64 16), align 8, !tbaa !5
  %2 = load i64, ptr getelementptr inbounds (i8, ptr @startclock, i64 16), align 8, !tbaa !5
  %3 = sub i64 %1, %2
  %4 = load i64, ptr getelementptr inbounds (i8, ptr @stopclock, i64 24), align 8, !tbaa !11
  %5 = load i64, ptr getelementptr inbounds (i8, ptr @startclock, i64 24), align 8, !tbaa !11
  %6 = sub nsw i64 %4, %5
  %7 = icmp slt i64 %6, 0
  %8 = add nsw i64 %6, 1000000
  %.sroa.6.0 = select i1 %7, i64 %8, i64 %6
  %.lobit = ashr i64 %6, 63
  %.sroa.0.0 = add nsw i64 %3, %.lobit
  %9 = mul nsw i64 %.sroa.0.0, 1000
  %10 = sitofp i64 %9 to double
  %11 = sitofp i64 %.sroa.6.0 to double
  %12 = fdiv double %11, 1.000000e+03
  %13 = fadd double %12, %10
  ret double %13
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) 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, !8, i64 16}
!6 = !{!"rusage", !7, i64 0, !7, i64 16, !9, i64 32, !9, i64 40, !9, i64 48, !9, i64 56, !9, i64 64, !9, i64 72, !9, i64 80, !9, i64 88, !9, i64 96, !9, i64 104, !9, i64 112, !9, i64 120, !9, i64 128, !9, i64 136}
!7 = !{!"timeval", !8, i64 0, !8, i64 8}
!8 = !{!"long", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!6, !8, i64 24}


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