; 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_setPixels_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 = %86, %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 %89

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
  %40 = load i16, ptr %14, align 2, !tbaa !10
  %41 = zext i16 %40 to i32
  %42 = icmp slt i32 %41, 256
  br i1 %42, label %43, label %86

43:                                               ; preds = %33
  %44 = load i16, ptr %15, align 2, !tbaa !10
  %45 = zext i16 %44 to i32
  %46 = icmp slt i32 %45, 160
  br i1 %46, label %47, label %86

47:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 2, ptr %16) #2
  %48 = load i16, ptr %15, align 2, !tbaa !10
  %49 = zext i16 %48 to i32
  %50 = mul nsw i32 %49, 128
  %51 = load i16, ptr %14, align 2, !tbaa !10
  %52 = zext i16 %51 to i32
  %53 = ashr i32 %52, 1
  %54 = add nsw i32 %50, %53
  %55 = trunc i32 %54 to i16
  store i16 %55, ptr %16, align 2, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 8, ptr %17) #2
  %56 = load ptr, ptr %8, align 8, !tbaa !5
  %57 = load i16, ptr %16, align 2, !tbaa !10
  %58 = zext i16 %57 to i32
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds i8, ptr %56, i64 %59
  store ptr %60, ptr %17, align 8, !tbaa !5
  %61 = load i16, ptr %14, align 2, !tbaa !10
  %62 = zext i16 %61 to i32
  %63 = and i32 %62, 1
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %75

65:                                               ; preds = %47
  %66 = load ptr, ptr %17, align 8, !tbaa !5
  %67 = load i8, ptr %66, align 1, !tbaa !9
  %68 = zext i8 %67 to i32
  %69 = and i32 %68, 240
  %70 = load i8, ptr %13, align 1, !tbaa !9
  %71 = zext i8 %70 to i32
  %72 = or i32 %69, %71
  %73 = trunc i32 %72 to i8
  %74 = load ptr, ptr %17, align 8, !tbaa !5
  store i8 %73, ptr %74, align 1, !tbaa !9
  br label %85

75:                                               ; preds = %47
  %76 = load ptr, ptr %17, align 8, !tbaa !5
  %77 = load i8, ptr %76, align 1, !tbaa !9
  %78 = zext i8 %77 to i32
  %79 = and i32 %78, 15
  %80 = load i8, ptr %12, align 1, !tbaa !9
  %81 = zext i8 %80 to i32
  %82 = or i32 %79, %81
  %83 = trunc i32 %82 to i8
  %84 = load ptr, ptr %17, align 8, !tbaa !5
  store i8 %83, ptr %84, align 1, !tbaa !9
  br label %85

85:                                               ; preds = %75, %65
  call void @llvm.lifetime.end.p0(i64 8, ptr %17) #2
  call void @llvm.lifetime.end.p0(i64 2, ptr %16) #2
  br label %86

86:                                               ; preds = %85, %43, %33
  %87 = load ptr, ptr %7, align 8, !tbaa !5
  %88 = getelementptr inbounds %struct.Vect2D_u16, ptr %87, i32 1
  store ptr %88, ptr %7, align 8, !tbaa !5
  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

89:                                               ; 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_setPixels_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
  %.not26 = icmp eq i16 %2, 0
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %21
  %.in = phi i16 [ %5, %21 ], [ %2, %3 ]
  %.027 = phi ptr [ %22, %21 ], [ %0, %3 ]
  %5 = add i16 %.in, -1
  %6 = load i16, ptr %.027, align 2, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %.027, i64 2
  %8 = load i16, ptr %7, align 2, !tbaa !12
  %9 = icmp ult i16 %6, 256
  %10 = icmp ult i16 %8, 160
  %or.cond = select i1 %9, i1 %10, i1 false
  br i1 %or.cond, label %11, label %21

11:                                               ; preds = %.lr.ph
  %12 = shl nuw nsw i16 %8, 7
  %13 = lshr i16 %6, 1
  %14 = or disjoint i16 %12, %13
  %15 = zext nneg i16 %14 to i64
  %16 = getelementptr inbounds i8, ptr %4, i64 %15
  %17 = and i16 %6, 1
  %.not25 = icmp eq i16 %17, 0
  %18 = load i8, ptr %16, align 1, !tbaa !13
  %. = select i1 %.not25, i8 15, i8 -16
  %.30.v = select i1 %.not25, i8 -16, i8 15
  %.30 = and i8 %.30.v, %1
  %19 = and i8 %18, %.
  %20 = or disjoint i8 %19, %.30
  store i8 %20, ptr %16, align 1, !tbaa !13
  br label %21

21:                                               ; preds = %11, %.lr.ph
  %22 = getelementptr inbounds i8, ptr %.027, i64 4
  %.not = icmp eq i16 %5, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %21, %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