; LLVM IR from Dovyski/opengl-demos :: src/texture-simple-woodbox/bmp.ll (ZN3Bmp15convertBGRtoRGBEv)

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

%class.Bmp = type { i32, i32, i32, i32, i32, ptr, %struct.HEADER, %struct.INFOHEADER }
%struct.HEADER = type { i16, i32, i16, i16, i32 }
%struct.INFOHEADER = type { i32, i32, i32, i16, i16, i32, i32, i32, i32, i32, i32 }

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN3Bmp15convertBGRtoRGBEv(ptr noundef nonnull align 8 dereferenceable(88) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i8, align 1
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %8 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #2
  %9 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 5
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %67

12:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !15
  br label %13

13:                                               ; preds = %63, %12
  %14 = load i32, ptr %4, align 4, !tbaa !15
  %15 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 1
  %16 = load i32, ptr %15, align 4, !tbaa !16
  %17 = icmp slt i32 %14, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %13
  store i32 2, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %66

19:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !15
  br label %20

20:                                               ; preds = %59, %19
  %21 = load i32, ptr %6, align 4, !tbaa !15
  %22 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 0
  %23 = load i32, ptr %22, align 8, !tbaa !17
  %24 = mul nsw i32 %23, 3
  %25 = icmp slt i32 %21, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %20
  store i32 5, ptr %5, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %62

27:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %28 = load i32, ptr %4, align 4, !tbaa !15
  %29 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 3
  %30 = load i32, ptr %29, align 4, !tbaa !18
  %31 = mul nsw i32 %28, %30
  %32 = load i32, ptr %6, align 4, !tbaa !15
  %33 = add nsw i32 %31, %32
  store i32 %33, ptr %7, align 4, !tbaa !15
  %34 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 5
  %35 = load ptr, ptr %34, align 8, !tbaa !9
  %36 = load i32, ptr %7, align 4, !tbaa !15
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds i8, ptr %35, i64 %37
  %39 = load i8, ptr %38, align 1, !tbaa !19
  store i8 %39, ptr %3, align 1, !tbaa !19
  %40 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 5
  %41 = load ptr, ptr %40, align 8, !tbaa !9
  %42 = load i32, ptr %7, align 4, !tbaa !15
  %43 = add nsw i32 %42, 2
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i8, ptr %41, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !19
  %47 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 5
  %48 = load ptr, ptr %47, align 8, !tbaa !9
  %49 = load i32, ptr %7, align 4, !tbaa !15
  %50 = sext i32 %49 to i64
  %51 = getelementptr inbounds i8, ptr %48, i64 %50
  store i8 %46, ptr %51, align 1, !tbaa !19
  %52 = load i8, ptr %3, align 1, !tbaa !19
  %53 = getelementptr inbounds %class.Bmp, ptr %8, i32 0, i32 5
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  %55 = load i32, ptr %7, align 4, !tbaa !15
  %56 = add nsw i32 %55, 2
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i8, ptr %54, i64 %57
  store i8 %52, ptr %58, align 1, !tbaa !19
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %59

59:                                               ; preds = %27
  %60 = load i32, ptr %6, align 4, !tbaa !15
  %61 = add nsw i32 %60, 3
  store i32 %61, ptr %6, align 4, !tbaa !15
  br label %20, !llvm.loop !20

62:                                               ; preds = %26
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %4, align 4, !tbaa !15
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %4, align 4, !tbaa !15
  br label %13, !llvm.loop !22

66:                                               ; preds = %18
  br label %67

67:                                               ; preds = %66, %1
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #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

attributes #0 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !6, i64 24}
!10 = !{!"_ZTS3Bmp", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !11, i64 16, !6, i64 24, !12, i64 32, !14, i64 48}
!11 = !{!"int", !7, i64 0}
!12 = !{!"_ZTS6HEADER", !13, i64 0, !11, i64 4, !13, i64 8, !13, i64 10, !11, i64 12}
!13 = !{!"short", !7, i64 0}
!14 = !{!"_ZTS10INFOHEADER", !11, i64 0, !11, i64 4, !11, i64 8, !13, i64 12, !13, i64 14, !11, i64 16, !11, i64 20, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36}
!15 = !{!11, !11, i64 0}
!16 = !{!10, !11, i64 4}
!17 = !{!10, !11, i64 0}
!18 = !{!10, !11, i64 12}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


; 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: mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN3Bmp15convertBGRtoRGBEv(ptr nocapture noundef nonnull readonly align 8 dereferenceable(88) %0) local_unnamed_addr #0 align 2 {
  %2 = getelementptr inbounds i8, ptr %0, i64 24
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.loopexit, label %.preheader12

.preheader12:                                     ; preds = %1
  %4 = getelementptr inbounds i8, ptr %0, i64 4
  %5 = load i32, ptr %4, align 4, !tbaa !14
  %6 = icmp sgt i32 %5, 0
  br i1 %6, label %.preheader.lr.ph, label %.loopexit

.preheader.lr.ph:                                 ; preds = %.preheader12
  %7 = getelementptr inbounds i8, ptr %0, i64 12
  %.pre = load i32, ptr %0, align 8, !tbaa !15
  br label %.preheader

.preheader:                                       ; preds = %.preheader.lr.ph, %._crit_edge
  %8 = phi i32 [ %5, %.preheader.lr.ph ], [ %11, %._crit_edge ]
  %9 = phi i32 [ %.pre, %.preheader.lr.ph ], [ %12, %._crit_edge ]
  %.014 = phi i32 [ 0, %.preheader.lr.ph ], [ %13, %._crit_edge ]
  %10 = icmp sgt i32 %9, 0
  br i1 %10, label %.lr.ph, label %._crit_edge

._crit_edge.loopexit:                             ; preds = %.lr.ph
  %.pre15 = load i32, ptr %4, align 4, !tbaa !14
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.preheader
  %11 = phi i32 [ %.pre15, %._crit_edge.loopexit ], [ %8, %.preheader ]
  %12 = phi i32 [ %29, %._crit_edge.loopexit ], [ %9, %.preheader ]
  %13 = add nuw nsw i32 %.014, 1
  %14 = icmp slt i32 %13, %11
  br i1 %14, label %.preheader, label %.loopexit, !llvm.loop !16

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %.01113 = phi i32 [ %28, %.lr.ph ], [ 0, %.preheader ]
  %15 = load i32, ptr %7, align 4, !tbaa !18
  %16 = mul nsw i32 %15, %.014
  %17 = add nsw i32 %16, %.01113
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  %19 = sext i32 %17 to i64
  %20 = getelementptr inbounds i8, ptr %18, i64 %19
  %21 = load i8, ptr %20, align 1, !tbaa !19
  %22 = add nsw i32 %17, 2
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i8, ptr %18, i64 %23
  %25 = load i8, ptr %24, align 1, !tbaa !19
  store i8 %25, ptr %20, align 1, !tbaa !19
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 %23
  store i8 %21, ptr %27, align 1, !tbaa !19
  %28 = add nuw nsw i32 %.01113, 3
  %29 = load i32, ptr %0, align 8, !tbaa !15
  %30 = mul nsw i32 %29, 3
  %31 = icmp slt i32 %28, %30
  br i1 %31, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !20

.loopexit:                                        ; preds = %._crit_edge, %.preheader12, %1
  ret void
}

attributes #0 = { mustprogress 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, !10, i64 24}
!6 = !{!"_ZTS3Bmp", !7, i64 0, !7, i64 4, !7, i64 8, !7, i64 12, !7, i64 16, !10, i64 24, !11, i64 32, !13, i64 48}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!"_ZTS6HEADER", !12, i64 0, !7, i64 4, !12, i64 8, !12, i64 10, !7, i64 12}
!12 = !{!"short", !8, i64 0}
!13 = !{!"_ZTS10INFOHEADER", !7, i64 0, !7, i64 4, !7, i64 8, !12, i64 12, !12, i64 14, !7, i64 16, !7, i64 20, !7, i64 24, !7, i64 28, !7, i64 32, !7, i64 36}
!14 = !{!6, !7, i64 4}
!15 = !{!6, !7, i64 0}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!6, !7, i64 12}
!19 = !{!8, !8, i64 0}
!20 = distinct !{!20, !17}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass