; LLVM IR from ctlst-tech/catpilot :: os/freertos/osa/FreeRTOS-Plus-POSIX/source (FreeRTOS_POSIX_timer)

; 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.timer_internal = type { %struct.xSTATIC_TIMER, %struct.sigevent, i32 }
%struct.xSTATIC_TIMER = type { ptr, %struct.xSTATIC_LIST_ITEM, i32, ptr, ptr, i8 }
%struct.xSTATIC_LIST_ITEM = type { i32, [4 x ptr] }
%struct.sigevent = type { i32, i32, %union.sigval, ptr, ptr }
%union.sigval = type { ptr }
%struct.itimerspec = type { %struct.timespec, %struct.timespec }
%struct.timespec = type { i64, i64 }

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

declare ptr @pvTimerGetTimerID(ptr noundef) #1

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

declare i64 @xTimerIsTimerActive(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local i32 @timer_gettime(ptr noundef %0, ptr noundef %1) #2 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %9, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = call ptr @pvTimerGetTimerID(ptr noundef %10)
  store ptr %11, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = call i32 @xTimerGetExpiryTime(ptr noundef %12)
  %14 = call i32 @xTaskGetTickCount()
  %15 = sub i32 %13, %14
  store i32 %15, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %16 = load ptr, ptr %6, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.timer_internal, ptr %16, i32 0, i32 2
  %18 = load i32, ptr %17, align 8, !tbaa !11
  store i32 %18, ptr %8, align 4, !tbaa !9
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = call i64 @xTimerIsTimerActive(ptr noundef %19)
  %21 = icmp ne i64 %20, 0
  br i1 %21, label %22, label %36

22:                                               ; preds = %2
  %23 = load i32, ptr %7, align 4, !tbaa !9
  %24 = udiv i32 %23, 1000
  %25 = zext i32 %24 to i64
  %26 = load ptr, ptr %4, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.itimerspec, ptr %26, i32 0, i32 1
  %28 = getelementptr inbounds %struct.timespec, ptr %27, i32 0, i32 0
  store i64 %25, ptr %28, align 8, !tbaa !16
  %29 = load i32, ptr %7, align 4, !tbaa !9
  %30 = urem i32 %29, 1000
  %31 = zext i32 %30 to i64
  %32 = mul nsw i64 %31, 1000000
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = getelementptr inbounds %struct.itimerspec, ptr %33, i32 0, i32 1
  %35 = getelementptr inbounds %struct.timespec, ptr %34, i32 0, i32 1
  store i64 %32, ptr %35, align 8, !tbaa !20
  br label %43

36:                                               ; preds = %2
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.itimerspec, ptr %37, i32 0, i32 1
  %39 = getelementptr inbounds %struct.timespec, ptr %38, i32 0, i32 0
  store i64 0, ptr %39, align 8, !tbaa !16
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.itimerspec, ptr %40, i32 0, i32 1
  %42 = getelementptr inbounds %struct.timespec, ptr %41, i32 0, i32 1
  store i64 0, ptr %42, align 8, !tbaa !20
  br label %43

43:                                               ; preds = %36, %22
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = icmp ne i32 %44, -1
  br i1 %45, label %46, label %60

46:                                               ; preds = %43
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = udiv i32 %47, 1000
  %49 = zext i32 %48 to i64
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.itimerspec, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds %struct.timespec, ptr %51, i32 0, i32 0
  store i64 %49, ptr %52, align 8, !tbaa !21
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = urem i32 %53, 1000
  %55 = zext i32 %54 to i64
  %56 = mul nsw i64 %55, 1000000
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.itimerspec, ptr %57, i32 0, i32 0
  %59 = getelementptr inbounds %struct.timespec, ptr %58, i32 0, i32 1
  store i64 %56, ptr %59, align 8, !tbaa !22
  br label %67

60:                                               ; preds = %43
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.itimerspec, ptr %61, i32 0, i32 0
  %63 = getelementptr inbounds %struct.timespec, ptr %62, i32 0, i32 0
  store i64 0, ptr %63, align 8, !tbaa !21
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.itimerspec, ptr %64, i32 0, i32 0
  %66 = getelementptr inbounds %struct.timespec, ptr %65, i32 0, i32 1
  store i64 0, ptr %66, align 8, !tbaa !22
  br label %67

67:                                               ; preds = %60, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret i32 0
}

declare i32 @xTaskGetTickCount() #1

declare i32 @xTimerGetExpiryTime(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 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 #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, !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 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 112}
!12 = !{!"timer_internal", !13, i64 0, !15, i64 80, !10, i64 112}
!13 = !{!"xSTATIC_TIMER", !6, i64 0, !14, i64 8, !10, i64 48, !6, i64 56, !6, i64 64, !7, i64 72}
!14 = !{!"xSTATIC_LIST_ITEM", !10, i64 0, !7, i64 8}
!15 = !{!"sigevent", !10, i64 0, !10, i64 4, !7, i64 8, !6, i64 16, !6, i64 24}
!16 = !{!17, !19, i64 16}
!17 = !{!"itimerspec", !18, i64 0, !18, i64 16}
!18 = !{!"timespec", !19, i64 0, !19, i64 8}
!19 = !{!"long", !7, i64 0}
!20 = !{!17, !19, i64 24}
!21 = !{!17, !19, i64 0}
!22 = !{!17, !19, 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"

declare ptr @pvTimerGetTimerID(ptr noundef) local_unnamed_addr #0

declare i64 @xTimerIsTimerActive(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @timer_gettime(ptr noundef %0, ptr nocapture noundef writeonly %1) local_unnamed_addr #1 {
  %3 = tail call ptr @pvTimerGetTimerID(ptr noundef %0) #2
  %4 = tail call i32 @xTimerGetExpiryTime(ptr noundef %0) #2
  %5 = tail call i32 @xTaskGetTickCount() #2
  %6 = getelementptr inbounds i8, ptr %3, i64 112
  %7 = load i32, ptr %6, align 8, !tbaa !5
  %8 = tail call i64 @xTimerIsTimerActive(ptr noundef %0) #2
  %.not = icmp eq i64 %8, 0
  br i1 %.not, label %15, label %9

9:                                                ; preds = %2
  %10 = sub i32 %4, %5
  %11 = udiv i32 %10, 1000
  %12 = zext nneg i32 %11 to i64
  %13 = urem i32 %10, 1000
  %narrow = mul nuw nsw i32 %13, 1000000
  %14 = zext nneg i32 %narrow to i64
  br label %15

15:                                               ; preds = %2, %9
  %.sink19 = phi i64 [ %12, %9 ], [ 0, %2 ]
  %.sink = phi i64 [ %14, %9 ], [ 0, %2 ]
  %16 = getelementptr inbounds i8, ptr %1, i64 16
  store i64 %.sink19, ptr %16, align 8
  %17 = getelementptr inbounds i8, ptr %1, i64 24
  store i64 %.sink, ptr %17, align 8
  %.not17 = icmp eq i32 %7, -1
  br i1 %.not17, label %23, label %18

18:                                               ; preds = %15
  %19 = udiv i32 %7, 1000
  %20 = zext nneg i32 %19 to i64
  %21 = urem i32 %7, 1000
  %narrow18 = mul nuw nsw i32 %21, 1000000
  %22 = zext nneg i32 %narrow18 to i64
  br label %23

23:                                               ; preds = %15, %18
  %.sink21 = phi i64 [ %20, %18 ], [ 0, %15 ]
  %.sink20 = phi i64 [ %22, %18 ], [ 0, %15 ]
  store i64 %.sink21, ptr %1, align 8
  %24 = getelementptr inbounds i8, ptr %1, i64 8
  store i64 %.sink20, ptr %24, align 8
  ret i32 0
}

declare i32 @xTaskGetTickCount() local_unnamed_addr #0

declare i32 @xTimerGetExpiryTime(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 = { 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, !12, i64 112}
!6 = !{!"timer_internal", !7, i64 0, !13, i64 80, !12, i64 112}
!7 = !{!"xSTATIC_TIMER", !8, i64 0, !11, i64 8, !12, i64 48, !8, i64 56, !8, i64 64, !9, i64 72}
!8 = !{!"any pointer", !9, i64 0}
!9 = !{!"omnipotent char", !10, i64 0}
!10 = !{!"Simple C/C++ TBAA"}
!11 = !{!"xSTATIC_LIST_ITEM", !12, i64 0, !9, i64 8}
!12 = !{!"int", !9, i64 0}
!13 = !{!"sigevent", !12, i64 0, !12, i64 4, !9, i64 8, !8, i64 16, !8, i64 24}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,TailCallElimPass