; 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 @updateBG1(ptr noundef, i16 noundef zeroext, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @handleScroll(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 %100

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 %31

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, 1
  %26 = trunc i32 %25 to i16
  store i16 %26, ptr %22, align 4, !tbaa !12
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.character, ptr %27, i32 0, i32 1
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = sub nsw i32 %29, 1
  store i32 %30, ptr %28, align 4, !tbaa !9
  br label %31

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

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

67:                                               ; preds = %43
  %68 = load i16, ptr %5, align 2, !tbaa !19
  %69 = zext i16 %68 to i32
  %70 = add nsw i32 %69, 1024
  %71 = trunc i32 %70 to i16
  store i16 %71, ptr %5, align 2, !tbaa !19
  br label %72

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

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

100:                                              ; preds = %84, %2
  ret void
}

; 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

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 @updateBG1(ptr noundef, i16 noundef zeroext, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @handleScroll(ptr nocapture noundef %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 %43

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 %15

12:                                               ; preds = %6
  %13 = add nuw i16 %8, 1
  store i16 %13, ptr %7, align 4, !tbaa !10
  %14 = add nsw i32 %4, -1
  store i32 %14, ptr %3, align 4, !tbaa !5
  br label %15

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

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

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

43:                                               ; preds = %36, %2
  ret void
}

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

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: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass