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

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

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

16:                                               ; preds = %7
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %74

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

18:                                               ; preds = %42, %17
  %19 = load i32, ptr %6, align 4, !tbaa !9
  %20 = icmp slt i32 %19, 4
  br i1 %20, label %22, label %21

21:                                               ; preds = %18
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %45

22:                                               ; preds = %18
  %23 = load ptr, ptr @blk, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.blk, ptr %23, i32 0, i32 0
  %25 = load ptr, ptr %24, align 8, !tbaa !20
  %26 = load ptr, ptr @blk, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.blk, ptr %26, i32 0, i32 8
  %28 = load i32, ptr %27, align 8, !tbaa !22
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %25, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !19
  %32 = zext i8 %31 to i32
  %33 = load i32, ptr %6, align 4, !tbaa !9
  %34 = mul nsw i32 8, %33
  %35 = shl i32 %32, %34
  %36 = load i32, ptr %3, align 4, !tbaa !9
  %37 = or i32 %36, %35
  store i32 %37, ptr %3, align 4, !tbaa !9
  %38 = load ptr, ptr @blk, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.blk, ptr %38, i32 0, i32 8
  %40 = load i32, ptr %39, align 8, !tbaa !22
  %41 = add i32 %40, 1
  store i32 %41, ptr %39, align 8, !tbaa !22
  br label %42

42:                                               ; preds = %22
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %6, align 4, !tbaa !9
  br label %18, !llvm.loop !23

45:                                               ; preds = %21
  %46 = load i32, ptr %3, align 4, !tbaa !9
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.vcpu, ptr %47, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !11
  %50 = load ptr, ptr %2, 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 = getelementptr inbounds %struct.kvm_run, ptr %52, i32 0, i32 9
  %54 = getelementptr inbounds %struct.anon.2, ptr %53, i32 0, i32 4
  %55 = load i64, ptr %54, align 8, !tbaa !19
  %56 = getelementptr inbounds i8, ptr %49, i64 %55
  store i32 %46, ptr %56, align 4, !tbaa !9
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.vcpu, ptr %57, i32 0, i32 1
  %59 = load ptr, ptr %58, align 8, !tbaa !11
  %60 = getelementptr inbounds %struct.kvm_run, ptr %59, i32 0, i32 9
  %61 = getelementptr inbounds %struct.anon.2, ptr %60, i32 0, i32 1
  %62 = load i8, ptr %61, align 1, !tbaa !19
  %63 = zext i8 %62 to i64
  %64 = load ptr, ptr %2, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.vcpu, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !11
  %67 = getelementptr inbounds %struct.kvm_run, ptr %66, i32 0, i32 9
  %68 = getelementptr inbounds %struct.anon.2, ptr %67, i32 0, i32 4
  %69 = load i64, ptr %68, align 8, !tbaa !19
  %70 = add i64 %69, %63
  store i64 %70, ptr %68, align 8, !tbaa !19
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %71

71:                                               ; preds = %45
  %72 = load i32, ptr %4, align 4, !tbaa !9
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %4, align 4, !tbaa !9
  br label %7, !llvm.loop !25

74:                                               ; preds = %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  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"}
!25 = distinct !{!25, !24}


; 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_diskr(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 %.preheader.lr.ph

.preheader.lr.ph:                                 ; preds = %1
  %6 = load ptr, ptr @blk, align 8, !tbaa !18
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %.preheader
  %8 = phi ptr [ %3, %.preheader.lr.ph ], [ %45, %.preheader ]
  %.01115 = phi i32 [ 0, %.preheader.lr.ph ], [ %44, %.preheader ]
  %9 = load ptr, ptr %6, align 8, !tbaa !19
  %.promoted = load i32, ptr %7, align 8, !tbaa !21
  %10 = zext i32 %.promoted to i64
  %11 = getelementptr inbounds i8, ptr %9, i64 %10
  %12 = load i8, ptr %11, align 1, !tbaa !17
  %13 = zext i8 %12 to i32
  %14 = add i32 %.promoted, 1
  store i32 %14, ptr %7, align 8, !tbaa !21
  %15 = zext i32 %14 to i64
  %16 = getelementptr inbounds i8, ptr %9, i64 %15
  %17 = load i8, ptr %16, align 1, !tbaa !17
  %18 = zext i8 %17 to i32
  %19 = shl nuw nsw i32 %18, 8
  %20 = or disjoint i32 %19, %13
  %21 = add i32 %.promoted, 2
  store i32 %21, ptr %7, align 8, !tbaa !21
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %9, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !17
  %25 = zext i8 %24 to i32
  %26 = shl nuw nsw i32 %25, 16
  %27 = or disjoint i32 %26, %20
  %28 = add i32 %.promoted, 3
  store i32 %28, ptr %7, align 8, !tbaa !21
  %29 = zext i32 %28 to i64
  %30 = getelementptr inbounds i8, ptr %9, i64 %29
  %31 = load i8, ptr %30, align 1, !tbaa !17
  %32 = zext i8 %31 to i32
  %33 = shl nuw i32 %32, 24
  %34 = or disjoint i32 %33, %27
  %35 = add i32 %.promoted, 4
  store i32 %35, ptr %7, align 8, !tbaa !21
  %36 = getelementptr inbounds i8, ptr %8, i64 40
  %37 = load i64, ptr %36, align 8, !tbaa !17
  %38 = getelementptr inbounds i8, ptr %8, i64 %37
  store i32 %34, ptr %38, align 4, !tbaa !22
  %39 = getelementptr inbounds i8, ptr %8, i64 33
  %40 = load i8, ptr %39, align 1, !tbaa !17
  %41 = zext i8 %40 to i64
  %42 = load i64, ptr %36, align 8, !tbaa !17
  %43 = add i64 %42, %41
  store i64 %43, ptr %36, align 8, !tbaa !17
  %44 = add nuw nsw i32 %.01115, 1
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %45, i64 36
  %47 = load i32, ptr %46, align 4, !tbaa !17
  %48 = icmp ult i32 %44, %47
  br i1 %48, label %.preheader, label %._crit_edge, !llvm.loop !23

._crit_edge:                                      ; preds = %.preheader, %1
  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, !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, !10, i64 0}
!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 = !{!20, !7, i64 16}
!22 = !{!7, !7, i64 0}
!23 = distinct !{!23, !24}
!24 = !{!"llvm.loop.mustprogress"}


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