; 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: nounwind uwtable
declare dso_local ptr @BMP_getWritePointer(i16 noundef zeroext, i16 noundef zeroext) #2

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

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

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

32:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %33 = load ptr, ptr %8, align 8, !tbaa !5
  %34 = call ptr @unpackBitmap(ptr noundef %33, ptr noundef null)
  store ptr %34, ptr %16, align 8, !tbaa !5
  %35 = load ptr, ptr %16, align 8, !tbaa !5
  %36 = icmp eq ptr %35, null
  br i1 %36, label %37, label %38

37:                                               ; preds = %32
  store i8 0, ptr %7, align 1
  store i32 1, ptr %17, align 4
  br label %54

38:                                               ; preds = %32
  %39 = load ptr, ptr %16, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.Bitmap, ptr %39, i32 0, i32 4
  %41 = load ptr, ptr %40, align 8, !tbaa !16
  %42 = load i16, ptr %14, align 2, !tbaa !9
  %43 = load i16, ptr %15, align 2, !tbaa !9
  %44 = load i16, ptr %14, align 2, !tbaa !9
  %45 = load i16, ptr %9, align 2, !tbaa !9
  %46 = load i16, ptr %10, align 2, !tbaa !9
  %47 = call ptr @BMP_getWritePointer(i16 noundef zeroext %45, i16 noundef zeroext %46)
  %48 = load i16, ptr %11, align 2, !tbaa !9
  %49 = zext i16 %48 to i32
  %50 = ashr i32 %49, 1
  %51 = trunc i32 %50 to i16
  %52 = load i16, ptr %12, align 2, !tbaa !9
  call void @BMP_scale(ptr noundef %41, i16 noundef zeroext %42, i16 noundef zeroext %43, i16 noundef zeroext %44, ptr noundef %47, i16 noundef zeroext %51, i16 noundef zeroext %52, i16 noundef zeroext 128)
  %53 = load ptr, ptr %16, align 8, !tbaa !5
  call void @MEM_free(ptr noundef %53)
  store i32 0, ptr %17, align 4
  br label %54

54:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  %55 = load i32, ptr %17, align 4
  switch i32 %55, label %84 [
    i32 0, label %56
  ]

56:                                               ; preds = %54
  br label %72

57:                                               ; preds = %6
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Bitmap, ptr %58, i32 0, i32 4
  %60 = load ptr, ptr %59, align 8, !tbaa !16
  %61 = load i16, ptr %14, align 2, !tbaa !9
  %62 = load i16, ptr %15, align 2, !tbaa !9
  %63 = load i16, ptr %14, align 2, !tbaa !9
  %64 = load i16, ptr %9, align 2, !tbaa !9
  %65 = load i16, ptr %10, align 2, !tbaa !9
  %66 = call ptr @BMP_getWritePointer(i16 noundef zeroext %64, i16 noundef zeroext %65)
  %67 = load i16, ptr %11, align 2, !tbaa !9
  %68 = zext i16 %67 to i32
  %69 = ashr i32 %68, 1
  %70 = trunc i32 %69 to i16
  %71 = load i16, ptr %12, align 2, !tbaa !9
  call void @BMP_scale(ptr noundef %60, i16 noundef zeroext %61, i16 noundef zeroext %62, i16 noundef zeroext %63, ptr noundef %66, i16 noundef zeroext %70, i16 noundef zeroext %71, i16 noundef zeroext 128)
  br label %72

72:                                               ; preds = %57, %56
  %73 = load i8, ptr %13, align 1, !tbaa !11
  %74 = icmp ne i8 %73, 0
  br i1 %74, label %75, label %83

75:                                               ; preds = %72
  %76 = load i16, ptr @pal, align 2, !tbaa !9
  %77 = zext i16 %76 to i32
  %78 = shl i32 %77, 4
  %79 = trunc i32 %78 to i16
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  %81 = getelementptr inbounds %struct.Bitmap, ptr %80, i32 0, i32 3
  %82 = load ptr, ptr %81, align 8, !tbaa !17
  call void @PAL_setPaletteColors(i16 noundef zeroext %79, ptr noundef %82, i32 noundef 0)
  br label %83

83:                                               ; preds = %75, %72
  store i8 1, ptr %7, align 1
  store i32 1, ptr %17, align 4
  br label %84

84:                                               ; preds = %83, %54
  call void @llvm.lifetime.end.p0(i64 2, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #4
  %85 = load i8, ptr %7, align 1
  ret i8 %85
}

; Function Attrs: noinline nounwind uwtable
declare dso_local void @BMP_scale(ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext, ptr noundef, i16 noundef zeroext, i16 noundef zeroext, i16 noundef zeroext) #3

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 = { 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 = { 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 #4 = { 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: nounwind uwtable
declare dso_local ptr @BMP_getWritePointer(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #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

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

13:                                               ; preds = %6
  %14 = tail call ptr @unpackBitmap(ptr noundef nonnull %0, ptr noundef null) #3
  %.not27 = icmp eq ptr %14, null
  br i1 %.not27, label %.critedge, label %15

15:                                               ; preds = %13
  %16 = getelementptr inbounds i8, ptr %14, i64 16
  %17 = load ptr, ptr %16, align 8, !tbaa !13
  %18 = tail call ptr @BMP_getWritePointer(i16 noundef zeroext %1, i16 noundef zeroext %2)
  %19 = lshr i16 %3, 1
  tail call void @BMP_scale(ptr noundef %17, i16 noundef zeroext %9, i16 noundef zeroext %11, i16 noundef zeroext %9, ptr noundef %18, i16 noundef zeroext %19, i16 noundef zeroext %4, i16 noundef zeroext 128)
  tail call void @MEM_free(ptr noundef nonnull %14) #3
  br label %25

20:                                               ; preds = %6
  %21 = getelementptr inbounds i8, ptr %0, i64 16
  %22 = load ptr, ptr %21, align 8, !tbaa !13
  %23 = tail call ptr @BMP_getWritePointer(i16 noundef zeroext %1, i16 noundef zeroext %2)
  %24 = lshr i16 %3, 1
  tail call void @BMP_scale(ptr noundef %22, i16 noundef zeroext %9, i16 noundef zeroext %11, i16 noundef zeroext %9, ptr noundef %23, i16 noundef zeroext %24, i16 noundef zeroext %4, i16 noundef zeroext 128)
  br label %25

25:                                               ; preds = %15, %20
  %.not28 = icmp eq i8 %5, 0
  br i1 %.not28, label %.critedge, label %26

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

.critedge:                                        ; preds = %13, %25, %26
  %.1 = phi i8 [ 1, %26 ], [ 1, %25 ], [ 0, %13 ]
  ret i8 %.1
}

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

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 = { 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 = { 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, !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