; LLVM IR from yettabaa/cub3D :: mlx-linux (mlx_xpm)

; 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.s_img = type { ptr, i64, ptr, i32, i32, i32, i32, i32, i32, ptr, %struct.XShmSegmentInfo }
%struct.XShmSegmentInfo = type { i64, i32, ptr, i32 }
%struct._XImage = type { i32, i32, i32, i32, ptr, i32, i32, i32, i32, i32, i32, i32, i64, i64, i64, ptr, %struct.funcs }
%struct.funcs = type { ptr, ptr, ptr, ptr, ptr, ptr }

; 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 i32 @mlx_int_xpm_set_pixel(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %13 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %13, ptr %12, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %56, %5
  %15 = load i32, ptr %12, align 4, !tbaa !9
  %16 = add nsw i32 %15, -1
  store i32 %16, ptr %12, align 4, !tbaa !9
  %17 = icmp ne i32 %15, 0
  br i1 %17, label %18, label %59

18:                                               ; preds = %14
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = getelementptr inbounds %struct.s_img, ptr %19, i32 0, i32 0
  %21 = load ptr, ptr %20, align 8, !tbaa !11
  %22 = getelementptr inbounds %struct._XImage, ptr %21, i32 0, i32 5
  %23 = load i32, ptr %22, align 8, !tbaa !15
  %24 = icmp ne i32 %23, 0
  br i1 %24, label %25, label %38

25:                                               ; preds = %18
  %26 = load i32, ptr %10, align 4, !tbaa !9
  %27 = and i32 %26, 255
  %28 = trunc i32 %27 to i8
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = mul nsw i32 %30, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i8, ptr %29, i64 %33
  %35 = load i32, ptr %12, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i8, ptr %34, i64 %36
  store i8 %28, ptr %37, align 1, !tbaa !18
  br label %56

38:                                               ; preds = %18
  %39 = load i32, ptr %10, align 4, !tbaa !9
  %40 = and i32 %39, 255
  %41 = trunc i32 %40 to i8
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = load i32, ptr %11, align 4, !tbaa !9
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = mul nsw i32 %43, %44
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds i8, ptr %42, i64 %46
  %48 = load i32, ptr %9, align 4, !tbaa !9
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i8, ptr %47, i64 %49
  %51 = load i32, ptr %12, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = sub i64 0, %52
  %54 = getelementptr inbounds i8, ptr %50, i64 %53
  %55 = getelementptr inbounds i8, ptr %54, i64 -1
  store i8 %41, ptr %55, align 1, !tbaa !18
  br label %56

56:                                               ; preds = %38, %25
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = ashr i32 %57, 8
  store i32 %58, ptr %10, align 4, !tbaa !9
  br label %14, !llvm.loop !19

59:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  %60 = load i32, ptr %6, align 4
  ret i32 %60
}

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 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"s_img", !6, i64 0, !13, i64 8, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !6, i64 48, !14, i64 56}
!13 = !{!"long", !7, i64 0}
!14 = !{!"", !13, i64 0, !10, i64 8, !6, i64 16, !10, i64 24}
!15 = !{!16, !10, i64 24}
!16 = !{!"_XImage", !10, i64 0, !10, i64 4, !10, i64 8, !10, i64 12, !6, i64 16, !10, i64 24, !10, i64 28, !10, i64 32, !10, i64 36, !10, i64 40, !10, i64 44, !10, i64 48, !13, i64 56, !13, i64 64, !13, i64 72, !6, i64 80, !17, i64 88}
!17 = !{!"funcs", !6, i64 0, !6, i64 8, !6, i64 16, !6, i64 24, !6, i64 32, !6, i64 40}
!18 = !{!7, !7, i64 0}
!19 = distinct !{!19, !20}
!20 = !{!"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: nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local i32 @mlx_int_xpm_set_pixel(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %.not17 = icmp eq i32 %2, 0
  br i1 %.not17, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %5
  %6 = mul nsw i32 %4, %2
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds i8, ptr %1, i64 %7
  %9 = sext i32 %2 to i64
  %10 = getelementptr inbounds i8, ptr %8, i64 %9
  %invariant.gep = getelementptr i8, ptr %10, i64 -1
  %xtraiter = and i64 %9, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.prol.loopexit, label %.prol.loopexit.unr-lcssa

.prol.loopexit.unr-lcssa:                         ; preds = %.lr.ph
  %indvars.iv.next.prol = add nsw i64 %9, -1
  %11 = load ptr, ptr %0, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 24
  %13 = load i32, ptr %12, align 8, !tbaa !13
  %.not16.prol = icmp eq i32 %13, 0
  %14 = trunc i32 %3 to i8
  %15 = sub nsw i64 1, %9
  %gep.prol = getelementptr i8, ptr %invariant.gep, i64 %15
  %16 = getelementptr inbounds i8, ptr %8, i64 %indvars.iv.next.prol
  %gep.sink.prol = select i1 %.not16.prol, ptr %gep.prol, ptr %16
  store i8 %14, ptr %gep.sink.prol, align 1, !tbaa !16
  %17 = ashr i32 %3, 8
  br label %.prol.loopexit

.prol.loopexit:                                   ; preds = %.prol.loopexit.unr-lcssa, %.lr.ph
  %indvars.iv.unr = phi i64 [ %9, %.lr.ph ], [ %indvars.iv.next.prol, %.prol.loopexit.unr-lcssa ]
  %.01518.unr = phi i32 [ %3, %.lr.ph ], [ %17, %.prol.loopexit.unr-lcssa ]
  %18 = icmp eq i32 %2, 1
  br i1 %18, label %._crit_edge, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.prol.loopexit
  %invariant.gep20 = getelementptr i8, ptr %8, i64 -1
  br label %19

19:                                               ; preds = %19, %.lr.ph.new
  %indvars.iv = phi i64 [ %indvars.iv.unr, %.lr.ph.new ], [ %indvars.iv.next.1, %19 ]
  %.01518 = phi i32 [ %.01518.unr, %.lr.ph.new ], [ %32, %19 ]
  %20 = load ptr, ptr %0, align 8, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %20, i64 24
  %22 = load i32, ptr %21, align 8, !tbaa !13
  %.not16 = icmp eq i32 %22, 0
  %23 = trunc i32 %.01518 to i8
  %24 = sub nsw i64 1, %indvars.iv
  %gep = getelementptr i8, ptr %invariant.gep, i64 %24
  %gep21 = getelementptr i8, ptr %invariant.gep20, i64 %indvars.iv
  %gep.sink = select i1 %.not16, ptr %gep, ptr %gep21
  store i8 %23, ptr %gep.sink, align 1, !tbaa !16
  %25 = lshr i32 %.01518, 8
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %26 = load ptr, ptr %0, align 8, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %26, i64 24
  %28 = load i32, ptr %27, align 8, !tbaa !13
  %.not16.1 = icmp eq i32 %28, 0
  %29 = trunc i32 %25 to i8
  %30 = sub nsw i64 2, %indvars.iv
  %gep.1 = getelementptr i8, ptr %invariant.gep, i64 %30
  %31 = getelementptr inbounds i8, ptr %8, i64 %indvars.iv.next.1
  %gep.sink.1 = select i1 %.not16.1, ptr %gep.1, ptr %31
  store i8 %29, ptr %gep.sink.1, align 1, !tbaa !16
  %32 = ashr i32 %.01518, 16
  %.not.1 = icmp eq i64 %indvars.iv.next.1, 0
  br i1 %.not.1, label %._crit_edge, label %19, !llvm.loop !17

._crit_edge:                                      ; preds = %.prol.loopexit, %19, %5
  ret i32 undef
}

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, !7, i64 0}
!6 = !{!"s_img", !7, i64 0, !10, i64 8, !7, i64 16, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !7, i64 48, !12, i64 56}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"long", !8, i64 0}
!11 = !{!"int", !8, i64 0}
!12 = !{!"", !10, i64 0, !11, i64 8, !7, i64 16, !11, i64 24}
!13 = !{!14, !11, i64 24}
!14 = !{!"_XImage", !11, i64 0, !11, i64 4, !11, i64 8, !11, i64 12, !7, i64 16, !11, i64 24, !11, i64 28, !11, i64 32, !11, i64 36, !11, i64 40, !11, i64 44, !11, i64 48, !10, i64 56, !10, i64 64, !10, i64 72, !7, i64 80, !15, i64 88}
!15 = !{!"funcs", !7, i64 0, !7, i64 8, !7, i64 16, !7, i64 24, !7, i64 32, !7, i64 40}
!16 = !{!8, !8, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass