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

@pal = external hidden global i16, align 2

declare void @MEM_free(ptr noundef) #0

; 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

; Function Attrs: noinline nounwind uwtable
declare dso_local void @BMP_drawBitmapData(ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i64 noundef) #2

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext i8 @BMP_drawBitmap(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i8 noundef zeroext %3) #2 {
  %5 = alloca i8, align 1
  %6 = alloca ptr, align 8
  %7 = alloca i16, align 2
  %8 = alloca i16, align 2
  %9 = alloca i8, align 1
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i16 %1, ptr %7, align 2, !tbaa !9
  store i16 %2, ptr %8, align 2, !tbaa !9
  store i8 %3, ptr %9, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 2, ptr %10) #3
  call void @llvm.lifetime.start.p0(i64 2, ptr %11) #3
  %14 = load ptr, ptr %6, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Bitmap, ptr %14, i32 0, i32 1
  %16 = load i16, ptr %15, align 2, !tbaa !12
  store i16 %16, ptr %10, align 2, !tbaa !9
  %17 = load ptr, ptr %6, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.Bitmap, ptr %17, i32 0, i32 2
  %19 = load i16, ptr %18, align 4, !tbaa !14
  store i16 %19, ptr %11, align 2, !tbaa !9
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.Bitmap, ptr %20, i32 0, i32 0
  %22 = load i16, ptr %21, align 8, !tbaa !15
  %23 = zext i16 %22 to i32
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %47

25:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = call ptr @unpackBitmap(ptr noundef %26, ptr noundef null)
  store ptr %27, ptr %12, align 8, !tbaa !5
  %28 = load ptr, ptr %12, align 8, !tbaa !5
  %29 = icmp eq ptr %28, null
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  store i8 0, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %44

31:                                               ; preds = %25
  %32 = load ptr, ptr %12, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.Bitmap, ptr %32, i32 0, i32 4
  %34 = load ptr, ptr %33, align 8, !tbaa !16
  %35 = load i16, ptr %7, align 2, !tbaa !9
  %36 = load i16, ptr %8, align 2, !tbaa !9
  %37 = load i16, ptr %10, align 2, !tbaa !9
  %38 = load i16, ptr %11, align 2, !tbaa !9
  %39 = load i16, ptr %10, align 2, !tbaa !9
  %40 = zext i16 %39 to i32
  %41 = ashr i32 %40, 1
  %42 = sext i32 %41 to i64
  call void @BMP_drawBitmapData(ptr noundef %34, i16 noundef zeroext %35, i16 noundef zeroext %36, i16 noundef zeroext %37, i16 noundef zeroext %38, i64 noundef %42)
  %43 = load ptr, ptr %12, align 8, !tbaa !5
  call void @MEM_free(ptr noundef %43)
  store i32 0, ptr %13, align 4
  br label %44

44:                                               ; preds = %31, %30
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  %45 = load i32, ptr %13, align 4
  switch i32 %45, label %71 [
    i32 0, label %46
  ]

46:                                               ; preds = %44
  br label %59

47:                                               ; preds = %4
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Bitmap, ptr %48, i32 0, i32 4
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  %51 = load i16, ptr %7, align 2, !tbaa !9
  %52 = load i16, ptr %8, align 2, !tbaa !9
  %53 = load i16, ptr %10, align 2, !tbaa !9
  %54 = load i16, ptr %11, align 2, !tbaa !9
  %55 = load i16, ptr %10, align 2, !tbaa !9
  %56 = zext i16 %55 to i32
  %57 = ashr i32 %56, 1
  %58 = sext i32 %57 to i64
  call void @BMP_drawBitmapData(ptr noundef %50, i16 noundef zeroext %51, i16 noundef zeroext %52, i16 noundef zeroext %53, i16 noundef zeroext %54, i64 noundef %58)
  br label %59

59:                                               ; preds = %47, %46
  %60 = load i8, ptr %9, align 1, !tbaa !11
  %61 = icmp ne i8 %60, 0
  br i1 %61, label %62, label %70

62:                                               ; preds = %59
  %63 = load i16, ptr @pal, align 2, !tbaa !9
  %64 = zext i16 %63 to i32
  %65 = shl i32 %64, 4
  %66 = trunc i32 %65 to i16
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Bitmap, ptr %67, i32 0, i32 3
  %69 = load ptr, ptr %68, align 8, !tbaa !17
  call void @PAL_setPaletteColors(i16 noundef zeroext %66, ptr noundef %69, i32 noundef 0)
  br label %70

70:                                               ; preds = %62, %59
  store i8 1, ptr %5, align 1
  store i32 1, ptr %13, align 4
  br label %71

71:                                               ; preds = %70, %44
  call void @llvm.lifetime.end.p0(i64 2, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 2, ptr %10) #3
  %72 = load i8, ptr %5, align 1
  ret i8 %72
}

declare ptr @unpackBitmap(ptr noundef, ptr noundef) #0

declare void @PAL_setPaletteColors(i16 noundef zeroext, ptr noundef, i32 noundef) #0

attributes #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 = { noinline 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 #3 = { 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 2}
!13 = !{!"", !10, i64 0, !10, i64 2, !10, i64 4, !6, i64 8, !6, i64 16}
!14 = !{!13, !10, i64 4}
!15 = !{!13, !10, i64 0}
!16 = !{!13, !6, i64 16}
!17 = !{!13, !6, i64 8}


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

@pal = external hidden local_unnamed_addr global i16, align 2

declare void @MEM_free(ptr noundef) local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
declare dso_local void @BMP_drawBitmapData(ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, i64 noundef) local_unnamed_addr #1

; Function Attrs: noinline nounwind uwtable
define dso_local zeroext range(i8 0, 2) i8 @BMP_drawBitmap(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i8 noundef zeroext %3) local_unnamed_addr #1 {
  %5 = getelementptr inbounds i8, ptr %0, i64 2
  %6 = load i16, ptr %5, align 2, !tbaa !5
  %7 = getelementptr inbounds i8, ptr %0, i64 4
  %8 = load i16, ptr %7, align 4, !tbaa !11
  %9 = load i16, ptr %0, align 8, !tbaa !12
  %.not = icmp eq i16 %9, 0
  br i1 %.not, label %17, label %10

10:                                               ; preds = %4
  %11 = tail call ptr @unpackBitmap(ptr noundef nonnull %0, ptr noundef null) #2
  %.not23 = icmp eq ptr %11, null
  br i1 %.not23, label %.critedge, label %12

12:                                               ; preds = %10
  %13 = getelementptr inbounds i8, ptr %11, i64 16
  %14 = load ptr, ptr %13, align 8, !tbaa !13
  %15 = lshr i16 %6, 1
  %16 = zext nneg i16 %15 to i64
  tail call void @BMP_drawBitmapData(ptr noundef %14, i16 noundef zeroext %1, i16 noundef zeroext %2, i16 noundef zeroext %6, i16 noundef zeroext %8, i64 noundef %16)
  tail call void @MEM_free(ptr noundef nonnull %11) #2
  br label %22

17:                                               ; preds = %4
  %18 = getelementptr inbounds i8, ptr %0, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !13
  %20 = lshr i16 %6, 1
  %21 = zext nneg i16 %20 to i64
  tail call void @BMP_drawBitmapData(ptr noundef %19, i16 noundef zeroext %1, i16 noundef zeroext %2, i16 noundef zeroext %6, i16 noundef zeroext %8, i64 noundef %21)
  br label %22

22:                                               ; preds = %12, %17
  %.not24 = icmp eq i8 %3, 0
  br i1 %.not24, label %.critedge, label %23

23:                                               ; preds = %22
  %24 = load i16, ptr @pal, align 2, !tbaa !14
  %25 = shl i16 %24, 4
  %26 = getelementptr inbounds i8, ptr %0, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  tail call void @PAL_setPaletteColors(i16 noundef zeroext %25, ptr noundef %27, i32 noundef 0) #2
  br label %.critedge

.critedge:                                        ; preds = %10, %22, %23
  %.1 = phi i8 [ 1, %23 ], [ 1, %22 ], [ 0, %10 ]
  ret i8 %.1
}

declare ptr @unpackBitmap(ptr noundef, ptr noundef) local_unnamed_addr #0

declare void @PAL_setPaletteColors(i16 noundef zeroext, ptr noundef, i32 noundef) local_unnamed_addr #0

attributes #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 = { noinline 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, !7, i64 2}
!6 = !{!"", !7, i64 0, !7, i64 2, !7, i64 4, !10, i64 8, !10, i64 16}
!7 = !{!"short", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !7, i64 0}
!13 = !{!6, !10, i64 16}
!14 = !{!7, !7, i64 0}
!15 = !{!6, !10, i64 8}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,SROAPass,SimplifyCFGPass,TailCallElimPass