; LLVM IR from cvxgrp/scs :: src/util.ll (scs_tocq)

; 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.timespec = type { i64, i64 }
%struct._scs_timer = type { %struct.timespec, %struct.timespec }

; Function Attrs: nounwind
declare i32 @clock_gettime(i32 noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local double @_scs_tocq(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca %struct.timespec, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %3) #4
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds %struct._scs_timer, ptr %4, i32 0, i32 1
  %6 = call i32 @clock_gettime(i32 noundef 1, ptr noundef %5) #4
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct._scs_timer, ptr %7, i32 0, i32 1
  %9 = getelementptr inbounds %struct.timespec, ptr %8, i32 0, i32 1
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct._scs_timer, ptr %11, i32 0, i32 0
  %13 = getelementptr inbounds %struct.timespec, ptr %12, i32 0, i32 1
  %14 = load i64, ptr %13, align 8, !tbaa !13
  %15 = sub nsw i64 %10, %14
  %16 = icmp slt i64 %15, 0
  br i1 %16, label %17, label %43

17:                                               ; preds = %1
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct._scs_timer, ptr %18, i32 0, i32 1
  %20 = getelementptr inbounds %struct.timespec, ptr %19, i32 0, i32 0
  %21 = load i64, ptr %20, align 8, !tbaa !14
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct._scs_timer, ptr %22, i32 0, i32 0
  %24 = getelementptr inbounds %struct.timespec, ptr %23, i32 0, i32 0
  %25 = load i64, ptr %24, align 8, !tbaa !15
  %26 = sub nsw i64 %21, %25
  %27 = sub nsw i64 %26, 1
  %28 = getelementptr inbounds %struct.timespec, ptr %3, i32 0, i32 0
  store i64 %27, ptr %28, align 8, !tbaa !16
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct._scs_timer, ptr %29, i32 0, i32 1
  %31 = getelementptr inbounds %struct.timespec, ptr %30, i32 0, i32 1
  %32 = load i64, ptr %31, align 8, !tbaa !9
  %33 = sitofp i64 %32 to double
  %34 = fadd double 1.000000e+09, %33
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct._scs_timer, ptr %35, i32 0, i32 0
  %37 = getelementptr inbounds %struct.timespec, ptr %36, i32 0, i32 1
  %38 = load i64, ptr %37, align 8, !tbaa !13
  %39 = sitofp i64 %38 to double
  %40 = fsub double %34, %39
  %41 = fptosi double %40 to i64
  %42 = getelementptr inbounds %struct.timespec, ptr %3, i32 0, i32 1
  store i64 %41, ptr %42, align 8, !tbaa !17
  br label %64

43:                                               ; preds = %1
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct._scs_timer, ptr %44, i32 0, i32 1
  %46 = getelementptr inbounds %struct.timespec, ptr %45, i32 0, i32 0
  %47 = load i64, ptr %46, align 8, !tbaa !14
  %48 = load ptr, ptr %2, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct._scs_timer, ptr %48, i32 0, i32 0
  %50 = getelementptr inbounds %struct.timespec, ptr %49, i32 0, i32 0
  %51 = load i64, ptr %50, align 8, !tbaa !15
  %52 = sub nsw i64 %47, %51
  %53 = getelementptr inbounds %struct.timespec, ptr %3, i32 0, i32 0
  store i64 %52, ptr %53, align 8, !tbaa !16
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = getelementptr inbounds %struct._scs_timer, ptr %54, i32 0, i32 1
  %56 = getelementptr inbounds %struct.timespec, ptr %55, i32 0, i32 1
  %57 = load i64, ptr %56, align 8, !tbaa !9
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct._scs_timer, ptr %58, i32 0, i32 0
  %60 = getelementptr inbounds %struct.timespec, ptr %59, i32 0, i32 1
  %61 = load i64, ptr %60, align 8, !tbaa !13
  %62 = sub nsw i64 %57, %61
  %63 = getelementptr inbounds %struct.timespec, ptr %3, i32 0, i32 1
  store i64 %62, ptr %63, align 8, !tbaa !17
  br label %64

64:                                               ; preds = %43, %17
  %65 = getelementptr inbounds %struct.timespec, ptr %3, i32 0, i32 0
  %66 = load i64, ptr %65, align 8, !tbaa !16
  %67 = sitofp i64 %66 to double
  %68 = getelementptr inbounds %struct.timespec, ptr %3, i32 0, i32 1
  %69 = load i64, ptr %68, align 8, !tbaa !17
  %70 = sitofp i64 %69 to double
  %71 = fdiv double %70, 1.000000e+06
  %72 = call double @llvm.fmuladd.f64(double %67, double 1.000000e+03, double %71)
  call void @llvm.lifetime.end.p0(i64 16, ptr %3) #4
  ret double %72
}

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #3

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

attributes #0 = { 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 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !12, i64 24}
!10 = !{!"_scs_timer", !11, i64 0, !11, i64 16}
!11 = !{!"timespec", !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 0}
!16 = !{!11, !12, i64 0}
!17 = !{!11, !12, 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"

; Function Attrs: nounwind
declare i32 @clock_gettime(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local double @_scs_tocq(ptr noundef %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 16
  %3 = tail call i32 @clock_gettime(i32 noundef 1, ptr noundef nonnull %2) #3
  %4 = getelementptr inbounds i8, ptr %0, i64 24
  %5 = load i64, ptr %4, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load i64, ptr %6, align 8, !tbaa !11
  %8 = sub nsw i64 %5, %7
  %9 = icmp slt i64 %8, 0
  %10 = load i64, ptr %2, align 8, !tbaa !12
  %11 = load i64, ptr %0, align 8, !tbaa !13
  br i1 %9, label %12, label %20

12:                                               ; preds = %1
  %13 = xor i64 %11, -1
  %14 = add i64 %10, %13
  %15 = sitofp i64 %5 to double
  %16 = fadd double %15, 1.000000e+09
  %17 = sitofp i64 %7 to double
  %18 = fsub double %16, %17
  %19 = fptosi double %18 to i64
  br label %22

20:                                               ; preds = %1
  %21 = sub nsw i64 %10, %11
  br label %22

22:                                               ; preds = %20, %12
  %.sroa.5.0 = phi i64 [ %19, %12 ], [ %8, %20 ]
  %.sroa.0.0 = phi i64 [ %14, %12 ], [ %21, %20 ]
  %23 = sitofp i64 %.sroa.0.0 to double
  %24 = sitofp i64 %.sroa.5.0 to double
  %25 = fdiv double %24, 1.000000e+06
  %26 = tail call double @llvm.fmuladd.f64(double %23, double 1.000000e+03, double %25)
  ret double %26
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #2

attributes #0 = { 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 = { 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !8, i64 24}
!6 = !{!"_scs_timer", !7, i64 0, !7, i64 16}
!7 = !{!"timespec", !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 8}
!12 = !{!6, !8, i64 16}
!13 = !{!6, !8, i64 0}


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