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

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

; Function Attrs: noinline nounwind uwtable
define dso_local void @BMP_drawBitmapData(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i64 noundef %5) #2 {
  %7 = alloca ptr, align 8
  %8 = alloca i16, align 2
  %9 = alloca i16, align 2
  %10 = alloca i16, align 2
  %11 = alloca i16, align 2
  %12 = alloca i64, align 8
  %13 = alloca i16, align 2
  %14 = alloca i16, align 2
  %15 = alloca ptr, align 8
  %16 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store i16 %1, ptr %8, align 2, !tbaa !9
  store i16 %2, ptr %9, align 2, !tbaa !9
  store i16 %3, ptr %10, align 2, !tbaa !9
  store i16 %4, ptr %11, align 2, !tbaa !9
  store i64 %5, ptr %12, align 8, !tbaa !11
  %17 = load i16, ptr %8, align 2, !tbaa !9
  %18 = zext i16 %17 to i32
  %19 = icmp sge i32 %18, 256
  br i1 %19, label %24, label %20

20:                                               ; preds = %6
  %21 = load i16, ptr %9, align 2, !tbaa !9
  %22 = zext i16 %21 to i32
  %23 = icmp sge i32 %22, 160
  br i1 %23, label %24, label %25

24:                                               ; preds = %20, %6
  br label %82

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 2, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 2, ptr %14) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %26 = load i16, ptr %10, align 2, !tbaa !9
  %27 = zext i16 %26 to i32
  %28 = load i16, ptr %8, align 2, !tbaa !9
  %29 = zext i16 %28 to i32
  %30 = add nsw i32 %27, %29
  %31 = icmp sgt i32 %30, 256
  br i1 %31, label %32, label %41

32:                                               ; preds = %25
  %33 = load i16, ptr %10, align 2, !tbaa !9
  %34 = zext i16 %33 to i32
  %35 = load i16, ptr %8, align 2, !tbaa !9
  %36 = zext i16 %35 to i32
  %37 = add nsw i32 %34, %36
  %38 = sub nsw i32 256, %37
  %39 = ashr i32 %38, 1
  %40 = trunc i32 %39 to i16
  store i16 %40, ptr %13, align 2, !tbaa !9
  br label %46

41:                                               ; preds = %25
  %42 = load i16, ptr %10, align 2, !tbaa !9
  %43 = zext i16 %42 to i32
  %44 = ashr i32 %43, 1
  %45 = trunc i32 %44 to i16
  store i16 %45, ptr %13, align 2, !tbaa !9
  br label %46

46:                                               ; preds = %41, %32
  %47 = load i16, ptr %11, align 2, !tbaa !9
  %48 = zext i16 %47 to i32
  %49 = load i16, ptr %9, align 2, !tbaa !9
  %50 = zext i16 %49 to i32
  %51 = add nsw i32 %48, %50
  %52 = icmp sgt i32 %51, 160
  br i1 %52, label %53, label %61

53:                                               ; preds = %46
  %54 = load i16, ptr %11, align 2, !tbaa !9
  %55 = zext i16 %54 to i32
  %56 = load i16, ptr %9, align 2, !tbaa !9
  %57 = zext i16 %56 to i32
  %58 = add nsw i32 %55, %57
  %59 = sub nsw i32 160, %58
  %60 = trunc i32 %59 to i16
  store i16 %60, ptr %14, align 2, !tbaa !9
  br label %63

61:                                               ; preds = %46
  %62 = load i16, ptr %11, align 2, !tbaa !9
  store i16 %62, ptr %14, align 2, !tbaa !9
  br label %63

63:                                               ; preds = %61, %53
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %64, ptr %15, align 8, !tbaa !5
  %65 = load i16, ptr %8, align 2, !tbaa !9
  %66 = load i16, ptr %9, align 2, !tbaa !9
  %67 = call ptr @BMP_getWritePointer(i16 noundef zeroext %65, i16 noundef zeroext %66)
  store ptr %67, ptr %16, align 8, !tbaa !5
  br label %68

