; LLVM IR from tomoyasuzuki/kvmm :: uart (clear_uart_data_reg)

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

@uart = external dso_local global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @clear_uart_data_reg() #0 {
  %1 = alloca i32, align 4
  %2 = load ptr, ptr @uart, align 8, !tbaa !5
  %3 = getelementptr inbounds %struct.uart, ptr %2, i32 0, i32 0
  store i8 0, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #2
  store i32 0, ptr %1, align 4, !tbaa !12
  br label %4

4:                                                ; preds = %26, %0
  %5 = load i32, ptr %1, align 4, !tbaa !12
  %6 = load ptr, ptr @uart, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.uart, ptr %6, i32 0, i32 9
  %8 = load i32, ptr %7, align 4, !tbaa !13
  %9 = icmp slt i32 %5, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %4
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #2
  br label %29

11:                                               ; preds = %4
  %12 = load ptr, ptr @uart, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.uart, ptr %12, i32 0, i32 10
  %14 = load ptr, ptr %13, align 8, !tbaa !14
  %15 = load i32, ptr %1, align 4, !tbaa !12
  %16 = add nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds i8, ptr %14, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !15
  %20 = load ptr, ptr @uart, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.uart, ptr %20, i32 0, i32 10
  %22 = load ptr, ptr %21, align 8, !tbaa !14
  %23 = load i32, ptr %1, align 4, !tbaa !12
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %22, i64 %24
  store i8 %19, ptr %25, align 1, !tbaa !15
  br label %26

26:                                               ; preds = %11
  %27 = load i32, ptr %1, align 4, !tbaa !12
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %1, align 4, !tbaa !12
  br label %4, !llvm.loop !16

29:                                               ; preds = %10
  %30 = load ptr, ptr @uart, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.uart, ptr %30, i32 0, i32 9
  %32 = load i32, ptr %31, align 4, !tbaa !13
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %34, label %39

34:                                               ; preds = %29
  %35 = load ptr, ptr @uart, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.uart, ptr %35, i32 0, i32 9
  %37 = load i32, ptr %36, align 4, !tbaa !13
  %38 = add nsw i32 %37, -1
  store i32 %38, ptr %36, align 4, !tbaa !13
  br label %39

39:                                               ; preds = %34, %29
  ret void
}

; 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

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 = { 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, !7, i64 0}
!10 = !{!"uart", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !11, i64 8, !11, i64 12, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!11, !11, i64 0}
!13 = !{!10, !11, i64 12}
!14 = !{!10, !6, i64 16}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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

@uart = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @clear_uart_data_reg() local_unnamed_addr #0 {
  %1 = load ptr, ptr @uart, align 8, !tbaa !5
  store i8 0, ptr %1, align 8, !tbaa !9
  %2 = getelementptr inbounds i8, ptr %1, i64 12
  %3 = load i32, ptr %2, align 4, !tbaa !12
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph, label %._crit_edge.thread

._crit_edge:                                      ; preds = %.lr.ph
  %5 = icmp sgt i32 %14, 0
  br i1 %5, label %17, label %._crit_edge.thread

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %6 = phi ptr [ %12, %.lr.ph ], [ %1, %0 ]
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %8 = load ptr, ptr %7, align 8, !tbaa !13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %9 = getelementptr inbounds i8, ptr %8, i64 %indvars.iv.next
  %10 = load i8, ptr %9, align 1, !tbaa !14
  %11 = getelementptr inbounds i8, ptr %8, i64 %indvars.iv
  store i8 %10, ptr %11, align 1, !tbaa !14
  %12 = load ptr, ptr @uart, align 8, !tbaa !5
  %13 = getelementptr inbounds i8, ptr %12, i64 12
  %14 = load i32, ptr %13, align 4, !tbaa !12
  %15 = sext i32 %14 to i64
  %16 = icmp slt i64 %indvars.iv.next, %15
  br i1 %16, label %.lr.ph, label %._crit_edge, !llvm.loop !15

17:                                               ; preds = %._crit_edge
  %18 = getelementptr inbounds i8, ptr %12, i64 12
  %19 = add nsw i32 %14, -1
  store i32 %19, ptr %18, align 4, !tbaa !12
  br label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %0, %17, %._crit_edge
  ret void
}

attributes #0 = { nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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" }

!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, !7, i64 0}
!10 = !{!"uart", !7, i64 0, !7, i64 1, !7, i64 2, !7, i64 3, !7, i64 4, !7, i64 5, !7, i64 6, !7, i64 7, !11, i64 8, !11, i64 12, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !11, i64 12}
!13 = !{!10, !6, i64 16}
!14 = !{!7, !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass