; LLVM IR from aap/librw :: src/d3d/d3d.ll (ZN2rw3d3d10rasterLockEPNS_6RasterEii)

; 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.rw::Raster" = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, ptr, ptr, ptr, i32, i32, i32, ptr, i32, i32 }
%"struct.rw::d3d::D3dRaster" = type { ptr, ptr, ptr, i32, i32, i8, i8, i8 }
%"struct.rw::RasterLevels" = type { i32, i32, [1 x %"struct.rw::RasterLevels::Level"] }
%"struct.rw::RasterLevels::Level" = type { i32, i32, i32, ptr }

@_ZN2rw3d3d18nativeRasterOffsetE = external dso_local 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: mustprogress nounwind uwtable
define dso_local noundef ptr @_ZN2rw3d3d10rasterLockEPNS_6RasterEii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %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
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %11 = load ptr, ptr %5, align 8, !tbaa !5
  %12 = load i32, ptr @_ZN2rw3d3d18nativeRasterOffsetE, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %11, i64 %13
  store ptr %14, ptr %8, align 8, !tbaa !5
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %"struct.rw::Raster", ptr %15, i32 0, i32 3
  %17 = load i32, ptr %16, align 4, !tbaa !11
  %18 = and i32 %17, 6
  %19 = icmp ne i32 %18, 0
  br i1 %19, label %20, label %21

20:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %82

21:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %22 = load ptr, ptr %8, align 8, !tbaa !5
  %23 = getelementptr inbounds %"struct.rw::d3d::D3dRaster", ptr %22, i32 0, i32 0
  %24 = load ptr, ptr %23, align 8, !tbaa !13
  store ptr %24, ptr %10, align 8, !tbaa !5
  %25 = load ptr, ptr %10, align 8, !tbaa !5
  %26 = getelementptr inbounds %"struct.rw::RasterLevels", ptr %25, i32 0, i32 2
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [1 x %"struct.rw::RasterLevels::Level"], ptr %26, i64 0, i64 %28
  %30 = getelementptr inbounds %"struct.rw::RasterLevels::Level", ptr %29, i32 0, i32 3
  %31 = load ptr, ptr %30, align 8, !tbaa !16
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = getelementptr inbounds %"struct.rw::Raster", ptr %32, i32 0, i32 9
  store ptr %31, ptr %33, align 8, !tbaa !18
  %34 = load ptr, ptr %10, align 8, !tbaa !5
  %35 = getelementptr inbounds %"struct.rw::RasterLevels", ptr %34, i32 0, i32 2
  %36 = load i32, ptr %6, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [1 x %"struct.rw::RasterLevels::Level"], ptr %35, i64 0, i64 %37
  %39 = getelementptr inbounds %"struct.rw::RasterLevels::Level", ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !19
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  %42 = getelementptr inbounds %"struct.rw::Raster", ptr %41, i32 0, i32 5
  store i32 %40, ptr %42, align 4, !tbaa !20
  %43 = load ptr, ptr %10, align 8, !tbaa !5
  %44 = getelementptr inbounds %"struct.rw::RasterLevels", ptr %43, i32 0, i32 2
  %45 = load i32, ptr %6, align 4, !tbaa !9
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1 x %"struct.rw::RasterLevels::Level"], ptr %44, i64 0, i64 %46
  %48 = getelementptr inbounds %"struct.rw::RasterLevels::Level", ptr %47, i32 0, i32 1
  %49 = load i32, ptr %48, align 4, !tbaa !21
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = getelementptr inbounds %"struct.rw::Raster", ptr %50, i32 0, i32 6
  store i32 %49, ptr %51, align 8, !tbaa !22
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %"struct.rw::Raster", ptr %52, i32 0, i32 5
  %54 = load i32, ptr %53, align 4, !tbaa !20
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  %56 = getelementptr inbounds %"struct.rw::d3d::D3dRaster", ptr %55, i32 0, i32 4
  %57 = load i32, ptr %56, align 4, !tbaa !23
  %58 = mul i32 %54, %57
  %59 = load ptr, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds %"struct.rw::Raster", ptr %59, i32 0, i32 8
  store i32 %58, ptr %60, align 8, !tbaa !24
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = and i32 %61, 2
  %63 = icmp ne i32 %62, 0
  br i1 %63, label %64, label %69

64:                                               ; preds = %21
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %"struct.rw::Raster", ptr %65, i32 0, i32 3
  %67 = load i32, ptr %66, align 4, !tbaa !11
  %68 = or i32 %67, 2
  store i32 %68, ptr %66, align 4, !tbaa !11
  br label %69

69:                                               ; preds = %64, %21
  %70 = load i32, ptr %7, align 4, !tbaa !9
  %71 = and i32 %70, 1
  %72 = icmp ne i32 %71, 0
  br i1 %72, label %73, label %78

73:                                               ; preds = %69
  %74 = load ptr, ptr %5, align 8, !tbaa !5
  %75 = getelementptr inbounds %"struct.rw::Raster", ptr %74, i32 0, i32 3
  %76 = load i32, ptr %75, align 4, !tbaa !11
  %77 = or i32 %76, 4
  store i32 %77, ptr %75, align 4, !tbaa !11
  br label %78

78:                                               ; preds = %73, %69
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  %80 = getelementptr inbounds %"struct.rw::Raster", ptr %79, i32 0, i32 9
  %81 = load ptr, ptr %80, align 8, !tbaa !18
  store ptr %81, ptr %4, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  br label %82

82:                                               ; preds = %78, %20
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  %83 = load ptr, ptr %4, align 8
  ret ptr %83
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 12}
!12 = !{!"_ZTSN2rw6RasterE", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !10, i64 16, !10, i64 20, !10, i64 24, !10, i64 28, !10, i64 32, !6, i64 40, !6, i64 48, !6, i64 56, !10, i64 64, !10, i64 68, !10, i64 72, !6, i64 80, !10, i64 88, !10, i64 92}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTSN2rw3d3d9D3dRasterE", !6, i64 0, !6, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !15, i64 32, !15, i64 33, !15, i64 34}
!15 = !{!"bool", !7, i64 0}
!16 = !{!17, !6, i64 16}
!17 = !{!"_ZTSN2rw12RasterLevels5LevelE", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 16}
!18 = !{!12, !6, i64 40}
!19 = !{!17, !10, i64 0}
!20 = !{!12, !10, i64 20}
!21 = !{!17, !10, i64 4}
!22 = !{!12, !10, i64 24}
!23 = !{!14, !10, i64 28}
!24 = !{!12, !10, i64 32}


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

%"struct.rw::RasterLevels::Level" = type { i32, i32, i32, ptr }

@_ZN2rw3d3d18nativeRasterOffsetE = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @_ZN2rw3d3d10rasterLockEPNS_6RasterEii(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = getelementptr inbounds i8, ptr %0, i64 12
  %5 = load i32, ptr %4, align 4, !tbaa !5
  %6 = and i32 %5, 6
  %.not = icmp eq i32 %6, 0
  br i1 %.not, label %condstore.split, label %28

condstore.split:                                  ; preds = %3
  %7 = load i32, ptr @_ZN2rw3d3d18nativeRasterOffsetE, align 4, !tbaa !11
  %8 = sext i32 %7 to i64
  %9 = getelementptr inbounds i8, ptr %0, i64 %8
  %10 = load ptr, ptr %9, align 8, !tbaa !12
  %11 = getelementptr inbounds i8, ptr %10, i64 8
  %12 = sext i32 %1 to i64
  %13 = getelementptr inbounds [1 x %"struct.rw::RasterLevels::Level"], ptr %11, i64 0, i64 %12
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %16 = getelementptr inbounds i8, ptr %0, i64 40
  store ptr %15, ptr %16, align 8, !tbaa !17
  %17 = getelementptr inbounds i8, ptr %0, i64 20
  %18 = load <2 x i32>, ptr %13, align 8, !tbaa !11
  store <2 x i32> %18, ptr %17, align 4, !tbaa !11
  %19 = getelementptr inbounds i8, ptr %9, i64 28
  %20 = load i32, ptr %19, align 4, !tbaa !18
  %21 = extractelement <2 x i32> %18, i64 0
  %22 = mul i32 %20, %21
  %23 = getelementptr inbounds i8, ptr %0, i64 32
  store i32 %22, ptr %23, align 8, !tbaa !19
  %24 = and i32 %2, 3
  %.not22 = icmp eq i32 %24, 0
  br i1 %.not22, label %28, label %25

25:                                               ; preds = %condstore.split
  %trunc = trunc i32 %2 to i3
  %26 = and i3 %trunc, 3
  %mask = tail call i3 @llvm.bitreverse.i3(i3 %26)
  %27 = zext i3 %mask to i32
  %simplifycfg.merge = or disjoint i32 %5, %27
  store i32 %simplifycfg.merge, ptr %4, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %25, %condstore.split, %3
  %.0 = phi ptr [ null, %3 ], [ %15, %condstore.split ], [ %15, %25 ]
  ret ptr %.0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i3 @llvm.bitreverse.i3(i3) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: 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" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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, !7, i64 12}
!6 = !{!"_ZTSN2rw6RasterE", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !10, i64 40, !10, i64 48, !10, i64 56, !7, i64 64, !7, i64 68, !7, i64 72, !10, i64 80, !7, i64 88, !7, i64 92}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"_ZTSN2rw3d3d9D3dRasterE", !10, i64 0, !10, i64 8, !10, i64 16, !7, i64 24, !7, i64 28, !14, i64 32, !14, i64 33, !14, i64 34}
!14 = !{!"bool", !8, i64 0}
!15 = !{!16, !10, i64 16}
!16 = !{!"_ZTSN2rw12RasterLevels5LevelE", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16}
!17 = !{!6, !10, i64 40}
!18 = !{!13, !7, i64 28}
!19 = !{!6, !7, i64 32}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass