; LLVM IR from JessonYue/LeetCodeLearning :: Array/cuijilin/Day20200706.ll (Z16reverseCharArrayPc)

; 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: mustprogress nounwind uwtable
define dso_local void @_Z16reverseCharArrayPc(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %6 = load ptr, ptr %2, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  br label %60

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

10:                                               ; preds = %18, %9
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !9
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds i8, ptr %11, i64 %13
  %15 = load i8, ptr %14, align 1, !tbaa !11
  %16 = sext i8 %15 to i32
  %17 = icmp ne i32 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %10
  %19 = load i32, ptr %3, align 4, !tbaa !9
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %3, align 4, !tbaa !9
  br label %10, !llvm.loop !12

21:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %56, %21
  %23 = load i32, ptr %4, align 4, !tbaa !9
  %24 = load i32, ptr %3, align 4, !tbaa !9
  %25 = sdiv i32 %24, 2
  %26 = icmp slt i32 %23, %25
  br i1 %26, label %28, label %27

27:                                               ; preds = %22
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %59

28:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %29 = load ptr, ptr %2, align 8, !tbaa !5
  %30 = load i32, ptr %4, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i8, ptr %29, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = sext i8 %33 to i32
  store i32 %34, ptr %5, align 4, !tbaa !9
  %35 = load ptr, ptr %2, align 8, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !9
  %37 = load i32, ptr %4, align 4, !tbaa !9
  %38 = sub nsw i32 %36, %37
  %39 = sub nsw i32 %38, 1
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i8, ptr %35, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = load ptr, ptr %2, align 8, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i8, ptr %43, i64 %45
  store i8 %42, ptr %46, align 1, !tbaa !11
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = trunc i32 %47 to i8
  %49 = load ptr, ptr %2, align 8, !tbaa !5
  %50 = load i32, ptr %3, align 4, !tbaa !9
  %51 = load i32, ptr %4, align 4, !tbaa !9
  %52 = sub nsw i32 %50, %51
  %53 = sub nsw i32 %52, 1
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds i8, ptr %49, i64 %54
  store i8 %48, ptr %55, align 1, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %56

56:                                               ; preds = %28
  %57 = load i32, ptr %4, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %4, align 4, !tbaa !9
  br label %22, !llvm.loop !14

59:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %60

60:                                               ; preds = %59, %8
  ret void
}

; 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

attributes #0 = { mustprogress 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 }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = distinct !{!14, !13}


; 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: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z16reverseCharArrayPc(ptr noundef %0) local_unnamed_addr #0 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %.loopexit, label %.preheader19

.preheader19:                                     ; preds = %1, %.preheader19
  %indvars.iv26 = phi i32 [ %indvars.iv.next27, %.preheader19 ], [ 0, %1 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %.preheader19 ], [ 0, %1 ]
  %3 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv
  %4 = load i8, ptr %3, align 1, !tbaa !5
  %.not = icmp eq i8 %4, 0
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %indvars.iv.next27 = add nuw i32 %indvars.iv26, 1
  br i1 %.not, label %.preheader, label %.preheader19, !llvm.loop !8

.preheader:                                       ; preds = %.preheader19
  %.not21 = icmp ult i64 %indvars.iv, 2
  br i1 %.not21, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %sext = shl i64 %indvars.iv, 32
  %5 = ashr exact i64 %sext, 32
  %6 = lshr i32 %indvars.iv26, 1
  %umax = tail call i32 @llvm.umax.i32(i32 %6, i32 1)
  %wide.trip.count = zext nneg i32 %umax to i64
  %7 = getelementptr i8, ptr %0, i64 %5
  %xtraiter = and i64 %wide.trip.count, 1
  %8 = icmp ult i32 %indvars.iv26, 4
  br i1 %8, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 2147483646
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %indvars.iv23 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next24.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %9 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv23
  %10 = load i8, ptr %9, align 1, !tbaa !5
  %11 = xor i64 %indvars.iv23, -1
  %12 = getelementptr i8, ptr %7, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !5
  store i8 %13, ptr %9, align 1, !tbaa !5
  store i8 %10, ptr %12, align 1, !tbaa !5
  %indvars.iv.next24 = or disjoint i64 %indvars.iv23, 1
  %14 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv.next24
  %15 = load i8, ptr %14, align 1, !tbaa !5
  %16 = xor i64 %indvars.iv23, -2
  %17 = getelementptr i8, ptr %7, i64 %16
  %18 = load i8, ptr %17, align 1, !tbaa !5
  store i8 %18, ptr %14, align 1, !tbaa !5
  store i8 %15, ptr %17, align 1, !tbaa !5
  %indvars.iv.next24.1 = add nuw nsw i64 %indvars.iv23, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !10

.loopexit.loopexit.unr-lcssa:                     ; preds = %.lr.ph, %.lr.ph.preheader
  %indvars.iv23.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next24.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa
  %19 = getelementptr inbounds i8, ptr %0, i64 %indvars.iv23.unr
  %20 = load i8, ptr %19, align 1, !tbaa !5
  %21 = xor i64 %indvars.iv23.unr, -1
  %22 = getelementptr i8, ptr %7, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !5
  store i8 %23, ptr %19, align 1, !tbaa !5
  store i8 %20, ptr %22, align 1, !tbaa !5
  br label %.loopexit

.loopexit:                                        ; preds = %.lr.ph.epil, %.loopexit.loopexit.unr-lcssa, %.preheader, %1
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.umax.i32(i32, i32) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) 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 speculatable willreturn memory(none) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = distinct !{!10, !9}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass