; 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.Pixel = type { %struct.Vect2D_s16, i16 }
%struct.Vect2D_s16 = 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_old(ptr noundef %0, i16 noundef zeroext %1) #1 {
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i8, align 1
  %9 = alloca i8, align 1
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca i16, align 2
  %13 = alloca ptr, align 8
  %14 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i16 %1, ptr %4, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 2, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  store i8 -16, ptr %8, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  store i8 15, ptr %9, align 1, !tbaa !11
  %15 = load ptr, ptr @bmp_buffer_write, align 8, !tbaa !5
  store ptr %15, ptr %6, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %16, ptr %5, align 8, !tbaa !5
  %17 = load i16, ptr %4, align 2, !tbaa !9
  store i16 %17, ptr %7, align 2, !tbaa !9
  br label %18

18:                                               ; preds = %74, %2
  %19 = load i16, ptr %7, align 2, !tbaa !9
  %20 = add i16 %19, -1
  store i16 %20, ptr %7, align 2, !tbaa !9
  %21 = icmp ne i16 %19, 0
  br i1 %21, label %22, label %77

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #2
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds %struct.Pixel, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds %struct.Vect2D_s16, ptr %24, i32 0, i32 0
  %26 = load i16, ptr %25, align 2, !tbaa !12
  store i16 %26, ptr %10, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #2
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.Pixel, ptr %27, i32 0, i32 0
  %29 = getelementptr inbounds %struct.Vect2D_s16, ptr %28, i32 0, i32 1
  %30 = load i16, ptr %29, align 2, !tbaa !15
  store i16 %30, ptr %11, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #2
  %31 = load i16, ptr %11, align 2, !tbaa !9
  %32 = zext i16 %31 to i32
  %33 = mul nsw i32 %32, 128
  %34 = load i16, ptr %10, align 2, !tbaa !9
  %35 = zext i16 %34 to i32
  %36 = ashr i32 %35, 1
  %37 = add nsw i32 %33, %36
  %38 = trunc i32 %37 to i16
  store i16 %38, ptr %12, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = load i16, ptr %12, align 2, !tbaa !9
  %41 = zext i16 %40 to i32
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds i8, ptr %39, i64 %42
  store ptr %43, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #2
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Pixel, ptr %44, i32 0, i32 1
  %46 = load i16, ptr %45, align 2, !tbaa !16
  %47 = trunc i16 %46 to i8
  store i8 %47, ptr %14, align 1, !tbaa !11
  %48 = load i16, ptr %10, align 2, !tbaa !9
  %49 = zext i16 %48 to i32
  %50 = and i32 %49, 1
  %51 = icmp ne i32 %50, 0
  br i1 %51, label %52, label %63

52:                                               ; preds = %22
  %53 = load ptr, ptr %13, align 8, !tbaa !5
  %54 = load i8, ptr %53, align 1, !tbaa !11
  %55 = zext i8 %54 to i32
  %56 = and i32 %55, 240
  %57 = load i8, ptr %14, align 1, !tbaa !11
  %58 = zext i8 %57 to i32
  %59 = and i32 %58, 15
  %60 = or i32 %56, %59
  %61 = trunc i32 %60 to i8
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  store i8 %61, ptr %62, align 1, !tbaa !11
  br label %74

63:                                               ; preds = %22
  %64 = load ptr, ptr %13, align 8, !tbaa !5
  %65 = load i8, ptr %64, align 1, !tbaa !11
  %66 = zext i8 %65 to i32
  %67 = and i32 %66, 15
  %68 = load i8, ptr %14, align 1, !tbaa !11
  %69 = zext i8 %68 to i32
  %70 = and i32 %69, 240
  %71 = or i32 %67, %70
  %72 = trunc i32 %71 to i8
  %73 = load ptr, ptr %13, align 8, !tbaa !5
  store i8 %72, ptr %73, align 1, !tbaa !11
  br label %74

74:                                               ; preds = %63, %52
  %75 = load ptr, ptr %5, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.Pixel, ptr %75, i32 1
  store ptr %76, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #2
  br label %18, !llvm.loop !17

77:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #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 = !{!10, !10, i64 0}
!10 = !{!"short", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 0}
!13 = !{!"", !14, i64 0, !10, i64 4}
!14 = !{!"", !10, i64 0, !10, i64 2}
!15 = !{!13, !10, i64 2}
!16 = !{!13, !10, i64 4}
!17 = distinct !{!17, !18}
!18 = !{!"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_old(ptr nocapture noundef readonly %0, i16 noundef zeroext %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr @bmp_buffer_write, align 8, !tbaa !5
  %.not19 = icmp eq i16 %1, 0
  br i1 %.not19, label %._crit_edge, label %.lr.ph

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

._crit_edge:                                      ; preds = %.lr.ph, %2
  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, !12, i64 0}
!10 = !{!"", !11, i64 0, !12, i64 4}
!11 = !{!"", !12, i64 0, !12, i64 2}
!12 = !{!"short", !7, i64 0}
!13 = !{!10, !12, i64 2}
!14 = !{!10, !12, i64 4}
!15 = !{!7, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}


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