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

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
  %31 = load i16, ptr %10, align 2, !tbaa !9
  %32 = zext i16 %31 to i32
  %33 = icmp slt i32 %32, 256
  br i1 %33, label %34, label %83

34:                                               ; preds = %22
  %35 = load i16, ptr %11, align 2, !tbaa !9
  %36 = zext i16 %35 to i32
  %37 = icmp slt i32 %36, 160
  br i1 %37, label %38, label %83

38:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 2, ptr %12) #2
  %39 = load i16, ptr %11, align 2, !tbaa !9
  %40 = zext i16 %39 to i32
  %41 = mul nsw i32 %40, 128
  %42 = load i16, ptr %10, align 2, !tbaa !9
  %43 = zext i16 %42 to i32
  %44 = ashr i32 %43, 1
  %45 = add nsw i32 %41, %44
  %46 = trunc i32 %45 to i16
  store i16 %46, ptr %12, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load i16, ptr %12, align 2, !tbaa !9
  %49 = zext i16 %48 to i32
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %47, i64 %50
  store ptr %51, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #2
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.Pixel, ptr %52, i32 0, i32 1
  %54 = load i16, ptr %53, align 2, !tbaa !16
  %55 = trunc i16 %54 to i8
  store i8 %55, ptr %14, align 1, !tbaa !11
  %56 = load i16, ptr %10, align 2, !tbaa !9
  %57 = zext i16 %56 to i32
  %58 = and i32 %57, 1
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %60, label %71

60:                                               ; preds = %38
  %61 = load ptr, ptr %13, align 8, !tbaa !5
  %62 = load i8, ptr %61, align 1, !tbaa !11
  %63 = zext i8 %62 to i32
  %64 = and i32 %63, 240
  %65 = load i8, ptr %14, align 1, !tbaa !11
  %66 = zext i8 %65 to i32
  %67 = and i32 %66, 15
  %68 = or i32 %64, %67
  %69 = trunc i32 %68 to i8
  %70 = load ptr, ptr %13, align 8, !tbaa !5
  store i8 %69, ptr %70, align 1, !tbaa !11
  br label %82

71:                                               ; preds = %38
  %72 = load ptr, ptr %13, align 8, !tbaa !5
  %73 = load i8, ptr %72, align 1, !tbaa !11
  %74 = zext i8 %73 to i32
  %75 = and i32 %74, 15
  %76 = load i8, ptr %14, align 1, !tbaa !11
  %77 = zext i8 %76 to i32
  %78 = and i32 %77, 240
  %79 = or i32 %75, %78
  %80 = trunc i32 %79 to i8
  %81 = load ptr, ptr %13, align 8, !tbaa !5
  store i8 %80, ptr %81, align 1, !tbaa !11
  br label %82

82:                                               ; preds = %71, %60
  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
  br label %83

83:                                               ; preds = %82, %34, %22
  %84 = load ptr, ptr %5, align 8, !tbaa !5
  %85 = getelementptr inbounds %struct.Pixel, ptr %84, i32 1
  store ptr %85, ptr %5, align 8, !tbaa !5
  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

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

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

10:                                               ; preds = %.lr.ph
  %11 = shl nuw nsw i16 %7, 7
  %12 = lshr i16 %5, 1
  %13 = or disjoint i16 %11, %12
  %14 = zext nneg i16 %13 to i64
  %15 = getelementptr inbounds i8, ptr %3, i64 %14
  %16 = getelementptr inbounds i8, ptr %.026, i64 4
  %17 = load i16, ptr %16, align 2, !tbaa !14
  %18 = and i16 %5, 1
  %.not24 = icmp eq i16 %18, 0
  %19 = load i8, ptr %15, align 1, !tbaa !15
  %20 = trunc i16 %17 to i8
  %. = select i1 %.not24, i8 15, i8 -16
  %.30 = select i1 %.not24, i8 -16, i8 15
  %21 = and i8 %19, %.
  %22 = and i8 %.30, %20
  %23 = or disjoint i8 %21, %22
  store i8 %23, ptr %15, align 1, !tbaa !15
  br label %24

24:                                               ; preds = %10, %.lr.ph
  %25 = getelementptr inbounds i8, ptr %.026, i64 6
  %.not = icmp eq i16 %4, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !16

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