; LLVM IR from alekmaul/pvsneslib :: snes-examples/graphics/Backgrounds/Mode1ContinuosScroll (Mode1ContinuosScroll)

; 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.character = type { i8, i32, i32 }
%struct.scroll = type { i8, i16, i16, i16, ptr, i8, i8, i16, i16 }

; Function Attrs: nounwind uwtable
declare dso_local void @updateBG2(ptr noundef, i16 noundef zeroext, i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

declare void @bgSetScroll(i8 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) #2

; Function Attrs: nounwind uwtable
define dso_local void @handleScrollSub(ptr noundef %0, ptr noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i16, align 2
  store ptr %0, ptr %3, align 8, !tbaa !5
  store ptr %1, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.character, ptr %6, i32 0, i32 1
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = icmp sgt i32 %8, 94
  br i1 %9, label %10, label %96

10:                                               ; preds = %2
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  %12 = getelementptr inbounds %struct.scroll, ptr %11, i32 0, i32 2
  %13 = load i16, ptr %12, align 4, !tbaa !12
  %14 = zext i16 %13 to i32
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.scroll, ptr %15, i32 0, i32 8
  %17 = load i16, ptr %16, align 4, !tbaa !15
  %18 = zext i16 %17 to i32
  %19 = icmp slt i32 %14, %18
  br i1 %19, label %20, label %27

20:                                               ; preds = %10
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.scroll, ptr %21, i32 0, i32 2
  %23 = load i16, ptr %22, align 4, !tbaa !12
  %24 = zext i16 %23 to i32
  %25 = add nsw i32 %24, 2
  %26 = trunc i32 %25 to i16
  store i16 %26, ptr %22, align 4, !tbaa !12
  br label %27

27:                                               ; preds = %20, %10
  %28 = load ptr, ptr %4, align 8, !tbaa !5
  %29 = getelementptr inbounds %struct.scroll, ptr %28, i32 0, i32 2
  %30 = load i16, ptr %29, align 4, !tbaa !12
  %31 = zext i16 %30 to i32
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.scroll, ptr %32, i32 0, i32 5
  %34 = load i8, ptr %33, align 8, !tbaa !16
  %35 = zext i8 %34 to i32
  %36 = add nsw i32 %35, 1
  %37 = mul nsw i32 %36, 256
  %38 = icmp sge i32 %31, %37
  br i1 %38, label %39, label %80

39:                                               ; preds = %27
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.scroll, ptr %40, i32 0, i32 5
  %42 = load i8, ptr %41, align 8, !tbaa !16
  %43 = zext i8 %42 to i32
  %44 = add nsw i32 %43, 1
  %45 = trunc i32 %44 to i8
  store i8 %45, ptr %41, align 8, !tbaa !16
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = getelementptr inbounds %struct.scroll, ptr %46, i32 0, i32 6
  %48 = load i8, ptr %47, align 1, !tbaa !17
  %49 = icmp ne i8 %48, 0
  %50 = xor i1 %49, true
  %51 = zext i1 %50 to i32
  %52 = trunc i32 %51 to i8
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.scroll, ptr %53, i32 0, i32 6
  store i8 %52, ptr %54, align 1, !tbaa !17
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #3
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.scroll, ptr %55, i32 0, i32 1
  %57 = load i16, ptr %56, align 2, !tbaa !18
  store i16 %57, ptr %5, align 2, !tbaa !19
  %58 = load ptr, ptr %4, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.scroll, ptr %58, i32 0, i32 6
  %60 = load i8, ptr %59, align 1, !tbaa !17
  %61 = zext i8 %60 to i32
  %62 = icmp eq i32 %61, 0
  br i1 %62, label %63, label %68

63:                                               ; preds = %39
  %64 = load i16, ptr %5, align 2, !tbaa !19
  %65 = zext i16 %64 to i32
  %66 = add nsw i32 %65, 1024
  %67 = trunc i32 %66 to i16
  store i16 %67, ptr %5, align 2, !tbaa !19
  br label %68

68:                                               ; preds = %63, %39
  %69 = load ptr, ptr %4, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.scroll, ptr %69, i32 0, i32 4
  %71 = load ptr, ptr %70, align 8, !tbaa !20
  %72 = load ptr, ptr %4, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.scroll, ptr %72, i32 0, i32 5
  %74 = load i8, ptr %73, align 8, !tbaa !16
  %75 = zext i8 %74 to i32
  %76 = mul nsw i32 2048, %75
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i8, ptr %71, i64 %77
  %79 = load i16, ptr %5, align 2, !tbaa !19
  call void @updateBG2(ptr noundef %78, i16 noundef zeroext %79, i32 noundef 2048)
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #3
  br label %80

80:                                               ; preds = %68, %27
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.scroll, ptr %81, i32 0, i32 0
  %83 = load i8, ptr %82, align 8, !tbaa !21
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.scroll, ptr %84, i32 0, i32 2
  %86 = load i16, ptr %85, align 4, !tbaa !12
  %87 = zext i16 %86 to i32
  %88 = sub nsw i32 %87, 255
  %89 = trunc i32 %88 to i16
  %90 = load ptr, ptr %4, align 8, !tbaa !5
  %91 = getelementptr inbounds %struct.scroll, ptr %90, i32 0, i32 3
  %92 = load i16, ptr %91, align 2, !tbaa !22
  %93 = zext i16 %92 to i32
  %94 = sub nsw i32 %93, 223
  %95 = trunc i32 %94 to i16
  call void @bgSetScroll(i8 noundef zeroext %83, i16 noundef zeroext %89, i16 noundef zeroext %95)
  br label %96

96:                                               ; preds = %80, %2
  ret void
}

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 4}
!10 = !{!"", !7, i64 0, !11, i64 4, !11, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !14, i64 4}
!13 = !{!"", !7, i64 0, !14, i64 2, !14, i64 4, !14, i64 6, !6, i64 8, !7, i64 16, !7, i64 17, !14, i64 18, !14, i64 20}
!14 = !{!"short", !7, i64 0}
!15 = !{!13, !14, i64 20}
!16 = !{!13, !7, i64 16}
!17 = !{!13, !7, i64 17}
!18 = !{!13, !14, i64 2}
!19 = !{!14, !14, i64 0}
!20 = !{!13, !6, i64 8}
!21 = !{!13, !7, i64 0}
!22 = !{!13, !14, i64 6}


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

; Function Attrs: nounwind uwtable
declare dso_local void @updateBG2(ptr noundef, i16 noundef zeroext, i32 noundef) local_unnamed_addr #0

declare void @bgSetScroll(i8 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @handleScrollSub(ptr nocapture noundef readonly %0, ptr nocapture noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %0, i64 4
  %4 = load i32, ptr %3, align 4, !tbaa !5
  %5 = icmp sgt i32 %4, 94
  br i1 %5, label %6, label %42

6:                                                ; preds = %2
  %7 = getelementptr inbounds i8, ptr %1, i64 4
  %8 = load i16, ptr %7, align 4, !tbaa !10
  %9 = getelementptr inbounds i8, ptr %1, i64 20
  %10 = load i16, ptr %9, align 4, !tbaa !14
  %11 = icmp ult i16 %8, %10
  br i1 %11, label %12, label %14

12:                                               ; preds = %6
  %13 = add i16 %8, 2
  store i16 %13, ptr %7, align 4, !tbaa !10
  br label %14

14:                                               ; preds = %12, %6
  %15 = phi i16 [ %13, %12 ], [ %8, %6 ]
  %16 = zext i16 %15 to i32
  %17 = getelementptr inbounds i8, ptr %1, i64 16
  %18 = load i8, ptr %17, align 8, !tbaa !15
  %19 = zext i8 %18 to i32
  %20 = add nuw nsw i32 %19, 1
  %21 = shl nuw nsw i32 %20, 8
  %.not = icmp ugt i32 %21, %16
  br i1 %.not, label %35, label %22

22:                                               ; preds = %14
  %23 = trunc i32 %20 to i8
  store i8 %23, ptr %17, align 8, !tbaa !15
  %24 = getelementptr inbounds i8, ptr %1, i64 17
  %25 = load i8, ptr %24, align 1, !tbaa !16
  %.not18 = icmp eq i8 %25, 0
  %26 = zext i1 %.not18 to i8
  store i8 %26, ptr %24, align 1, !tbaa !16
  %27 = getelementptr inbounds i8, ptr %1, i64 2
  %28 = load i16, ptr %27, align 2, !tbaa !17
  %29 = add i16 %28, 1024
  %spec.select = select i1 %.not18, i16 %28, i16 %29
  %30 = getelementptr inbounds i8, ptr %1, i64 8
  %31 = load ptr, ptr %30, align 8, !tbaa !18
  %.mask = shl nuw nsw i32 %20, 11
  %32 = and i32 %.mask, 522240
  %33 = zext nneg i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %31, i64 %33
  tail call void @updateBG2(ptr noundef %34, i16 noundef zeroext %spec.select, i32 noundef 2048)
  %.pre = load i16, ptr %7, align 4, !tbaa !10
  br label %35

35:                                               ; preds = %22, %14
  %36 = phi i16 [ %.pre, %22 ], [ %15, %14 ]
  %37 = load i8, ptr %1, align 8, !tbaa !19
  %38 = add i16 %36, -255
  %39 = getelementptr inbounds i8, ptr %1, i64 6
  %40 = load i16, ptr %39, align 2, !tbaa !20
  %41 = add i16 %40, -223
  tail call void @bgSetScroll(i8 noundef zeroext %37, i16 noundef zeroext %38, i16 noundef zeroext %41) #2
  br label %42

42:                                               ; preds = %35, %2
  ret void
}

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" }
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, !9, i64 4}
!6 = !{!"", !7, i64 0, !9, i64 4, !9, i64 8}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!"int", !7, i64 0}
!10 = !{!11, !12, i64 4}
!11 = !{!"", !7, i64 0, !12, i64 2, !12, i64 4, !12, i64 6, !13, i64 8, !7, i64 16, !7, i64 17, !12, i64 18, !12, i64 20}
!12 = !{!"short", !7, i64 0}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!11, !12, i64 20}
!15 = !{!11, !7, i64 16}
!16 = !{!11, !7, i64 17}
!17 = !{!11, !12, i64 2}
!18 = !{!11, !13, i64 8}
!19 = !{!11, !7, i64 0}
!20 = !{!11, !12, i64 6}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass