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

; 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.vcpu = type { i32, ptr, %struct.kvm_sregs, %struct.kvm_regs }
%struct.kvm_sregs = type { %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_segment, %struct.kvm_dtable, %struct.kvm_dtable, i64, i64, i64, i64, i64, i64, i64, [4 x i64] }
%struct.kvm_segment = type { i64, i32, i16, i8, i8, i8, i8, i8, i8, i8, i8, i8, i8 }
%struct.kvm_dtable = type { i64, i16, [3 x i16] }
%struct.kvm_regs = type { i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64 }
%struct.kvm_run = type { i8, i8, [6 x i8], i32, i8, i8, i16, i64, i64, %union.anon, i64, i64, %union.anon.27 }
%union.anon = type { %struct.anon.12 }
%struct.anon.12 = type { [32 x i64] }
%union.anon.27 = type { %struct.kvm_sync_regs, [1528 x i8] }
%struct.kvm_sync_regs = type { %struct.kvm_regs, %struct.kvm_sregs, %struct.kvm_vcpu_events }
%struct.kvm_vcpu_events = type { %struct.anon.28, %struct.anon.29, %struct.anon.30, i32, i32, %struct.anon.31, [27 x i8], i8, i64 }
%struct.anon.28 = type { i8, i8, i8, i8, i32 }
%struct.anon.29 = type { i8, i8, i8, i8 }
%struct.anon.30 = type { i8, i8, i8, i8 }
%struct.anon.31 = type { i8, i8, i8, i8 }
%struct.anon.2 = type { i8, i8, i16, i32, i64 }
%struct.uart = type { i8, i8, i8, i8, i8, i8, i8, i8, i32, i32, ptr }

@uart = external dso_local global ptr, align 8
@outfd = external global i32, align 4

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

; 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 @emulate_uart_portw(ptr noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) #1 {
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  store i32 %3, ptr %8, align 4, !tbaa !9
  %11 = load i32, ptr %6, align 4, !tbaa !9
  switch i32 %11, label %63 [
    i32 1016, label %12
    i32 1017, label %49
  ]

12:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %13

13:                                               ; preds = %45, %12
  %14 = load i32, ptr %9, align 4, !tbaa !9
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %48

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.vcpu, ptr %19, i32 0, i32 1
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.vcpu, ptr %22, i32 0, i32 1
  %24 = load ptr, ptr %23, align 8, !tbaa !11
  %25 = getelementptr inbounds %struct.kvm_run, ptr %24, i32 0, i32 9
  %26 = getelementptr inbounds %struct.anon.2, ptr %25, i32 0, i32 4
  %27 = load i64, ptr %26, align 8, !tbaa !19
  %28 = getelementptr inbounds i8, ptr %21, i64 %27
  store ptr %28, ptr %10, align 8, !tbaa !5
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  %30 = load i8, ptr %29, align 1, !tbaa !19
  %31 = sext i8 %30 to i32
  %32 = call i32 @putchar(i32 noundef %31)
  %33 = load i32, ptr @outfd, align 4, !tbaa !9
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = call i64 @write(i32 noundef %33, ptr noundef %34, i64 noundef 1)
  %36 = load i32, ptr %8, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.vcpu, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !11
  %41 = getelementptr inbounds %struct.kvm_run, ptr %40, i32 0, i32 9
  %42 = getelementptr inbounds %struct.anon.2, ptr %41, i32 0, i32 4
  %43 = load i64, ptr %42, align 8, !tbaa !19
  %44 = add i64 %43, %37
  store i64 %44, ptr %42, align 8, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %45

45:                                               ; preds = %18
  %46 = load i32, ptr %9, align 4, !tbaa !9
  %47 = add nsw i32 %46, 1
  store i32 %47, ptr %9, align 4, !tbaa !9
  br label %13, !llvm.loop !20

48:                                               ; preds = %17
  br label %64

49:                                               ; preds = %4
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.vcpu, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !11
  %53 = load ptr, ptr %5, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.vcpu, ptr %53, i32 0, i32 1
  %55 = load ptr, ptr %54, align 8, !tbaa !11
  %56 = getelementptr inbounds %struct.kvm_run, ptr %55, i32 0, i32 9
  %57 = getelementptr inbounds %struct.anon.2, ptr %56, i32 0, i32 4
  %58 = load i64, ptr %57, align 8, !tbaa !19
  %59 = getelementptr inbounds i8, ptr %52, i64 %58
  %60 = load i8, ptr %59, align 1, !tbaa !19
  %61 = load ptr, ptr @uart, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.uart, ptr %61, i32 0, i32 1
  store i8 %60, ptr %62, align 1, !tbaa !22
  br label %64

63:                                               ; preds = %4
  br label %64

64:                                               ; preds = %63, %49, %48
  ret void
}

; Function Attrs: inlinehint nounwind uwtable
declare i32 @putchar(i32 noundef) #2

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { inlinehint 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 = { "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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"vcpu", !10, i64 0, !6, i64 8, !13, i64 16, !18, i64 328}
!13 = !{!"kvm_sregs", !14, i64 0, !14, i64 24, !14, i64 48, !14, i64 72, !14, i64 96, !14, i64 120, !14, i64 144, !14, i64 168, !17, i64 192, !17, i64 208, !15, i64 224, !15, i64 232, !15, i64 240, !15, i64 248, !15, i64 256, !15, i64 264, !15, i64 272, !7, i64 280}
!14 = !{!"kvm_segment", !15, i64 0, !10, i64 8, !16, i64 12, !7, i64 14, !7, i64 15, !7, i64 16, !7, i64 17, !7, i64 18, !7, i64 19, !7, i64 20, !7, i64 21, !7, i64 22, !7, i64 23}
!15 = !{!"long long", !7, i64 0}
!16 = !{!"short", !7, i64 0}
!17 = !{!"kvm_dtable", !15, i64 0, !16, i64 8, !7, i64 10}
!18 = !{!"kvm_regs", !15, i64 0, !15, i64 8, !15, i64 16, !15, i64 24, !15, i64 32, !15, i64 40, !15, i64 48, !15, i64 56, !15, i64 64, !15, i64 72, !15, i64 80, !15, i64 88, !15, i64 96, !15, i64 104, !15, i64 112, !15, i64 120, !15, i64 128, !15, i64 136}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = !{!23, !7, i64 1}
!23 = !{!"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, !10, i64 8, !10, i64 12, !6, i64 16}


; 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
@outfd = external local_unnamed_addr global i32, align 4

; Function Attrs: nofree nounwind uwtable
define dso_local void @emulate_uart_portw(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  switch i32 %1, label %.loopexit [
    i32 1016, label %.preheader
    i32 1017, label %23
  ]

.preheader:                                       ; preds = %4
  %5 = icmp sgt i32 %2, 0
  br i1 %5, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %.preheader
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = sext i32 %3 to i64
  br label %8

8:                                                ; preds = %.lr.ph, %8
  %.011 = phi i32 [ 0, %.lr.ph ], [ %22, %8 ]
  %9 = load ptr, ptr %6, align 8, !tbaa !5
  %10 = getelementptr inbounds i8, ptr %9, i64 40
  %11 = load i64, ptr %10, align 8, !tbaa !17
  %12 = getelementptr inbounds i8, ptr %9, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !17
  %14 = sext i8 %13 to i32
  %15 = tail call i32 @putchar(i32 noundef %14)
  %16 = load i32, ptr @outfd, align 4, !tbaa !18
  %17 = tail call i64 @write(i32 noundef %16, ptr noundef nonnull %12, i64 noundef 1) #3
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds i8, ptr %18, i64 40
  %20 = load i64, ptr %19, align 8, !tbaa !17
  %21 = add i64 %20, %7
  store i64 %21, ptr %19, align 8, !tbaa !17
  %22 = add nuw nsw i32 %.011, 1
  %exitcond.not = icmp eq i32 %22, %2
  br i1 %exitcond.not, label %.loopexit, label %8, !llvm.loop !19

23:                                               ; preds = %4
  %24 = getelementptr inbounds i8, ptr %0, i64 8
  %25 = load ptr, ptr %24, align 8, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %25, i64 40
  %27 = load i64, ptr %26, align 8, !tbaa !17
  %28 = getelementptr inbounds i8, ptr %25, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !17
  %30 = load ptr, ptr @uart, align 8, !tbaa !21
  %31 = getelementptr inbounds i8, ptr %30, i64 1
  store i8 %29, ptr %31, align 1, !tbaa !22
  br label %.loopexit

.loopexit:                                        ; preds = %8, %.preheader, %4, %23
  ret void
}

; Function Attrs: inlinehint nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #1

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

attributes #0 = { nofree 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 = { inlinehint nofree 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 = { nofree "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, !10, i64 8}
!6 = !{!"vcpu", !7, i64 0, !10, i64 8, !11, i64 16, !16, i64 328}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"kvm_sregs", !12, i64 0, !12, i64 24, !12, i64 48, !12, i64 72, !12, i64 96, !12, i64 120, !12, i64 144, !12, i64 168, !15, i64 192, !15, i64 208, !13, i64 224, !13, i64 232, !13, i64 240, !13, i64 248, !13, i64 256, !13, i64 264, !13, i64 272, !8, i64 280}
!12 = !{!"kvm_segment", !13, i64 0, !7, i64 8, !14, i64 12, !8, i64 14, !8, i64 15, !8, i64 16, !8, i64 17, !8, i64 18, !8, i64 19, !8, i64 20, !8, i64 21, !8, i64 22, !8, i64 23}
!13 = !{!"long long", !8, i64 0}
!14 = !{!"short", !8, i64 0}
!15 = !{!"kvm_dtable", !13, i64 0, !14, i64 8, !8, i64 10}
!16 = !{!"kvm_regs", !13, i64 0, !13, i64 8, !13, i64 16, !13, i64 24, !13, i64 32, !13, i64 40, !13, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !13, i64 80, !13, i64 88, !13, i64 96, !13, i64 104, !13, i64 112, !13, i64 120, !13, i64 128, !13, i64 136}
!17 = !{!8, !8, i64 0}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!10, !10, i64 0}
!22 = !{!23, !8, i64 1}
!23 = !{!"uart", !8, i64 0, !8, i64 1, !8, i64 2, !8, i64 3, !8, i64 4, !8, i64 5, !8, i64 6, !8, i64 7, !7, i64 8, !7, i64 12, !10, i64 16}


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