; LLVM IR from tomoyasuzuki/kvmm :: blk (emulate_diskw)

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

@blk = external dso_local global ptr, align 8

; 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_diskw(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %5

5:                                                ; preds = %62, %1
  %6 = load i32, ptr %3, align 4, !tbaa !9
  %7 = load ptr, ptr %2, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.vcpu, ptr %7, i32 0, i32 1
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = getelementptr inbounds %struct.kvm_run, ptr %9, i32 0, i32 9
  %11 = getelementptr inbounds %struct.anon.2, ptr %10, i32 0, i32 3
  %12 = load i32, ptr %11, align 4, !tbaa !19
  %13 = icmp ult i32 %6, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %65

15:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = getelementptr inbounds %struct.vcpu, ptr %16, i32 0, i32 1
  %18 = load ptr, ptr %17, align 8, !tbaa !11
  %19 = load ptr, ptr %2, 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 = getelementptr inbounds %struct.kvm_run, ptr %21, i32 0, i32 9
  %23 = getelementptr inbounds %struct.anon.2, ptr %22, i32 0, i32 4
  %24 = load i64, ptr %23, align 8, !tbaa !19
  %25 = getelementptr inbounds i32, ptr %18, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  store i32 %26, ptr %4, align 4, !tbaa !9
  %27 = load i32, ptr %4, align 4, !tbaa !9
  %28 = trunc i32 %27 to i8
  %29 = load ptr, ptr @blk, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.blk, ptr %29, i32 0, i32 0
  %31 = load ptr, ptr %30, align 8, !tbaa !20
  %32 = load ptr, ptr @blk, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.blk, ptr %32, i32 0, i32 8
  %34 = load i32, ptr %33, align 8, !tbaa !22
  %35 = zext i32 %34 to i64
  %36 = getelementptr inbounds i8, ptr %31, i64 %35
  store i8 %28, ptr %36, align 1, !tbaa !19
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.vcpu, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !11
  %40 = getelementptr inbounds %struct.kvm_run, ptr %39, i32 0, i32 9
  %41 = getelementptr inbounds %struct.anon.2, ptr %40, i32 0, i32 1
  %42 = load i8, ptr %41, align 1, !tbaa !19
  %43 = zext i8 %42 to i64
  %44 = load ptr, ptr %2, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.vcpu, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !11
  %47 = getelementptr inbounds %struct.kvm_run, ptr %46, i32 0, i32 9
  %48 = getelementptr inbounds %struct.anon.2, ptr %47, i32 0, i32 4
  %49 = load i64, ptr %48, align 8, !tbaa !19
  %50 = add i64 %49, %43
  store i64 %50, ptr %48, align 8, !tbaa !19
  %51 = load ptr, ptr %2, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.vcpu, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !11
  %54 = getelementptr inbounds %struct.kvm_run, ptr %53, i32 0, i32 9
  %55 = getelementptr inbounds %struct.anon.2, ptr %54, i32 0, i32 1
  %56 = load i8, ptr %55, align 1, !tbaa !19
  %57 = zext i8 %56 to i32
  %58 = load ptr, ptr @blk, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.blk, ptr %58, i32 0, i32 8
  %60 = load i32, ptr %59, align 8, !tbaa !22
  %61 = add i32 %60, %57
  store i32 %61, ptr %59, align 8, !tbaa !22
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %62

62:                                               ; preds = %15
  %63 = load i32, ptr %3, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %3, align 4, !tbaa !9
  br label %5, !llvm.loop !23

65:                                               ; preds = %14
  ret void
}

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 = { 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 = !{!21, !6, i64 0}
!21 = !{!"blk", !6, i64 0, !7, i64 8, !7, i64 9, !7, i64 10, !7, i64 11, !7, i64 12, !7, i64 13, !7, i64 14, !10, i64 16, !7, i64 20}
!22 = !{!21, !10, i64 16}
!23 = distinct !{!23, !24}
!24 = !{!"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"

@blk = 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 @emulate_diskw(ptr nocapture noundef readonly %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 36
  %5 = load i32, ptr %4, align 4, !tbaa !17
  %.not = icmp eq i32 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %.pre = load ptr, ptr @blk, align 8, !tbaa !18
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.pre, i64 16
  %.pre9 = load i32, ptr %.phi.trans.insert, align 8, !tbaa !19
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph, %1
  ret void

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %6 = phi i32 [ %31, %.lr.ph ], [ %.pre9, %.lr.ph.preheader ]
  %7 = phi ptr [ %28, %.lr.ph ], [ %.pre, %.lr.ph.preheader ]
  %8 = phi ptr [ %24, %.lr.ph ], [ %3, %.lr.ph.preheader ]
  %.08 = phi i32 [ %32, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %9 = getelementptr inbounds i8, ptr %8, i64 40
  %10 = load i64, ptr %9, align 8, !tbaa !17
  %11 = getelementptr inbounds i32, ptr %8, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !21
  %13 = trunc i32 %12 to i8
  %14 = load ptr, ptr %7, align 8, !tbaa !22
  %15 = zext i32 %6 to i64
  %16 = getelementptr inbounds i8, ptr %14, i64 %15
  store i8 %13, ptr %16, align 1, !tbaa !17
  %17 = load ptr, ptr %2, align 8, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %17, i64 33
  %19 = load i8, ptr %18, align 1, !tbaa !17
  %20 = zext i8 %19 to i64
  %21 = getelementptr inbounds i8, ptr %17, i64 40
  %22 = load i64, ptr %21, align 8, !tbaa !17
  %23 = add i64 %22, %20
  store i64 %23, ptr %21, align 8, !tbaa !17
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = getelementptr inbounds i8, ptr %24, i64 33
  %26 = load i8, ptr %25, align 1, !tbaa !17
  %27 = zext i8 %26 to i32
  %28 = load ptr, ptr @blk, align 8, !tbaa !18
  %29 = getelementptr inbounds i8, ptr %28, i64 16
  %30 = load i32, ptr %29, align 8, !tbaa !19
  %31 = add i32 %30, %27
  store i32 %31, ptr %29, align 8, !tbaa !19
  %32 = add nuw nsw i32 %.08, 1
  %33 = getelementptr inbounds i8, ptr %24, i64 36
  %34 = load i32, ptr %33, align 4, !tbaa !17
  %35 = icmp ult i32 %32, %34
  br i1 %35, label %.lr.ph, label %._crit_edge, !llvm.loop !23
}

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, !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 = !{!10, !10, i64 0}
!19 = !{!20, !7, i64 16}
!20 = !{!"blk", !10, i64 0, !8, i64 8, !8, i64 9, !8, i64 10, !8, i64 11, !8, i64 12, !8, i64 13, !8, i64 14, !7, i64 16, !8, i64 20}
!21 = !{!7, !7, i64 0}
!22 = !{!20, !10, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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