; 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.sigevent = type { i32, i32, %union.sigval, ptr, ptr }
%union.sigval = type { ptr }
%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] }

@FreeRTOS_errno = external global i32, align 4
@.str = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @prvTimerCallback(ptr noundef) #0

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

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

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

12:                                               ; preds = %3
  %13 = load ptr, ptr %5, align 8, !tbaa !9
  %14 = getelementptr inbounds %struct.sigevent, ptr %13, i32 0, i32 0
  %15 = load i32, ptr %14, align 8, !tbaa !11
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %18

17:                                               ; preds = %12, %3
  store i32 95, ptr @FreeRTOS_errno, align 4, !tbaa !5
  store i32 -1, ptr %7, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %17, %12
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %27

21:                                               ; preds = %18
  %22 = call ptr @pvPortMalloc(i64 noundef 120)
  store ptr %22, ptr %8, align 8, !tbaa !9
  %23 = load ptr, ptr %8, align 8, !tbaa !9
  %24 = icmp eq ptr %23, null
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  store i32 11, ptr @FreeRTOS_errno, align 4, !tbaa !5
  store i32 -1, ptr %7, align 4, !tbaa !5
  br label %26

26:                                               ; preds = %25, %21
  br label %27

27:                                               ; preds = %26, %18
  %28 = load i32, ptr %7, align 4, !tbaa !5
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %41

30:                                               ; preds = %27
  %31 = load ptr, ptr %8, align 8, !tbaa !9
  %32 = getelementptr inbounds %struct.timer_internal, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %32, ptr align 8 %33, i64 32, i1 false), !tbaa.struct !13
  %34 = load ptr, ptr %8, align 8, !tbaa !9
  %35 = getelementptr inbounds %struct.timer_internal, ptr %34, i32 0, i32 2
  store i32 0, ptr %35, align 8, !tbaa !15
  %36 = load ptr, ptr %8, align 8, !tbaa !9
  %37 = load ptr, ptr %8, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.timer_internal, ptr %37, i32 0, i32 0
  %39 = call ptr @xTimerCreateStatic(ptr noundef @.str, i32 noundef -1, i64 noundef 0, ptr noundef %36, ptr noundef @prvTimerCallback, ptr noundef %38)
  %40 = load ptr, ptr %6, align 8, !tbaa !9
  store ptr %39, ptr %40, align 8, !tbaa !9
  br label %41

41:                                               ; preds = %30, %27
  %42 = load i32, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  ret i32 %42
}

declare ptr @pvPortMalloc(i64 noundef) #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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

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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"sigevent", !6, i64 0, !6, i64 4, !7, i64 8, !10, i64 16, !10, i64 24}
!13 = !{i64 0, i64 4, !5, i64 4, i64 4, !5, i64 8, i64 8, !14, i64 16, i64 8, !9, i64 24, i64 8, !9}
!14 = !{!7, !7, i64 0}
!15 = !{!16, !6, i64 112}
!16 = !{!"timer_internal", !17, i64 0, !12, i64 80, !6, i64 112}
!17 = !{!"xSTATIC_TIMER", !10, i64 0, !18, i64 8, !6, i64 48, !10, i64 56, !10, i64 64, !7, i64 72}
!18 = !{!"xSTATIC_LIST_ITEM", !6, i64 0, !7, 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"

@FreeRTOS_errno = external local_unnamed_addr global i32, align 4
@.str = external hidden unnamed_addr constant [6 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @prvTimerCallback(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @timer_create(i32 noundef %0, ptr noundef readonly %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %1, null
  br i1 %4, label %.critedge, label %5

5:                                                ; preds = %3
  %6 = load i32, ptr %1, align 8, !tbaa !5
  %7 = icmp eq i32 %6, 1
  br i1 %7, label %.critedge, label %8

.critedge:                                        ; preds = %5, %3
  store i32 95, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %.thread

8:                                                ; preds = %5
  %9 = tail call ptr @pvPortMalloc(i64 noundef 120) #3
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %12

11:                                               ; preds = %8
  store i32 11, ptr @FreeRTOS_errno, align 4, !tbaa !11
  br label %.thread

12:                                               ; preds = %8
  %13 = getelementptr inbounds i8, ptr %9, i64 80
  tail call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(32) %13, ptr noundef nonnull align 8 dereferenceable(32) %1, i64 32, i1 false), !tbaa.struct !12
  %14 = getelementptr inbounds i8, ptr %9, i64 112
  store i32 0, ptr %14, align 8, !tbaa !15
  %15 = tail call ptr @xTimerCreateStatic(ptr noundef nonnull @.str, i32 noundef -1, i64 noundef 0, ptr noundef nonnull %9, ptr noundef nonnull @prvTimerCallback, ptr noundef nonnull %9) #3
  store ptr %15, ptr %2, align 8, !tbaa !14
  br label %.thread

.thread:                                          ; preds = %.critedge, %11, %12
  %.118 = phi i32 [ 0, %12 ], [ -1, %11 ], [ -1, %.critedge ]
  ret i32 %.118
}

declare ptr @pvPortMalloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

declare ptr @xTimerCreateStatic(ptr noundef, i32 noundef, i64 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #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 = { "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 nounwind willreturn memory(argmem: readwrite) }
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, !7, i64 0}
!6 = !{!"sigevent", !7, i64 0, !7, i64 4, !8, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{i64 0, i64 4, !11, i64 4, i64 4, !11, i64 8, i64 8, !13, i64 16, i64 8, !14, i64 24, i64 8, !14}
!13 = !{!8, !8, i64 0}
!14 = !{!10, !10, i64 0}
!15 = !{!16, !7, i64 112}
!16 = !{!"timer_internal", !17, i64 0, !6, i64 80, !7, i64 112}
!17 = !{!"xSTATIC_TIMER", !10, i64 0, !18, i64 8, !7, i64 48, !10, i64 56, !10, i64 64, !8, i64 72}
!18 = !{!"xSTATIC_LIST_ITEM", !7, i64 0, !8, i64 8}


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