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

; 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.Vect2D_u16 = type { i16, i16 }

@bmp_buffer_write = 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 @BMP_setPixelsFast_V2D_old(ptr noundef %0, i8 noundef zeroext %1, i16 noundef zeroext %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca i8, align 1
  %6 = alloca i16, align 2
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i16, align 2
  %10 = alloca i8, align 1
  %11 = alloca i8, align 1
  %12 = alloca i8, align 1
  %13 = alloca i8, align 1
  %14 = alloca i16, align 2
  %15 = alloca i16, align 2
  %16 = alloca i16, align 2
  %17 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i8 %1, ptr %5, align 1, !tbaa !9
  store i16 %2, ptr %6, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  store i8 -16, ptr %10, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %11) #2
  store i8 15, ptr %11, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %12) #2
  %18 = load i8, ptr %5, align 1, !tbaa !9
  %19 = zext i8 %18 to i32
  %20 = and i32 %19, 240
  %21 = trunc i32 %20 to i8
  store i8 %21, ptr %12, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 1, ptr %13) #2
  %22 = load i8, ptr %5, align 1, !tbaa !9
  %23 = zext i8 %22 to i32
  %24 = and i32 %23, 15
  %25 = trunc i32 %24 to i8
  store i8 %25, ptr %13, align 1, !tbaa !9
  %26 = load ptr, ptr @bmp_buffer_write, align 8, !tbaa !5
  store ptr %26, ptr %8, align 8, !tbaa !5
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %27, ptr %7, align 8, !tbaa !5
  %28 = load i16, ptr %6, align 2, !tbaa !10
  store i16 %28, ptr %9, align 2, !tbaa !10
  br label %29

29:                                               ; preds = %77, %3
  %30 = load i16, ptr %9, align 2, !tbaa !10
  %31 = add i16 %30, -1
  store i16 %31, ptr %9, align 2, !tbaa !10
  %32 = icmp ne i16 %30, 0
  br i1 %32, label %33, label %80

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #2
  %34 = load ptr, ptr %7, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.Vect2D_u16, ptr %34, i32 0, i32 0
  %36 = load i16, ptr %35, align 2, !tbaa !12
  store i16 %36, ptr %14, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 2, ptr %15) #2
  %37 = load ptr, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Vect2D_u16, ptr %37, i32 0, i32 1
  %39 = load i16, ptr %38, align 2, !tbaa !14
  store i16 %39, ptr %15, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #2
  %40 = load i16, ptr %15, align 2, !tbaa !10
  %41 = zext i16 %40 to i32
  %42 = mul nsw i32 %41, 128
  %43 = load i16, ptr %14, align 2, !tbaa !10
  %44 = zext i16 %43 to i32
  %45 = ashr i32 %44, 1
  %46 = add nsw i32 %42, %45
  %47 = trunc i32 %46 to i16
  store i16 %47, ptr %16, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #2
  %48 = load ptr, ptr %8, align 8, !tbaa !5
  %49 = load i16, ptr %16, align 2, !tbaa !10
  %50 = zext i16 %49 to i32
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i8, ptr %48, i64 %51
  store ptr %52, ptr %17, align 8, !tbaa !5
  %53 = load i16, ptr %14, align 2, !tbaa !10
  %54 = zext i16 %53 to i32
  %55 = and i32 %54, 1
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %67

57:                                               ; preds = %33
  %58 = load ptr, ptr %17, align 8, !tbaa !5
  %59 = load i8, ptr %58, align 1, !tbaa !9
  %60 = zext i8 %59 to i32
  %61 = and i32 %60, 240
  %62 = load i8, ptr %13, align 1, !tbaa !9
  %63 = zext i8 %62 to i32
  %64 = or i32 %61, %63
  %65 = trunc i32 %64 to i8
  %66 = load ptr, ptr %17, align 8, !tbaa !5
  store i8 %65, ptr %66, align 1, !tbaa !9
  br label %77

67:                                               ; preds = %33
  %68 = load ptr, ptr %17, align 8, !tbaa !5
  %69 = load i8, ptr %68, align 1, !tbaa !9
  %70 = zext i8 %69 to i32
  %71 = and i32 %70, 15
  %72 = load i8, ptr %12, align 1, !tbaa !9
  %73 = zext i8 %72 to i32
  %74 = or i32 %71, %73
  %75 = trunc i32 %74 to i8
  %76 = load ptr, ptr %17, align 8, !tbaa !5
  store i8 %75, ptr %76, align 1, !tbaa !9
  br label %77

77:                                               ; preds = %67, %57
  %78 = load ptr, ptr %7, align 8, !tbaa !5
  %79 = getelementptr inbounds %struct.Vect2D_u16, ptr %78, i32 1
  store ptr %79, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #2
  br label %29, !llvm.loop !15

80:                                               ; preds = %29
  call void @llvm.lifetime.end.p0(i64 1, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !7, i64 0}
!12 = !{!13, !11, i64 0}
!13 = !{!"", !11, i64 0, !11, i64 2}
!14 = !{!13, !11, i64 2}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}


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

@bmp_buffer_write = 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 @BMP_setPixelsFast_V2D_old(ptr nocapture noundef readonly %0, i8 noundef zeroext %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = load ptr, ptr @bmp_buffer_write, align 8, !tbaa !5
  %.not20 = icmp eq i16 %2, 0
  br i1 %.not20, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %.lr.ph
  %.in = phi i16 [ %5, %.lr.ph ], [ %2, %3 ]
  %.021 = phi ptr [ %18, %.lr.ph ], [ %0, %3 ]
  %5 = add i16 %.in, -1
  %6 = load i16, ptr %.021, align 2, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %.021, i64 2
  %8 = load i16, ptr %7, align 2, !tbaa !12
  %9 = shl i16 %8, 7
  %10 = lshr i16 %6, 1
  %11 = add i16 %9, %10
  %12 = zext i16 %11 to i64
  %13 = getelementptr inbounds i8, ptr %4, i64 %12
  %14 = and i16 %6, 1
  %.not19 = icmp eq i16 %14, 0
  %15 = load i8, ptr %13, align 1, !tbaa !13
  %. = select i1 %.not19, i8 15, i8 -16
  %.24.v = select i1 %.not19, i8 -16, i8 15
  %.24 = and i8 %.24.v, %1
  %16 = and i8 %15, %.
  %17 = or disjoint i8 %16, %.24
  store i8 %17, ptr %13, align 1, !tbaa !13
  %18 = getelementptr inbounds i8, ptr %.021, i64 4
  %.not = icmp eq i16 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph, %3
  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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"", !11, i64 0, !11, i64 2}
!11 = !{!"short", !7, i64 0}
!12 = !{!10, !11, i64 2}
!13 = !{!7, !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass