; LLVM IR from tomoyasuzuki/kvmm :: io (emulate_io)

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

@.str = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @emulate_io_out(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @emulate_io_in(ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @emulate_io(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  store ptr %0, ptr %2, align 8, !tbaa !5
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds %struct.vcpu, ptr %3, i32 0, i32 1
  %5 = load ptr, ptr %4, align 8, !tbaa !9
  %6 = getelementptr inbounds %struct.kvm_run, ptr %5, i32 0, i32 9
  %7 = getelementptr inbounds %struct.anon.2, ptr %6, i32 0, i32 0
  %8 = load i8, ptr %7, align 8, !tbaa !18
  %9 = zext i8 %8 to i32
  switch i32 %9, label %14 [
    i32 1, label %10
    i32 0, label %12
  ]

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  call void @emulate_io_out(ptr noundef %11)
  br label %22

12:                                               ; preds = %1
  %13 = load ptr, ptr %2, align 8, !tbaa !5
  call void @emulate_io_in(ptr noundef %13)
  br label %22

14:                                               ; preds = %1
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.vcpu, ptr %15, i32 0, i32 1
  %17 = load ptr, ptr %16, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.kvm_run, ptr %17, i32 0, i32 3
  %19 = load i32, ptr %18, align 8, !tbaa !19
  %20 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %19)
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  call void (ptr, ...) @print_regs(ptr noundef %21)
  br label %22

22:                                               ; preds = %14, %12, %10
  ret void
}

declare i32 @printf(ptr noundef, ...) #1

declare void @print_regs(...) #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 = { "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, !6, i64 8}
!10 = !{!"vcpu", !11, i64 0, !6, i64 8, !12, i64 16, !17, i64 328}
!11 = !{!"int", !7, i64 0}
!12 = !{!"kvm_sregs", !13, i64 0, !13, i64 24, !13, i64 48, !13, i64 72, !13, i64 96, !13, i64 120, !13, i64 144, !13, i64 168, !16, i64 192, !16, i64 208, !14, i64 224, !14, i64 232, !14, i64 240, !14, i64 248, !14, i64 256, !14, i64 264, !14, i64 272, !7, i64 280}
!13 = !{!"kvm_segment", !14, i64 0, !11, i64 8, !15, 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}
!14 = !{!"long long", !7, i64 0}
!15 = !{!"short", !7, i64 0}
!16 = !{!"kvm_dtable", !14, i64 0, !15, i64 8, !7, i64 10}
!17 = !{!"kvm_regs", !14, i64 0, !14, i64 8, !14, i64 16, !14, i64 24, !14, i64 32, !14, i64 40, !14, i64 48, !14, i64 56, !14, i64 64, !14, i64 72, !14, i64 80, !14, i64 88, !14, i64 96, !14, i64 104, !14, i64 112, !14, i64 120, !14, i64 128, !14, i64 136}
!18 = !{!7, !7, i64 0}
!19 = !{!20, !11, i64 8}
!20 = !{!"kvm_run", !7, i64 0, !7, i64 1, !7, i64 2, !11, i64 8, !7, i64 12, !7, i64 13, !15, i64 14, !14, i64 16, !14, i64 24, !7, i64 32, !14, i64 288, !14, i64 296, !7, i64 304}


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

@.str = external hidden unnamed_addr constant [17 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @emulate_io_out(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @emulate_io_in(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @emulate_io(ptr noundef %0) local_unnamed_addr #0 {
  %2 = getelementptr inbounds i8, ptr %0, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 32
  %5 = load i8, ptr %4, align 8, !tbaa !17
  switch i8 %5, label %8 [
    i8 1, label %6
    i8 0, label %7
  ]

6:                                                ; preds = %1
  tail call void @emulate_io_out(ptr noundef nonnull %0)
  br label %12

7:                                                ; preds = %1
  tail call void @emulate_io_in(ptr noundef nonnull %0)
  br label %12

8:                                                ; preds = %1
  %9 = getelementptr inbounds i8, ptr %3, i64 8
  %10 = load i32, ptr %9, align 8, !tbaa !18
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %10)
  tail call void (ptr, ...) @print_regs(ptr noundef nonnull %0) #3
  br label %12

12:                                               ; preds = %8, %7, %6
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

declare void @print_regs(...) local_unnamed_addr #2

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 = { nofree nounwind "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 = { "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 = !{!19, !7, i64 8}
!19 = !{!"kvm_run", !8, i64 0, !8, i64 1, !8, i64 2, !7, i64 8, !8, i64 12, !8, i64 13, !14, i64 14, !13, i64 16, !13, i64 24, !8, i64 32, !13, i64 288, !13, i64 296, !8, i64 304}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,TailCallElimPass