; LLVM IR from Stephane-D/SGDK :: src (vdp_bg)

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

@hscrl_addr = external global i16, align 2
@hscroll = external hidden global [2 x i16], align 2
@hscroll_update = external hidden global i8, align 1
@vscroll = external hidden global [2 x i16], align 2
@vscroll_update = external hidden global i8, align 1

; 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 zeroext i8 @VDP_doVBlankScrollProcess() #1 {
  %1 = alloca ptr, align 8
  %2 = alloca ptr, align 8
  %3 = alloca i16, align 2
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %3) #2
  store ptr inttoptr (i64 12582912 to ptr), ptr %1, align 8, !tbaa !5
  store ptr inttoptr (i64 12582916 to ptr), ptr %2, align 8, !tbaa !5
  %4 = load i16, ptr @hscrl_addr, align 2, !tbaa !9
  store i16 %4, ptr %3, align 2, !tbaa !9
  %5 = load i8, ptr @hscroll_update, align 1, !tbaa !11
  %6 = zext i8 %5 to i32
  %7 = and i32 %6, 1
  %8 = icmp ne i32 %7, 0
  br i1 %8, label %9, label %23

9:                                                ; preds = %0
  %10 = load i16, ptr %3, align 2, !tbaa !9
  %11 = zext i16 %10 to i64
  %12 = and i64 %11, 16383
  %13 = add i64 16384, %12
  %14 = shl i64 %13, 16
  %15 = load i16, ptr %3, align 2, !tbaa !9
  %16 = zext i16 %15 to i64
  %17 = lshr i64 %16, 14
  %18 = or i64 %17, 0
  %19 = add i64 %14, %18
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  store volatile i64 %19, ptr %20, align 8, !tbaa !12
  %21 = load i16, ptr @hscroll, align 2, !tbaa !9
  %22 = load ptr, ptr %1, align 8, !tbaa !5
  store volatile i16 %21, ptr %22, align 2, !tbaa !9
  br label %23

23:                                               ; preds = %9, %0
  %24 = load i16, ptr %3, align 2, !tbaa !9
  %25 = zext i16 %24 to i32
  %26 = add nsw i32 %25, 2
  %27 = trunc i32 %26 to i16
  store i16 %27, ptr %3, align 2, !tbaa !9
  %28 = load i8, ptr @hscroll_update, align 1, !tbaa !11
  %29 = zext i8 %28 to i32
  %30 = and i32 %29, 2
  %31 = icmp ne i32 %30, 0
  br i1 %31, label %32, label %46

32:                                               ; preds = %23
  %33 = load i16, ptr %3, align 2, !tbaa !9
  %34 = zext i16 %33 to i64
  %35 = and i64 %34, 16383
  %36 = add i64 16384, %35
  %37 = shl i64 %36, 16
  %38 = load i16, ptr %3, align 2, !tbaa !9
  %39 = zext i16 %38 to i64
  %40 = lshr i64 %39, 14
  %41 = or i64 %40, 0
  %42 = add i64 %37, %41
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  store volatile i64 %42, ptr %43, align 8, !tbaa !12
  %44 = load i16, ptr getelementptr inbounds ([2 x i16], ptr @hscroll, i64 0, i64 1), align 2, !tbaa !9
  %45 = load ptr, ptr %1, align 8, !tbaa !5
  store volatile i16 %44, ptr %45, align 2, !tbaa !9
  br label %46

46:                                               ; preds = %32, %23
  store i16 0, ptr %3, align 2, !tbaa !9
  %47 = load i8, ptr @vscroll_update, align 1, !tbaa !11
  %48 = zext i8 %47 to i32
  %49 = and i32 %48, 1
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %61

51:                                               ; preds = %46
  %52 = load i16, ptr %3, align 2, !tbaa !9
  %53 = zext i16 %52 to i64
  %54 = and i64 %53, 127
  %55 = add i64 16384, %54
  %56 = shl i64 %55, 16
  %57 = add i64 %56, 16
  %58 = load ptr, ptr %2, align 8, !tbaa !5
  store volatile i64 %57, ptr %58, align 8, !tbaa !12
  %59 = load i16, ptr @vscroll, align 2, !tbaa !9
  %60 = load ptr, ptr %1, align 8, !tbaa !5
  store volatile i16 %59, ptr %60, align 2, !tbaa !9
  br label %61

61:                                               ; preds = %51, %46
  %62 = load i16, ptr %3, align 2, !tbaa !9
  %63 = zext i16 %62 to i32
  %64 = add nsw i32 %63, 2
  %65 = trunc i32 %64 to i16
  store i16 %65, ptr %3, align 2, !tbaa !9
  %66 = load i8, ptr @vscroll_update, align 1, !tbaa !11
  %67 = zext i8 %66 to i32
  %68 = and i32 %67, 2
  %69 = icmp ne i32 %68, 0
  br i1 %69, label %70, label %80

70:                                               ; preds = %61
  %71 = load i16, ptr %3, align 2, !tbaa !9
  %72 = zext i16 %71 to i64
  %73 = and i64 %72, 127
  %74 = add i64 16384, %73
  %75 = shl i64 %74, 16
  %76 = add i64 %75, 16
  %77 = load ptr, ptr %2, align 8, !tbaa !5
  store volatile i64 %76, ptr %77, align 8, !tbaa !12
  %78 = load i16, ptr getelementptr inbounds ([2 x i16], ptr @vscroll, i64 0, i64 1), align 2, !tbaa !9
  %79 = load ptr, ptr %1, align 8, !tbaa !5
  store volatile i16 %78, ptr %79, align 2, !tbaa !9
  br label %80

80:                                               ; preds = %70, %61
  store i8 0, ptr @hscroll_update, align 1, !tbaa !11
  store i8 0, ptr @vscroll_update, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 2, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %2) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #2
  ret i8 0
}

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 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"long", !7, i64 0}


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

@hscrl_addr = external local_unnamed_addr global i16, align 2
@hscroll = external hidden local_unnamed_addr global [2 x i16], align 2
@hscroll_update = external hidden local_unnamed_addr global i8, align 1
@vscroll = external hidden local_unnamed_addr global [2 x i16], align 2
@vscroll_update = external hidden local_unnamed_addr global i8, align 1

; Function Attrs: nofree norecurse nounwind uwtable
define dso_local noundef zeroext i8 @VDP_doVBlankScrollProcess() local_unnamed_addr #0 {
  %1 = load i16, ptr @hscrl_addr, align 2, !tbaa !5
  %2 = load i8, ptr @hscroll_update, align 1, !tbaa !9
  %3 = and i8 %2, 1
  %.not = icmp eq i8 %3, 0
  br i1 %.not, label %12, label %4

4:                                                ; preds = %0
  %5 = zext i16 %1 to i64
  %6 = shl nuw nsw i64 %5, 16
  %7 = and i64 %6, 1073676288
  %8 = lshr i64 %5, 14
  %9 = or disjoint i64 %8, %7
  %10 = or disjoint i64 %9, 1073741824
  store volatile i64 %10, ptr inttoptr (i64 12582916 to ptr), align 8, !tbaa !10
  %11 = load i16, ptr @hscroll, align 2, !tbaa !5
  store volatile i16 %11, ptr inttoptr (i64 12582912 to ptr), align 4194304, !tbaa !5
  %.pre = load i8, ptr @hscroll_update, align 1, !tbaa !9
  br label %12

12:                                               ; preds = %4, %0
  %13 = phi i8 [ %.pre, %4 ], [ %2, %0 ]
  %14 = and i8 %13, 2
  %.not15 = icmp eq i8 %14, 0
  br i1 %.not15, label %24, label %15

15:                                               ; preds = %12
  %16 = add i16 %1, 2
  %17 = zext i16 %16 to i64
  %18 = shl nuw nsw i64 %17, 16
  %19 = and i64 %18, 1073676288
  %20 = lshr i64 %17, 14
  %21 = or disjoint i64 %20, %19
  %22 = or disjoint i64 %21, 1073741824
  store volatile i64 %22, ptr inttoptr (i64 12582916 to ptr), align 8, !tbaa !10
  %23 = load i16, ptr getelementptr inbounds (i8, ptr @hscroll, i64 2), align 2, !tbaa !5
  store volatile i16 %23, ptr inttoptr (i64 12582912 to ptr), align 4194304, !tbaa !5
  br label %24

24:                                               ; preds = %15, %12
  %25 = load i8, ptr @vscroll_update, align 1, !tbaa !9
  %26 = and i8 %25, 1
  %.not16 = icmp eq i8 %26, 0
  br i1 %.not16, label %29, label %27

27:                                               ; preds = %24
  store volatile i64 1073741840, ptr inttoptr (i64 12582916 to ptr), align 8, !tbaa !10
  %28 = load i16, ptr @vscroll, align 2, !tbaa !5
  store volatile i16 %28, ptr inttoptr (i64 12582912 to ptr), align 4194304, !tbaa !5
  %.pre18 = load i8, ptr @vscroll_update, align 1, !tbaa !9
  br label %29

29:                                               ; preds = %27, %24
  %30 = phi i8 [ %.pre18, %27 ], [ %25, %24 ]
  %31 = and i8 %30, 2
  %.not17 = icmp eq i8 %31, 0
  br i1 %.not17, label %34, label %32

32:                                               ; preds = %29
  store volatile i64 1073872912, ptr inttoptr (i64 12582916 to ptr), align 8, !tbaa !10
  %33 = load i16, ptr getelementptr inbounds (i8, ptr @vscroll, i64 2), align 2, !tbaa !5
  store volatile i16 %33, ptr inttoptr (i64 12582912 to ptr), align 4194304, !tbaa !5
  br label %34

34:                                               ; preds = %32, %29
  store i8 0, ptr @hscroll_update, align 1, !tbaa !9
  store i8 0, ptr @vscroll_update, align 1, !tbaa !9
  ret i8 0
}

attributes #0 = { nofree norecurse 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" }

!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 = !{!"short", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"long", !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass