; LLVM IR from asllop/AppOS :: serial (serial)

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

@serial_timeout_t_ms = external hidden global i64, align 8

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 void @core_sleep(i64 noundef) #0

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

declare i32 @core_unlock(i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i64 @serial_receive(i8 noundef zeroext %0, ptr noundef %1, i64 noundef %2) #2 {
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store i8 %0, ptr %4, align 1, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !8
  store i64 %2, ptr %6, align 8, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %9 = call i64 (...) @core_timestamp()
  store i64 %9, ptr %8, align 8, !tbaa !12
  %10 = call i32 @core_lock(i32 noundef 3)
  store i64 0, ptr %7, align 8, !tbaa !10
  br label %11

11:                                               ; preds = %42, %3
  %12 = load i64, ptr %7, align 8, !tbaa !10
  %13 = load i64, ptr %6, align 8, !tbaa !10
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %45

15:                                               ; preds = %11
  br label %16

16:                                               ; preds = %28, %15
  %17 = load i8, ptr %4, align 1, !tbaa !5
  %18 = call zeroext i8 @is_data_ready(i8 noundef zeroext %17)
  %19 = zext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %29

21:                                               ; preds = %16
  call void @core_sleep(i64 noundef 0)
  %22 = call i64 (...) @core_timestamp()
  %23 = load i64, ptr %8, align 8, !tbaa !12
  %24 = sub i64 %22, %23
  %25 = load i64, ptr @serial_timeout_t_ms, align 8, !tbaa !10
  %26 = icmp uge i64 %24, %25
  br i1 %26, label %27, label %28

27:                                               ; preds = %21
  br label %29

28:                                               ; preds = %21
  br label %16, !llvm.loop !14

29:                                               ; preds = %27, %16
  %30 = load i8, ptr %4, align 1, !tbaa !5
  %31 = call zeroext i8 @is_data_ready(i8 noundef zeroext %30)
  %32 = icmp ne i8 %31, 0
  br i1 %32, label %33, label %40

33:                                               ; preds = %29
  %34 = load i8, ptr %4, align 1, !tbaa !5
  %35 = call zeroext i8 @serial_read_byte(i8 noundef zeroext %34)
  %36 = load ptr, ptr %5, align 8, !tbaa !8
  %37 = load i64, ptr %7, align 8, !tbaa !10
  %38 = getelementptr inbounds i8, ptr %36, i64 %37
  store i8 %35, ptr %38, align 1, !tbaa !5
  %39 = call i64 (...) @core_timestamp()
  store i64 %39, ptr %8, align 8, !tbaa !12
  br label %41

40:                                               ; preds = %29
  br label %45

41:                                               ; preds = %33
  br label %42

42:                                               ; preds = %41
  %43 = load i64, ptr %7, align 8, !tbaa !10
  %44 = add i64 %43, 1
  store i64 %44, ptr %7, align 8, !tbaa !10
  br label %11, !llvm.loop !16

45:                                               ; preds = %40, %11
  %46 = call i32 @core_unlock(i32 noundef 3)
  %47 = load i64, ptr %7, align 8, !tbaa !10
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  ret i64 %47
}

declare i64 @core_timestamp(...) #0

declare zeroext i8 @is_data_ready(i8 noundef zeroext) #0

declare zeroext i8 @serial_read_byte(i8 noundef zeroext) #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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !6, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long long", !6, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@serial_timeout_t_ms = external hidden local_unnamed_addr global i64, align 8

declare i32 @core_lock(i32 noundef) local_unnamed_addr #0

declare void @core_sleep(i64 noundef) local_unnamed_addr #0

declare i32 @core_unlock(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local i64 @serial_receive(i8 noundef zeroext %0, ptr nocapture noundef writeonly %1, i64 noundef %2) local_unnamed_addr #1 {
  %4 = tail call i64 (...) @core_timestamp() #2
  %5 = tail call i32 @core_lock(i32 noundef 3) #2
  %.not15 = icmp eq i64 %2, 0
  br i1 %.not15, label %._crit_edge, label %.preheader

.preheader:                                       ; preds = %3, %15
  %.012 = phi i64 [ %18, %15 ], [ %4, %3 ]
  %.0911 = phi i64 [ %19, %15 ], [ 0, %3 ]
  br label %6

6:                                                ; preds = %.preheader, %9
  %7 = tail call zeroext i8 @is_data_ready(i8 noundef zeroext %0) #2
  %8 = icmp eq i8 %7, 0
  br i1 %8, label %9, label %13

9:                                                ; preds = %6
  tail call void @core_sleep(i64 noundef 0) #2
  %10 = tail call i64 (...) @core_timestamp() #2
  %11 = sub i64 %10, %.012
  %12 = load i64, ptr @serial_timeout_t_ms, align 8, !tbaa !5
  %.not = icmp ult i64 %11, %12
  br i1 %.not, label %6, label %13, !llvm.loop !9

13:                                               ; preds = %9, %6
  %14 = tail call zeroext i8 @is_data_ready(i8 noundef zeroext %0) #2
  %.not10 = icmp eq i8 %14, 0
  br i1 %.not10, label %._crit_edge, label %15

15:                                               ; preds = %13
  %16 = tail call zeroext i8 @serial_read_byte(i8 noundef zeroext %0) #2
  %17 = getelementptr inbounds i8, ptr %1, i64 %.0911
  store i8 %16, ptr %17, align 1, !tbaa !11
  %18 = tail call i64 (...) @core_timestamp() #2
  %19 = add nuw i64 %.0911, 1
  %exitcond.not = icmp eq i64 %19, %2
  br i1 %exitcond.not, label %._crit_edge, label %.preheader, !llvm.loop !12

._crit_edge:                                      ; preds = %15, %13, %3
  %.09.lcssa = phi i64 [ 0, %3 ], [ %.0911, %13 ], [ %2, %15 ]
  %20 = tail call i32 @core_unlock(i32 noundef 3) #2
  ret i64 %.09.lcssa
}

declare i64 @core_timestamp(...) local_unnamed_addr #0

declare zeroext i8 @is_data_ready(i8 noundef zeroext) local_unnamed_addr #0

declare zeroext i8 @serial_read_byte(i8 noundef zeroext) 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, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !10}


; Effective O3 passes: GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass