; LLVM IR from ravireddy07/cpp-book :: src/Hacker-Rank (structuring-the-document)

; 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.document = type { ptr, i32 }
%struct.paragraph = type { ptr, i32 }
%struct.sentence = type { ptr, i32 }
%struct.word = type { ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @add_char(ptr noundef, i8 noundef signext) #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_word(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_sentence(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_paragraph(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local { ptr, i32 } @get_document(ptr noundef %0) #0 {
  %2 = alloca %struct.document, align 8
  %3 = alloca ptr, align 8
  %4 = alloca %struct.paragraph, align 8
  %5 = alloca %struct.sentence, align 8
  %6 = alloca %struct.word, align 8
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %4) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %8 = getelementptr inbounds %struct.document, ptr %2, i32 0, i32 0
  store ptr null, ptr %8, align 8, !tbaa !9
  %9 = getelementptr inbounds %struct.paragraph, ptr %4, i32 0, i32 0
  store ptr null, ptr %9, align 8, !tbaa !12
  %10 = getelementptr inbounds %struct.sentence, ptr %5, i32 0, i32 0
  store ptr null, ptr %10, align 8, !tbaa !14
  %11 = getelementptr inbounds %struct.word, ptr %6, i32 0, i32 0
  store ptr null, ptr %11, align 8, !tbaa !16
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !18
  br label %12

12:                                               ; preds = %36, %1
  %13 = load i32, ptr %7, align 4, !tbaa !18
  %14 = zext i32 %13 to i64
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = call i64 @strlen(ptr noundef %15) #4
  %17 = icmp ule i64 %14, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  br label %39

19:                                               ; preds = %12
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = load i32, ptr %7, align 4, !tbaa !18
  %22 = zext i32 %21 to i64
  %23 = getelementptr inbounds i8, ptr %20, i64 %22
  %24 = load i8, ptr %23, align 1, !tbaa !19
  %25 = sext i8 %24 to i32
  switch i32 %25, label %29 [
    i32 32, label %26
    i32 46, label %27
    i32 10, label %28
    i32 0, label %28
  ]

26:                                               ; preds = %19
  call void @add_word(ptr noundef %5, ptr noundef %6)
  br label %35

27:                                               ; preds = %19
  call void @add_word(ptr noundef %5, ptr noundef %6)
  call void @add_sentence(ptr noundef %4, ptr noundef %5)
  br label %35

28:                                               ; preds = %19, %19
  call void @add_paragraph(ptr noundef %2, ptr noundef %4)
  br label %35

29:                                               ; preds = %19
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !18
  %32 = zext i32 %31 to i64
  %33 = getelementptr inbounds i8, ptr %30, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !19
  call void @add_char(ptr noundef %6, i8 noundef signext %34)
  br label %35

35:                                               ; preds = %29, %28, %27, %26
  br label %36

36:                                               ; preds = %35
  %37 = load i32, ptr %7, align 4, !tbaa !18
  %38 = add i32 %37, 1
  store i32 %38, ptr %7, align 4, !tbaa !18
  br label %12, !llvm.loop !20

39:                                               ; preds = %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %4) #3
  %40 = load { ptr, i32 }, ptr %2, align 8
  ret { ptr, i32 } %40
}

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

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr 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 willreturn memory(read) "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 willreturn memory(read) }

!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, !6, i64 0}
!10 = !{!"document", !6, i64 0, !11, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !6, i64 0}
!13 = !{!"paragraph", !6, i64 0, !11, i64 8}
!14 = !{!15, !6, i64 0}
!15 = !{!"sentence", !6, i64 0, !11, i64 8}
!16 = !{!17, !6, i64 0}
!17 = !{!"word", !6, i64 0}
!18 = !{!11, !11, i64 0}
!19 = !{!7, !7, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"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"

%struct.document = type { ptr, i32 }
%struct.paragraph = type { ptr, i32 }
%struct.sentence = type { ptr, i32 }
%struct.word = type { ptr }

; Function Attrs: nounwind uwtable
declare dso_local void @add_char(ptr noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_word(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_sentence(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @add_paragraph(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local { ptr, i32 } @get_document(ptr nocapture noundef readonly %0) local_unnamed_addr #0 {
  %2 = alloca %struct.document, align 8
  %3 = alloca %struct.paragraph, align 8
  %4 = alloca %struct.sentence, align 8
  %5 = alloca %struct.word, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #3
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %5) #3
  store ptr null, ptr %2, align 8, !tbaa !5
  store ptr null, ptr %3, align 8, !tbaa !11
  store ptr null, ptr %4, align 8, !tbaa !13
  store ptr null, ptr %5, align 8, !tbaa !15
  br label %7

6:                                                ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  %.fca.0.load = load ptr, ptr %2, align 8
  %.fca.0.insert = insertvalue { ptr, i32 } poison, ptr %.fca.0.load, 0
  %.fca.1.gep = getelementptr inbounds i8, ptr %2, i64 8
  %.fca.1.load = load i32, ptr %.fca.1.gep, align 8
  %.fca.1.insert = insertvalue { ptr, i32 } %.fca.0.insert, i32 %.fca.1.load, 1
  ret { ptr, i32 } %.fca.1.insert

7:                                                ; preds = %1, %15
  %8 = phi i64 [ 0, %1 ], [ %17, %15 ]
  %.07 = phi i32 [ 0, %1 ], [ %16, %15 ]
  %9 = getelementptr inbounds i8, ptr %0, i64 %8
  %10 = load i8, ptr %9, align 1, !tbaa !17
  switch i8 %10, label %14 [
    i8 32, label %11
    i8 46, label %12
    i8 10, label %13
    i8 0, label %13
  ]

11:                                               ; preds = %7
  call void @add_word(ptr noundef nonnull %4, ptr noundef nonnull %5)
  br label %15

12:                                               ; preds = %7
  call void @add_word(ptr noundef nonnull %4, ptr noundef nonnull %5)
  call void @add_sentence(ptr noundef nonnull %3, ptr noundef nonnull %4)
  br label %15

13:                                               ; preds = %7, %7
  call void @add_paragraph(ptr noundef nonnull %2, ptr noundef nonnull %3)
  br label %15

14:                                               ; preds = %7
  call void @add_char(ptr noundef nonnull %5, i8 noundef signext %10)
  br label %15

15:                                               ; preds = %11, %12, %13, %14
  %16 = add i32 %.07, 1
  %17 = zext i32 %16 to i64
  %18 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #4
  %.not = icmp ult i64 %18, %17
  br i1 %.not, label %6, label %7, !llvm.loop !18
}

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: mustprogress 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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 willreturn memory(read) }

!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 = !{!"document", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !7, i64 0}
!12 = !{!"paragraph", !7, i64 0, !10, i64 8}
!13 = !{!14, !7, i64 0}
!14 = !{!"sentence", !7, i64 0, !10, i64 8}
!15 = !{!16, !7, i64 0}
!16 = !{!"word", !7, i64 0}
!17 = !{!8, !8, i64 0}
!18 = distinct !{!18, !19}
!19 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass