; LLVM IR from MostafaTwfiq/C-DataStructures-And-Algorithms :: Algorithms/ArraysAlg/Sources (CharArrayAlg)

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

@stderr = external global ptr, align 8
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #1

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

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

; Function Attrs: nounwind uwtable
define dso_local void @charArrayReverseWords(ptr noundef %0) #3 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %11, label %14

11:                                               ; preds = %1
  %12 = load ptr, ptr @stderr, align 8, !tbaa !5
  %13 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef @.str, ptr noundef @.str.7, ptr noundef @.str.8) #5
  call void @exit(i32 noundef -83386157) #6
  unreachable

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #5
  %15 = call ptr @vectorInitialization(i32 noundef 5, ptr noundef @destroyString, ptr noundef null)
  store ptr %15, ptr %3, align 8, !tbaa !5
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = call ptr @stringInitialization(i32 noundef 5)
  call void @vectorAdd(ptr noundef %16, ptr noundef %17)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %18 = load ptr, ptr %2, align 8, !tbaa !5
  store ptr %18, ptr %4, align 8, !tbaa !5
  br label %19

19:                                               ; preds = %59, %14
  %20 = load ptr, ptr %2, align 8, !tbaa !5
  %21 = load i8, ptr %20, align 1, !tbaa !9
  %22 = sext i8 %21 to i32
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %62

24:                                               ; preds = %19
  %25 = load ptr, ptr %2, align 8, !tbaa !5
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp eq i32 %27, 32
  br i1 %28, label %29, label %51

29:                                               ; preds = %24
  br label %30

30:                                               ; preds = %36, %29
  %31 = load ptr, ptr %2, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 1
  %33 = load i8, ptr %32, align 1, !tbaa !9
  %34 = sext i8 %33 to i32
  %35 = icmp eq i32 %34, 32
  br i1 %35, label %36, label %39

36:                                               ; preds = %30
  %37 = load ptr, ptr %2, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i32 1
  store ptr %38, ptr %2, align 8, !tbaa !5
  br label %30, !llvm.loop !10

39:                                               ; preds = %30
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = call i32 @vectorGetLength(ptr noundef %41)
  %43 = sub nsw i32 %42, 1
  %44 = call ptr @vectorGet(ptr noundef %40, i32 noundef %43)
  %45 = call i32 @stringGetLength(ptr noundef %44)
  %46 = icmp ne i32 %45, 0
  br i1 %46, label %47, label %50

47:                                               ; preds = %39
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = call ptr @stringInitialization(i32 noundef 5)
  call void @vectorAdd(ptr noundef %48, ptr noundef %49)
  br label %50

50:                                               ; preds = %47, %39
  br label %59

51:                                               ; preds = %24
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = load ptr, ptr %3, align 8, !tbaa !5
  %54 = call i32 @vectorGetLength(ptr noundef %53)
  %55 = sub nsw i32 %54, 1
  %56 = call ptr @vectorGet(ptr noundef %52, i32 noundef %55)
  %57 = load ptr, ptr %2, align 8, !tbaa !5
  %58 = load i8, ptr %57, align 1, !tbaa !9
  call void @stringAppendChar(ptr noundef %56, i8 noundef signext %58)
  br label %59

59:                                               ; preds = %51, %50
  %60 = load ptr, ptr %2, align 8, !tbaa !5
  %61 = getelementptr inbounds i8, ptr %60, i32 1
  store ptr %61, ptr %2, align 8, !tbaa !5
  br label %19, !llvm.loop !12

62:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %63 = load ptr, ptr %3, align 8, !tbaa !5
  %64 = call i32 @vectorGetLength(ptr noundef %63)
  %65 = sub nsw i32 %64, 1
  store i32 %65, ptr %5, align 4, !tbaa !13
  br label %66

66:                                               ; preds = %102, %62
  %67 = load i32, ptr %5, align 4, !tbaa !13
  %68 = icmp sge i32 %67, 0
  br i1 %68, label %70, label %69

69:                                               ; preds = %66
  store i32 6, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  br label %105

70:                                               ; preds = %66
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %71 = load ptr, ptr %3, align 8, !tbaa !5
  %72 = load i32, ptr %5, align 4, !tbaa !13
  %73 = call ptr @vectorGet(ptr noundef %71, i32 noundef %72)
  store ptr %73, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !13
  br label %74

74:                                               ; preds = %86, %70
  %75 = load i32, ptr %8, align 4, !tbaa !13
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = call i32 @stringGetLength(ptr noundef %76)
  %78 = icmp slt i32 %75, %77
  br i1 %78, label %80, label %79

79:                                               ; preds = %74
  store i32 9, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %89

80:                                               ; preds = %74
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = load i32, ptr %8, align 4, !tbaa !13
  %83 = call signext i8 @stringGet(ptr noundef %81, i32 noundef %82)
  %84 = load ptr, ptr %4, align 8, !tbaa !5
  %85 = getelementptr inbounds i8, ptr %84, i32 1
  store ptr %85, ptr %4, align 8, !tbaa !5
  store i8 %83, ptr %84, align 1, !tbaa !9
  br label %86

86:                                               ; preds = %80
  %87 = load i32, ptr %8, align 4, !tbaa !13
  %88 = add nsw i32 %87, 1
  store i32 %88, ptr %8, align 4, !tbaa !13
  br label %74, !llvm.loop !15

89:                                               ; preds = %79
  %90 = load i32, ptr %5, align 4, !tbaa !13
  %91 = icmp ne i32 %90, 0
  br i1 %91, label %92, label %99

92:                                               ; preds = %89
  %93 = load ptr, ptr %7, align 8, !tbaa !5
  %94 = call i32 @stringGetLength(ptr noundef %93)
  %95 = icmp ne i32 %94, 0
  br i1 %95, label %96, label %99

96:                                               ; preds = %92
  %97 = load ptr, ptr %4, align 8, !tbaa !5
  %98 = getelementptr inbounds i8, ptr %97, i32 1
  store ptr %98, ptr %4, align 8, !tbaa !5
  store i8 32, ptr %97, align 1, !tbaa !9
  br label %101

99:                                               ; preds = %92, %89
  %100 = load ptr, ptr %4, align 8, !tbaa !5
  store i8 0, ptr %100, align 1, !tbaa !9
  br label %101

101:                                              ; preds = %99, %96
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  br label %102

102:                                              ; preds = %101
  %103 = load i32, ptr %5, align 4, !tbaa !13
  %104 = add nsw i32 %103, -1
  store i32 %104, ptr %5, align 4, !tbaa !13
  br label %66, !llvm.loop !16

105:                                              ; preds = %69
  %106 = load ptr, ptr %3, align 8, !tbaa !5
  call void @destroyVector(ptr noundef %106)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #5
  ret void
}

declare ptr @vectorInitialization(i32 noundef, ptr noundef, ptr noundef) #4

declare void @destroyString(ptr noundef) #4

declare void @vectorAdd(ptr noundef, ptr noundef) #4

declare ptr @stringInitialization(i32 noundef) #4

declare i32 @stringGetLength(ptr noundef) #4

declare ptr @vectorGet(ptr noundef, i32 noundef) #4

declare i32 @vectorGetLength(ptr noundef) #4

declare void @stringAppendChar(ptr noundef, i8 noundef signext) #4

declare signext i8 @stringGet(ptr noundef, i32 noundef) #4

declare void @destroyVector(ptr noundef) #4

attributes #0 = { nounwind "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 = { noreturn nounwind "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = { "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 #5 = { nounwind }
attributes #6 = { noreturn 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !11}
!16 = distinct !{!16, !11}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [31 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [11 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [25 x i8], align 1

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @charArrayReverseWords(ptr noundef %0) local_unnamed_addr #2 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %3, label %6

3:                                                ; preds = %1
  %4 = load ptr, ptr @stderr, align 8, !tbaa !5
  %5 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %4, ptr noundef nonnull @.str, ptr noundef nonnull @.str.7, ptr noundef nonnull @.str.8) #4
  tail call void @exit(i32 noundef -83386157) #5
  unreachable

6:                                                ; preds = %1
  %7 = tail call ptr @vectorInitialization(i32 noundef 5, ptr noundef nonnull @destroyString, ptr noundef null) #6
  %8 = tail call ptr @stringInitialization(i32 noundef 5) #6
  tail call void @vectorAdd(ptr noundef %7, ptr noundef %8) #6
  br label %9

9:                                                ; preds = %26, %6
  %.029 = phi ptr [ %0, %6 ], [ %27, %26 ]
  %10 = load i8, ptr %.029, align 1, !tbaa !9
  switch i8 %10, label %21 [
    i8 0, label %28
    i8 32, label %.preheader
  ]

.preheader:                                       ; preds = %9, %.preheader
  %.1 = phi ptr [ %11, %.preheader ], [ %.029, %9 ]
  %11 = getelementptr inbounds i8, ptr %.1, i64 1
  %12 = load i8, ptr %11, align 1, !tbaa !9
  %13 = icmp eq i8 %12, 32
  br i1 %13, label %.preheader, label %14, !llvm.loop !10

14:                                               ; preds = %.preheader
  %15 = tail call i32 @vectorGetLength(ptr noundef %7) #6
  %16 = add nsw i32 %15, -1
  %17 = tail call ptr @vectorGet(ptr noundef %7, i32 noundef %16) #6
  %18 = tail call i32 @stringGetLength(ptr noundef %17) #6
  %.not38 = icmp eq i32 %18, 0
  br i1 %.not38, label %26, label %19

19:                                               ; preds = %14
  %20 = tail call ptr @stringInitialization(i32 noundef 5) #6
  tail call void @vectorAdd(ptr noundef %7, ptr noundef %20) #6
  br label %26

21:                                               ; preds = %9
  %22 = tail call i32 @vectorGetLength(ptr noundef %7) #6
  %23 = add nsw i32 %22, -1
  %24 = tail call ptr @vectorGet(ptr noundef %7, i32 noundef %23) #6
  %25 = load i8, ptr %.029, align 1, !tbaa !9
  tail call void @stringAppendChar(ptr noundef %24, i8 noundef signext %25) #6
  br label %26

26:                                               ; preds = %14, %19, %21
  %.2 = phi ptr [ %.1, %19 ], [ %.1, %14 ], [ %.029, %21 ]
  %27 = getelementptr inbounds i8, ptr %.2, i64 1
  br label %9, !llvm.loop !12

28:                                               ; preds = %9
  %29 = tail call i32 @vectorGetLength(ptr noundef %7) #6
  %30 = icmp sgt i32 %29, 0
  br i1 %30, label %.lr.ph46, label %._crit_edge47

._crit_edge47:                                    ; preds = %39, %.thread, %28
  tail call void @destroyVector(ptr noundef %7) #6
  ret void

.lr.ph46:                                         ; preds = %28, %39
  %.03044.in = phi i32 [ %.03044, %39 ], [ %29, %28 ]
  %.03143 = phi ptr [ %spec.select39, %39 ], [ %0, %28 ]
  %.03044 = add nsw i32 %.03044.in, -1
  %31 = tail call ptr @vectorGet(ptr noundef %7, i32 noundef %.03044) #6
  %32 = tail call i32 @stringGetLength(ptr noundef %31) #6
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph46
  %.132.lcssa = phi ptr [ %.03143, %.lr.ph46 ], [ %35, %.lr.ph ]
  %.not36 = icmp eq i32 %.03044, 0
  br i1 %.not36, label %.thread, label %39

.thread:                                          ; preds = %._crit_edge
  store i8 0, ptr %.132.lcssa, align 1, !tbaa !9
  br label %._crit_edge47

.lr.ph:                                           ; preds = %.lr.ph46, %.lr.ph
  %.041 = phi i32 [ %36, %.lr.ph ], [ 0, %.lr.ph46 ]
  %.13240 = phi ptr [ %35, %.lr.ph ], [ %.03143, %.lr.ph46 ]
  %34 = tail call signext i8 @stringGet(ptr noundef %31, i32 noundef %.041) #6
  %35 = getelementptr inbounds i8, ptr %.13240, i64 1
  store i8 %34, ptr %.13240, align 1, !tbaa !9
  %36 = add nuw nsw i32 %.041, 1
  %37 = tail call i32 @stringGetLength(ptr noundef %31) #6
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %.lr.ph, label %._crit_edge, !llvm.loop !13

39:                                               ; preds = %._crit_edge
  %40 = tail call i32 @stringGetLength(ptr noundef %31) #6
  %.not37 = icmp ne i32 %40, 0
  %spec.select = select i1 %.not37, i8 32, i8 0
  %spec.select39.idx = zext i1 %.not37 to i64
  %spec.select39 = getelementptr inbounds i8, ptr %.132.lcssa, i64 %spec.select39.idx
  store i8 %spec.select, ptr %.132.lcssa, align 1, !tbaa !9
  %41 = icmp sgt i32 %.03044.in, 1
  br i1 %41, label %.lr.ph46, label %._crit_edge47, !llvm.loop !14
}

declare ptr @vectorInitialization(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #3

declare void @destroyString(ptr noundef) #3

declare void @vectorAdd(ptr noundef, ptr noundef) local_unnamed_addr #3

declare ptr @stringInitialization(i32 noundef) local_unnamed_addr #3

declare i32 @stringGetLength(ptr noundef) local_unnamed_addr #3

declare ptr @vectorGet(ptr noundef, i32 noundef) local_unnamed_addr #3

declare i32 @vectorGetLength(ptr noundef) local_unnamed_addr #3

declare void @stringAppendChar(ptr noundef, i8 noundef signext) local_unnamed_addr #3

declare signext i8 @stringGet(ptr noundef, i32 noundef) local_unnamed_addr #3

declare void @destroyVector(ptr noundef) local_unnamed_addr #3

attributes #0 = { nofree nounwind "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 = { noreturn nounwind "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 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 = { cold nounwind }
attributes #5 = { noreturn nounwind }
attributes #6 = { 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 = !{!7, !7, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}
!12 = distinct !{!12, !11}
!13 = distinct !{!13, !11}
!14 = distinct !{!14, !11}


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