; LLVM IR from asllop/AppOS :: core/task (task)

; 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.TaskStruct = type { i32, ptr, i32, i32, i32, i32, ptr, ptr, ptr, ptr, ptr, i64, i64 }

declare i32 @core_lock(i32 noundef) #0

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

declare i32 @core_unlock(i32 noundef) #0

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

declare ptr @get_task(i32 noundef) #0

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

14:                                               ; preds = %3
  br label %15

15:                                               ; preds = %26, %14
  %16 = load ptr, ptr %8, align 8, !tbaa !9
  %17 = getelementptr inbounds %struct.TaskStruct, ptr %16, i32 0, i32 4
  %18 = load i32, ptr %17, align 8, !tbaa !11
  %19 = icmp ne i32 %18, 2
  br i1 %19, label %20, label %27

20:                                               ; preds = %15
  %21 = load ptr, ptr %8, align 8, !tbaa !9
  %22 = getelementptr inbounds %struct.TaskStruct, ptr %21, i32 0, i32 4
  %23 = load i32, ptr %22, align 8, !tbaa !11
  %24 = icmp eq i32 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %20
  store i32 -510, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %40

26:                                               ; preds = %20
  call void @core_sleep(i64 noundef 0)
  br label %15, !llvm.loop !15

27:                                               ; preds = %15
  %28 = load ptr, ptr %8, align 8, !tbaa !9
  %29 = getelementptr inbounds %struct.TaskStruct, ptr %28, i32 0, i32 5
  %30 = load i32, ptr %29, align 4, !tbaa !17
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  store i32 %30, ptr %31, align 4, !tbaa !5
  %32 = load ptr, ptr %8, align 8, !tbaa !9
  %33 = getelementptr inbounds %struct.TaskStruct, ptr %32, i32 0, i32 6
  %34 = load ptr, ptr %33, align 8, !tbaa !18
  %35 = load ptr, ptr %7, align 8, !tbaa !9
  store ptr %34, ptr %35, align 8, !tbaa !9
  %36 = call i32 @core_lock(i32 noundef 1)
  %37 = load ptr, ptr %8, align 8, !tbaa !9
  call void @terminate_task(ptr noundef %37)
  %38 = call i32 @core_unlock(i32 noundef 1)
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %40

39:                                               ; preds = %3
  store i32 -510, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %40

40:                                               ; preds = %39, %27, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %41 = load i32, ptr %4, align 4
  ret i32 %41
}

; Function Attrs: nounwind uwtable
declare dso_local void @core_sleep(i64 noundef) #2

declare void @terminate_task(ptr noundef) #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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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 24}
!12 = !{!"TaskStruct", !6, i64 0, !10, i64 8, !6, i64 16, !6, i64 20, !6, i64 24, !6, i64 28, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !13, i64 72, !14, i64 80}
!13 = !{!"long", !7, i64 0}
!14 = !{!"long long", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = !{!12, !6, i64 28}
!18 = !{!12, !10, i64 32}


; 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 i32 @core_lock(i32 noundef) local_unnamed_addr #0

declare i32 @core_unlock(i32 noundef) local_unnamed_addr #0

declare ptr @get_task(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local range(i32 -510, 1) i32 @core_result(i32 noundef %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #1 {
  %4 = tail call ptr @get_task(i32 noundef %0) #2
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %3
  %5 = getelementptr inbounds i8, ptr %4, i64 24
  br label %6

6:                                                ; preds = %.preheader, %8
  %7 = load i32, ptr %5, align 8, !tbaa !5
  switch i32 %7, label %8 [
    i32 2, label %9
    i32 0, label %.loopexit
  ]

8:                                                ; preds = %6
  tail call void @core_sleep(i64 noundef 0)
  br label %6, !llvm.loop !13

9:                                                ; preds = %6
  %10 = getelementptr inbounds i8, ptr %4, i64 28
  %11 = load i32, ptr %10, align 4, !tbaa !15
  store i32 %11, ptr %1, align 4, !tbaa !16
  %12 = getelementptr inbounds i8, ptr %4, i64 32
  %13 = load ptr, ptr %12, align 8, !tbaa !17
  store ptr %13, ptr %2, align 8, !tbaa !18
  %14 = tail call i32 @core_lock(i32 noundef 1) #2
  tail call void @terminate_task(ptr noundef nonnull %4) #2
  %15 = tail call i32 @core_unlock(i32 noundef 1) #2
  br label %.loopexit

.loopexit:                                        ; preds = %6, %3, %9
  %.0 = phi i32 [ 0, %9 ], [ -510, %3 ], [ -510, %6 ]
  ret i32 %.0
}

; Function Attrs: nounwind uwtable
declare dso_local void @core_sleep(i64 noundef) local_unnamed_addr #1

declare void @terminate_task(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, !7, i64 24}
!6 = !{!"TaskStruct", !7, i64 0, !10, i64 8, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !10, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !10, i64 64, !11, i64 72, !12, i64 80}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"long", !8, i64 0}
!12 = !{!"long long", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!6, !7, i64 28}
!16 = !{!7, !7, i64 0}
!17 = !{!6, !10, i64 32}
!18 = !{!10, !10, i64 0}


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