; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (832)

; 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: nounwind uwtable
define dso_local ptr @flipAndInvertImage(ptr noundef %0, i32 noundef %1, ptr noundef %2, ptr noundef %3, ptr noundef %4) #0 {
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !5
  store ptr %3, ptr %9, align 8, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %17 = load ptr, ptr %8, align 8, !tbaa !5
  %18 = load i32, ptr %17, align 4, !tbaa !9
  store i32 %18, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %19 = load i32, ptr %7, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = mul i64 8, %20
  %22 = call noalias ptr @malloc(i64 noundef %21) #4
  store ptr %22, ptr %12, align 8, !tbaa !5
  %23 = load ptr, ptr %12, align 8, !tbaa !5
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %44

25:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !9
  br label %26

26:                                               ; preds = %40, %25
  %27 = load i32, ptr %13, align 4, !tbaa !9
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp slt i32 %27, %28
  br i1 %29, label %31, label %30

30:                                               ; preds = %26
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %43

31:                                               ; preds = %26
  %32 = load i32, ptr %11, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = mul i64 4, %33
  %35 = call noalias ptr @malloc(i64 noundef %34) #4
  %36 = load ptr, ptr %12, align 8, !tbaa !5
  %37 = load i32, ptr %13, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds ptr, ptr %36, i64 %38
  store ptr %35, ptr %39, align 8, !tbaa !5
  br label %40

40:                                               ; preds = %31
  %41 = load i32, ptr %13, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %13, align 4, !tbaa !9
  br label %26, !llvm.loop !11

43:                                               ; preds = %30
  br label %44

44:                                               ; preds = %43, %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %83, %44
  %46 = load i32, ptr %14, align 4, !tbaa !9
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %50, label %49

49:                                               ; preds = %45
  store i32 5, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %86

50:                                               ; preds = %45
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  %51 = load i32, ptr %11, align 4, !tbaa !9
  %52 = sub nsw i32 %51, 1
  store i32 %52, ptr %16, align 4, !tbaa !9
  br label %53

53:                                               ; preds = %79, %50
  %54 = load i32, ptr %16, align 4, !tbaa !9
  %55 = icmp sge i32 %54, 0
  br i1 %55, label %57, label %56

56:                                               ; preds = %53
  store i32 8, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  br label %82

57:                                               ; preds = %53
  %58 = load ptr, ptr %6, align 8, !tbaa !5
  %59 = load i32, ptr %14, align 4, !tbaa !9
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds ptr, ptr %58, i64 %60
  %62 = load ptr, ptr %61, align 8, !tbaa !5
  %63 = load i32, ptr %16, align 4, !tbaa !9
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds i32, ptr %62, i64 %64
  %66 = load i32, ptr %65, align 4, !tbaa !9
  %67 = xor i32 %66, 1
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  %69 = load i32, ptr %14, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds ptr, ptr %68, i64 %70
  %72 = load ptr, ptr %71, align 8, !tbaa !5
  %73 = load i32, ptr %11, align 4, !tbaa !9
  %74 = sub nsw i32 %73, 1
  %75 = load i32, ptr %16, align 4, !tbaa !9
  %76 = sub nsw i32 %74, %75
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds i32, ptr %72, i64 %77
  store i32 %67, ptr %78, align 4, !tbaa !9
  br label %79

79:                                               ; preds = %57
  %80 = load i32, ptr %16, align 4, !tbaa !9
  %81 = add nsw i32 %80, -1
  store i32 %81, ptr %16, align 4, !tbaa !9
  br label %53, !llvm.loop !13

82:                                               ; preds = %56
  br label %83

83:                                               ; preds = %82
  %84 = load i32, ptr %14, align 4, !tbaa !9
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %14, align 4, !tbaa !9
  br label %45, !llvm.loop !14

86:                                               ; preds = %49
  %87 = load i32, ptr %7, align 4, !tbaa !9
  %88 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 %87, ptr %88, align 4, !tbaa !9
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %89, ptr %90, align 8, !tbaa !5
  %91 = load ptr, ptr %12, align 8, !tbaa !5
  store i32 1, ptr %15, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  ret ptr %91
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nounwind allocsize(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 }
attributes #4 = { nounwind allocsize(0) }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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 nounwind uwtable
define dso_local noalias noundef ptr @flipAndInvertImage(ptr nocapture noundef readonly %0, i32 noundef %1, ptr noundef %2, ptr nocapture noundef writeonly %3, ptr nocapture noundef writeonly %4) local_unnamed_addr #0 {
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = sext i32 %1 to i64
  %8 = shl nsw i64 %7, 3
  %9 = tail call noalias ptr @malloc(i64 noundef %8) #2
  %.not = icmp ne ptr %9, null
  %10 = icmp sgt i32 %1, 0
  %or.cond = and i1 %.not, %10
  br i1 %or.cond, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %5
  %11 = sext i32 %6 to i64
  %12 = shl nsw i64 %11, 2
  %wide.trip.count = zext nneg i32 %1 to i64
  br label %13

13:                                               ; preds = %.lr.ph, %13
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %13 ]
  %14 = tail call noalias ptr @malloc(i64 noundef %12) #2
  %15 = getelementptr inbounds ptr, ptr %9, i64 %indvars.iv
  store ptr %14, ptr %15, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit, label %13, !llvm.loop !11

.loopexit:                                        ; preds = %13, %5
  %16 = icmp sgt i32 %6, 0
  %or.cond47 = select i1 %10, i1 %16, i1 false
  br i1 %or.cond47, label %.lr.ph32.us.preheader, label %._crit_edge36

.lr.ph32.us.preheader:                            ; preds = %.loopexit
  %17 = add nsw i32 %6, -1
  %18 = zext nneg i32 %6 to i64
  %19 = zext nneg i32 %17 to i64
  %wide.trip.count45 = zext nneg i32 %1 to i64
  %20 = shl nuw nsw i64 %19, 2
  %21 = add nuw nsw i64 %20, 4
  %22 = shl nuw nsw i64 %18, 2
  %23 = sub nsw i64 %21, %22
  %min.iters.check = icmp ult i32 %6, 8
  %n.vec = and i64 %18, 2147483640
  %ind.end = and i64 %18, 7
  %cmp.n = icmp eq i64 %n.vec, %18
  br label %.lr.ph32.us

.lr.ph32.us:                                      ; preds = %.lr.ph32.us.preheader, %._crit_edge.us
  %indvars.iv42 = phi i64 [ 0, %.lr.ph32.us.preheader ], [ %indvars.iv.next43, %._crit_edge.us ]
  %24 = getelementptr inbounds ptr, ptr %0, i64 %indvars.iv42
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %26 = getelementptr inbounds ptr, ptr %9, i64 %indvars.iv42
  %27 = load ptr, ptr %26, align 8, !tbaa !9
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %.lr.ph32.us
  %scevgep = getelementptr i8, ptr %27, i64 %23
  %28 = getelementptr i8, ptr %27, i64 %20
  %scevgep48 = getelementptr i8, ptr %28, i64 4
  %scevgep49 = getelementptr i8, ptr %25, i64 %22
  %bound0 = icmp ult ptr %scevgep, %scevgep49
  %bound1 = icmp ult ptr %25, %scevgep48
  %found.conflict = and i1 %bound0, %bound1
  br i1 %found.conflict, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %vector.memcheck, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %vector.memcheck ]
  %29 = xor i64 %index, -1
  %30 = add i64 %29, %18
  %31 = getelementptr inbounds i32, ptr %25, i64 %30
  %32 = getelementptr inbounds i8, ptr %31, i64 -12
  %33 = getelementptr inbounds i8, ptr %31, i64 -28
  %wide.load = load <4 x i32>, ptr %32, align 4, !tbaa !5, !alias.scope !13
  %wide.load50 = load <4 x i32>, ptr %33, align 4, !tbaa !5, !alias.scope !13
  %34 = xor <4 x i32> %wide.load, <i32 1, i32 1, i32 1, i32 1>
  %35 = shufflevector <4 x i32> %34, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %36 = xor <4 x i32> %wide.load50, <i32 1, i32 1, i32 1, i32 1>
  %37 = shufflevector <4 x i32> %36, <4 x i32> poison, <4 x i32> <i32 3, i32 2, i32 1, i32 0>
  %38 = sub nuw nsw i64 %19, %30
  %39 = getelementptr inbounds i32, ptr %27, i64 %38
  %40 = getelementptr inbounds i8, ptr %39, i64 16
  store <4 x i32> %35, ptr %39, align 4, !tbaa !5, !alias.scope !16, !noalias !13
  store <4 x i32> %37, ptr %40, align 4, !tbaa !5, !alias.scope !16, !noalias !13
  %index.next = add nuw i64 %index, 8
  %41 = icmp eq i64 %index.next, %n.vec
  br i1 %41, label %middle.block, label %vector.body, !llvm.loop !18

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %.lr.ph32.us
  %indvars.iv39.in.ph = phi i64 [ %18, %vector.memcheck ], [ %18, %.lr.ph32.us ], [ %ind.end, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv39.in = phi i64 [ %indvars.iv39, %scalar.ph ], [ %indvars.iv39.in.ph, %scalar.ph.preheader ]
  %indvars.iv39 = add nsw i64 %indvars.iv39.in, -1
  %42 = getelementptr inbounds i32, ptr %25, i64 %indvars.iv39
  %43 = load i32, ptr %42, align 4, !tbaa !5
  %44 = xor i32 %43, 1
  %45 = sub nuw nsw i64 %19, %indvars.iv39
  %46 = getelementptr inbounds i32, ptr %27, i64 %45
  store i32 %44, ptr %46, align 4, !tbaa !5
  %47 = icmp sgt i64 %indvars.iv39.in, 1
  br i1 %47, label %scalar.ph, label %._crit_edge.us, !llvm.loop !21

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %indvars.iv.next43 = add nuw nsw i64 %indvars.iv42, 1
  %exitcond46.not = icmp eq i64 %indvars.iv.next43, %wide.trip.count45
  br i1 %exitcond46.not, label %._crit_edge36, label %.lr.ph32.us, !llvm.loop !22

._crit_edge36:                                    ; preds = %._crit_edge.us, %.loopexit
  store i32 %1, ptr %3, align 4, !tbaa !5
  store ptr %2, ptr %4, align 8, !tbaa !9
  ret ptr %9
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

attributes #0 = { nofree 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 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }

!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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14}
!14 = distinct !{!14, !15}
!15 = distinct !{!15, !"LVerDomain"}
!16 = !{!17}
!17 = distinct !{!17, !15}
!18 = distinct !{!18, !12, !19, !20}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !12, !19}
!22 = distinct !{!22, !12}


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