; LLVM IR from asllop/AppOS :: term/drivers/term_serial (term_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"

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

declare i64 @serial_send(i8 noundef zeroext, ptr noundef, i64 noundef) #1

declare i64 @ansi_strlen(ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local void @term_serial_where(i32 noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca [10 x i8], align 1
  %8 = alloca i32, align 4
  %9 = alloca i8, align 1
  %10 = alloca i32, align 4
  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 10, ptr %7) #3
  %11 = getelementptr inbounds [10 x i8], ptr %7, i64 0, i64 0
  %12 = call ptr @ansi_report_cursor(ptr noundef %11)
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = trunc i32 %13 to i8
  %15 = getelementptr inbounds [10 x i8], ptr %7, i64 0, i64 0
  %16 = getelementptr inbounds [10 x i8], ptr %7, i64 0, i64 0
  %17 = call i64 @ansi_strlen(ptr noundef %16)
  %18 = call i64 @serial_send(i8 noundef zeroext %14, ptr noundef %15, i64 noundef %17)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %44, %3
  br label %20

20:                                               ; preds = %19
  %21 = load i32, ptr %4, align 4, !tbaa !5
  %22 = trunc i32 %21 to i8
  %23 = call zeroext i1 @serial_avail(i8 noundef zeroext %22)
  br i1 %23, label %24, label %44

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #3
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = trunc i32 %25 to i8
  %27 = call i64 @serial_receive(i8 noundef zeroext %26, ptr noundef %9, i64 noundef 1)
  %28 = load i8, ptr %9, align 1, !tbaa !11
  %29 = load i32, ptr %8, align 4, !tbaa !5
  %30 = add nsw i32 %29, 1
  store i32 %30, ptr %8, align 4, !tbaa !5
  %31 = sext i32 %29 to i64
  %32 = getelementptr inbounds [10 x i8], ptr %7, i64 0, i64 %31
  store i8 %28, ptr %32, align 1, !tbaa !11
  %33 = load i8, ptr %9, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = icmp eq i32 %34, 82
  br i1 %35, label %36, label %40

36:                                               ; preds = %24
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [10 x i8], ptr %7, i64 0, i64 %38
  store i8 0, ptr %39, align 1, !tbaa !11
  store i32 3, ptr %10, align 4
  br label %41

40:                                               ; preds = %24
  store i32 0, ptr %10, align 4
  br label %41

41:                                               ; preds = %40, %36
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #3
  %42 = load i32, ptr %10, align 4
  switch i32 %42, label %56 [
    i32 0, label %43
    i32 3, label %45
  ]

43:                                               ; preds = %41
  br label %44

44:                                               ; preds = %43, %20
  br label %19

45:                                               ; preds = %41
  %46 = getelementptr inbounds [10 x i8], ptr %7, i64 0, i64 0
  %47 = load ptr, ptr %6, align 8, !tbaa !9
  %48 = load ptr, ptr %5, align 8, !tbaa !9
  %49 = call zeroext i1 @ansi_parse_report(ptr noundef %46, ptr noundef %47, ptr noundef %48)
  %50 = load ptr, ptr %6, align 8, !tbaa !9
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = add nsw i32 %51, -1
  store i32 %52, ptr %50, align 4, !tbaa !5
  %53 = load ptr, ptr %5, align 8, !tbaa !9
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = add nsw i32 %54, -1
  store i32 %55, ptr %53, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 10, ptr %7) #3
  ret void

56:                                               ; preds = %41
  unreachable
}

declare ptr @ansi_report_cursor(ptr noundef) #1

declare zeroext i1 @serial_avail(i8 noundef zeroext) #1

declare i64 @serial_receive(i8 noundef zeroext, ptr noundef, i64 noundef) #1

declare zeroext i1 @ansi_parse_report(ptr noundef, ptr noundef, 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 = !{!"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 = !{!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: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

declare i64 @serial_send(i8 noundef zeroext, ptr noundef, i64 noundef) local_unnamed_addr #1

declare i64 @ansi_strlen(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
define dso_local void @term_serial_where(i32 noundef %0, ptr noundef %1, ptr noundef %2) local_unnamed_addr #2 {
  %4 = alloca [10 x i8], align 1
  %5 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 10, ptr nonnull %4) #3
  %6 = call ptr @ansi_report_cursor(ptr noundef nonnull %4) #3
  %7 = trunc i32 %0 to i8
  %8 = call i64 @ansi_strlen(ptr noundef nonnull %4) #3
  %9 = call i64 @serial_send(i8 noundef zeroext %7, ptr noundef nonnull %4, i64 noundef %8) #3
  br label %.outer

.outer:                                           ; preds = %.thread, %3
  %.09.ph = phi i32 [ %15, %.thread ], [ 0, %3 ]
  br label %10

10:                                               ; preds = %.outer, %10
  %11 = call zeroext i1 @serial_avail(i8 noundef zeroext %7) #3
  br i1 %11, label %12, label %10

12:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %5) #3
  %13 = call i64 @serial_receive(i8 noundef zeroext %7, ptr noundef nonnull %5, i64 noundef 1) #3
  %14 = load i8, ptr %5, align 1, !tbaa !5
  %15 = add nuw nsw i32 %.09.ph, 1
  %16 = zext nneg i32 %.09.ph to i64
  %17 = getelementptr inbounds [10 x i8], ptr %4, i64 0, i64 %16
  store i8 %14, ptr %17, align 1, !tbaa !5
  %18 = icmp eq i8 %14, 82
  br i1 %18, label %19, label %.thread

.thread:                                          ; preds = %12
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %5) #3
  br label %.outer

19:                                               ; preds = %12
  %20 = zext nneg i32 %15 to i64
  %21 = getelementptr inbounds [10 x i8], ptr %4, i64 0, i64 %20
  store i8 0, ptr %21, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %5) #3
  %22 = call zeroext i1 @ansi_parse_report(ptr noundef nonnull %4, ptr noundef %2, ptr noundef %1) #3
  %23 = load i32, ptr %2, align 4, !tbaa !8
  %24 = add nsw i32 %23, -1
  store i32 %24, ptr %2, align 4, !tbaa !8
  %25 = load i32, ptr %1, align 4, !tbaa !8
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %1, align 4, !tbaa !8
  call void @llvm.lifetime.end.p0(i64 10, ptr nonnull %4) #3
  ret void
}

declare ptr @ansi_report_cursor(ptr noundef) local_unnamed_addr #1

declare zeroext i1 @serial_avail(i8 noundef zeroext) local_unnamed_addr #1

declare i64 @serial_receive(i8 noundef zeroext, ptr noundef, i64 noundef) local_unnamed_addr #1

declare zeroext i1 @ansi_parse_report(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

attributes #0 = { mustprogress 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopUnrollPass,SROAPass,SimplifyCFGPass