68:                                               ; preds = %72, %63
  %69 = load i16, ptr %14, align 2, !tbaa !9
  %70 = add i16 %69, -1
  store i16 %70, ptr %14, align 2, !tbaa !9
  %71 = icmp ne i16 %69, 0
  br i1 %71, label %72, label %81

72:                                               ; preds = %68
  %73 = load ptr, ptr %16, align 8, !tbaa !5
  %74 = load ptr, ptr %15, align 8, !tbaa !5
  %75 = load i16, ptr %13, align 2, !tbaa !9
  call void @memcpy(ptr noundef %73, ptr noundef %74, i16 noundef zeroext %75)
  %76 = load i64, ptr %12, align 8, !tbaa !11
  %77 = load ptr, ptr %15, align 8, !tbaa !5
  %78 = getelementptr inbounds i8, ptr %77, i64 %76
  store ptr %78, ptr %15, align 8, !tbaa !5
  %79 = load ptr, ptr %16, align 8, !tbaa !5
  %80 = getelementptr inbounds i8, ptr %79, i64 128
  store ptr %80, ptr %16, align 8, !tbaa !5
  br label %68, !llvm.loop !13

81:                                               ; preds = %68
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %13) #4
  br label %82

82:                                               ; preds = %81, %24
  ret void
}

declare void @memcpy(ptr noundef, ptr noundef, i16 noundef zeroext) #3

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 = { 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 = { "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 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local ptr @BMP_getWritePointer(i16 noundef zeroext, i16 noundef zeroext) local_unnamed_addr #0

; Function Attrs: noinline nounwind uwtable
define dso_local void @BMP_drawBitmapData(ptr noundef %0, i16 noundef zeroext %1, i16 noundef zeroext %2, i16 noundef zeroext %3, i16 noundef zeroext %4, i64 noundef %5) local_unnamed_addr #1 {
  %7 = icmp ugt i16 %1, 255
  %8 = icmp ugt i16 %2, 159
  %or.cond = or i1 %7, %8
  br i1 %or.cond, label %.loopexit, label %9

9:                                                ; preds = %6
  %10 = zext nneg i16 %2 to i32
  %11 = zext nneg i16 %1 to i32
  %12 = zext i16 %3 to i32
  %13 = add nuw nsw i32 %12, %11
  %14 = icmp ugt i32 %13, 256
  %15 = sub nsw i32 256, %13
  %16 = lshr i32 %15, 1
  %17 = trunc i32 %16 to i16
  %18 = lshr i16 %3, 1
  %.025 = select i1 %14, i16 %17, i16 %18
  %19 = zext i16 %4 to i32
  %20 = add nuw nsw i32 %19, %10
  %21 = icmp ugt i32 %20, 160
  %22 = trunc i32 %20 to i16
  %23 = sub i16 160, %22
  %.024 = select i1 %21, i16 %23, i16 %4
  %24 = tail call ptr @BMP_getWritePointer(i16 noundef zeroext %1, i16 noundef zeroext %2)
  %.not30 = icmp eq i16 %.024, 0
  br i1 %.not30, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %9, %.lr.ph
  %.033 = phi ptr [ %27, %.lr.ph ], [ %24, %9 ]
  %.02332 = phi ptr [ %26, %.lr.ph ], [ %0, %9 ]
  %.131 = phi i16 [ %25, %.lr.ph ], [ %.024, %9 ]
  %25 = add i16 %.131, -1
  tail call void @memcpy(ptr noundef %.033, ptr noundef %.02332, i16 noundef zeroext %.025) #3
  %26 = getelementptr inbounds i8, ptr %.02332, i64 %5
  %27 = getelementptr inbounds i8, ptr %.033, i64 128
  %.not = icmp eq i16 %25, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !5

.loopexit:                                        ; preds = %.lr.ph, %9, %6
  ret void
}

declare void @memcpy(ptr noundef, ptr noundef, i16 noundef zeroext) local_unnamed_addr #2

attributes #0 = { 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 = { 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 = { "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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass