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

; Function Attrs: nounwind uwtable
define dso_local void @prvTimerCallback(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %5 = load ptr, ptr %2, align 8, !tbaa !5
  %6 = call ptr @pvTimerGetTimerID(ptr noundef %5)
  store ptr %6, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #4
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = icmp ne ptr %7, null
  %9 = zext i1 %8 to i32
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %13

11:                                               ; preds = %1
  call void @vPortRaiseBASEPRI()
  br label %12

12:                                               ; preds = %12, %11
  br label %12

13:                                               ; preds = %1
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.timer_internal, ptr %14, i32 0, i32 1
  %16 = getelementptr inbounds %struct.sigevent, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !9
  %18 = icmp ne i32 %17, 1
  %19 = zext i1 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %23

21:                                               ; preds = %13
  call void @vPortRaiseBASEPRI()
  br label %22

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

23:                                               ; preds = %13
  %24 = load ptr, ptr %3, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.timer_internal, ptr %24, i32 0, i32 2
  %26 = load i32, ptr %25, align 8, !tbaa !15
  %27 = icmp ugt i32 %26, 0
  br i1 %27, label %28, label %34

28:                                               ; preds = %23
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.timer_internal, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 8, !tbaa !15
  %33 = call i64 @xTimerGenericCommand(ptr noundef %29, i64 noundef 4, i32 noundef %32, ptr noundef null, i32 noundef 0)
  br label %34

34:                                               ; preds = %28, %23
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.timer_internal, ptr %35, i32 0, i32 1
  %37 = getelementptr inbounds %struct.sigevent, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !9
  %39 = icmp eq i32 %38, 2
  br i1 %39, label %40, label %71

40:                                               ; preds = %34
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.timer_internal, ptr %41, i32 0, i32 1
  %43 = getelementptr inbounds %struct.sigevent, ptr %42, i32 0, i32 4
  %44 = load ptr, ptr %43, align 8, !tbaa !16
  %45 = icmp eq ptr %44, null
  br i1 %45, label %46, label %56

46:                                               ; preds = %40
  %47 = load ptr, ptr %3, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.timer_internal, ptr %47, i32 0, i32 1
  %49 = getelementptr inbounds %struct.sigevent, ptr %48, i32 0, i32 3
  %50 = load ptr, ptr %49, align 8, !tbaa !17
  %51 = load ptr, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.timer_internal, ptr %51, i32 0, i32 1
  %53 = getelementptr inbounds %struct.sigevent, ptr %52, i32 0, i32 2
  %54 = getelementptr inbounds %union.sigval, ptr %53, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8
  call void %50(ptr %55)
  br label %70

56:                                               ; preds = %40
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.timer_internal, ptr %57, i32 0, i32 1
  %59 = getelementptr inbounds %struct.sigevent, ptr %58, i32 0, i32 4
  %60 = load ptr, ptr %59, align 8, !tbaa !16
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.timer_internal, ptr %61, i32 0, i32 1
  %63 = getelementptr inbounds %struct.sigevent, ptr %62, i32 0, i32 3
  %64 = load ptr, ptr %63, align 8, !tbaa !17
  %65 = load ptr, ptr %3, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.timer_internal, ptr %65, i32 0, i32 1
  %67 = getelementptr inbounds %struct.sigevent, ptr %66, i32 0, i32 2
  %68 = load ptr, ptr %67, align 8, !tbaa !18
  %69 = call i32 @pthread_create(ptr noundef %4, ptr noundef %60, ptr noundef %64, ptr noundef %68)
  br label %70

70:                                               ; preds = %56, %46
  br label %71

71:                                               ; preds = %70, %34
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret void
}

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

declare ptr @pvTimerGetTimerID(ptr noundef) #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() #3

declare i64 @xTimerGenericCommand(ptr noundef, i64 noundef, i32 noundef, ptr noundef, i32 noundef) #2

declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

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

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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { alwaysinline 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 = { 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, !13, i64 80}
!10 = !{!"timer_internal", !11, i64 0, !14, i64 80, !13, i64 112}
!11 = !{!"xSTATIC_TIMER", !6, i64 0, !12, i64 8, !13, i64 48, !6, i64 56, !6, i64 64, !7, i64 72}
!12 = !{!"xSTATIC_LIST_ITEM", !13, i64 0, !7, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!"sigevent", !13, i64 0, !13, i64 4, !7, i64 8, !6, i64 16, !6, i64 24}
!15 = !{!10, !13, i64 112}
!16 = !{!10, !6, i64 104}
!17 = !{!10, !6, i64 96}
!18 = !{!7, !7, i64 0}


; 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 uwtable
define dso_local void @prvTimerCallback(ptr noundef %0) local_unnamed_addr #0 {
  %2 = alloca ptr, align 8
  %3 = tail call ptr @pvTimerGetTimerID(ptr noundef %0) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %4, label %6

4:                                                ; preds = %1
  tail call void @vPortRaiseBASEPRI()
  br label %5

5:                                                ; preds = %5, %4
  br label %5

6:                                                ; preds = %1
  %7 = getelementptr inbounds i8, ptr %3, i64 80
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %.not14 = icmp eq i32 %8, 1
  br i1 %.not14, label %9, label %11

9:                                                ; preds = %6
  tail call void @vPortRaiseBASEPRI()
  br label %10

10:                                               ; preds = %10, %9
  br label %10

11:                                               ; preds = %6
  %12 = getelementptr inbounds i8, ptr %3, i64 112
  %13 = load i32, ptr %12, align 8, !tbaa !14
  %.not15 = icmp eq i32 %13, 0
  br i1 %.not15, label %16, label %14

14:                                               ; preds = %11
  %15 = tail call i64 @xTimerGenericCommand(ptr noundef %0, i64 noundef 4, i32 noundef %13, ptr noundef null, i32 noundef 0) #4
  %.pre = load i32, ptr %7, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %14, %11
  %17 = phi i32 [ %.pre, %14 ], [ %8, %11 ]
  %18 = icmp eq i32 %17, 2
  br i1 %18, label %19, label %30

19:                                               ; preds = %16
  %20 = getelementptr inbounds i8, ptr %3, i64 104
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  %22 = icmp eq ptr %21, null
  %23 = getelementptr inbounds i8, ptr %3, i64 96
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = getelementptr inbounds i8, ptr %3, i64 88
  %26 = load ptr, ptr %25, align 8
  br i1 %22, label %27, label %28

27:                                               ; preds = %19
  tail call void %24(ptr %26) #4
  br label %30

28:                                               ; preds = %19
  %29 = call i32 @pthread_create(ptr noundef nonnull %2, ptr noundef nonnull %21, ptr noundef %24, ptr noundef %26) #4
  br label %30

30:                                               ; preds = %27, %28, %16
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4
  ret void
}

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

declare ptr @pvTimerGetTimerID(ptr noundef) local_unnamed_addr #2

; Function Attrs: alwaysinline nounwind uwtable
declare hidden void @vPortRaiseBASEPRI() local_unnamed_addr #3

declare i64 @xTimerGenericCommand(ptr noundef, i64 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @pthread_create(ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

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

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" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { alwaysinline 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 = { 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 80}
!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}
!14 = !{!6, !12, i64 112}
!15 = !{!6, !8, i64 104}
!16 = !{!6, !8, i64 96}


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