; LLVM IR from syby119/CG-projects :: doc/c++ tutorial/1. new grammer/3. dynamic memory issue (main)

; 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.Student = type { [80 x i8], i32 }

@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [27 x i8], align 1

declare i32 @printf(ptr noundef, ...) #0

; 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: nounwind
declare void @free(ptr noundef) #3

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

; Function Attrs: nounwind uwtable
define dso_local void @example2() #4 {
  %1 = alloca ptr, align 8
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = call i32 (ptr, ...) @printf(ptr noundef @.str.2)
  call void @llvm.lifetime.start.p0(i64 8, ptr %1) #5
  %5 = call noalias ptr @malloc(i64 noundef 840) #6
  store ptr %5, ptr %1, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %6

6:                                                ; preds = %33, %0
  %7 = load i32, ptr %2, align 4, !tbaa !9
  %8 = icmp slt i32 %7, 10
  br i1 %8, label %10, label %9

9:                                                ; preds = %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  br label %36

10:                                               ; preds = %6
  %11 = load i32, ptr %2, align 4, !tbaa !9
  %12 = add nsw i32 65, %11
  %13 = trunc i32 %12 to i8
  %14 = load ptr, ptr %1, align 8, !tbaa !5
  %15 = load i32, ptr %2, align 4, !tbaa !9
  %16 = sext i32 %15 to i64
  %17 = getelementptr inbounds %struct.Student, ptr %14, i64 %16
  %18 = getelementptr inbounds %struct.Student, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds [80 x i8], ptr %18, i64 0, i64 0
  store i8 %13, ptr %19, align 4, !tbaa !11
  %20 = load ptr, ptr %1, align 8, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds %struct.Student, ptr %20, i64 %22
  %24 = getelementptr inbounds %struct.Student, ptr %23, i32 0, i32 0
  %25 = getelementptr inbounds [80 x i8], ptr %24, i64 0, i64 1
  store i8 0, ptr %25, align 1, !tbaa !11
  %26 = load i32, ptr %2, align 4, !tbaa !9
  %27 = add nsw i32 20, %26
  %28 = load ptr, ptr %1, align 8, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !9
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds %struct.Student, ptr %28, i64 %30
  %32 = getelementptr inbounds %struct.Student, ptr %31, i32 0, i32 1
  store i32 %27, ptr %32, align 4, !tbaa !12
  br label %33

33:                                               ; preds = %10
  %34 = load i32, ptr %2, align 4, !tbaa !9
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %2, align 4, !tbaa !9
  br label %6, !llvm.loop !14

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

37:                                               ; preds = %56, %36
  %38 = load i32, ptr %3, align 4, !tbaa !9
  %39 = icmp slt i32 %38, 10
  br i1 %39, label %41, label %40

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

41:                                               ; preds = %37
  %42 = load i32, ptr %3, align 4, !tbaa !9
  %43 = load ptr, ptr %1, align 8, !tbaa !5
  %44 = load i32, ptr %3, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds %struct.Student, ptr %43, i64 %45
  %47 = getelementptr inbounds %struct.Student, ptr %46, i32 0, i32 0
  %48 = getelementptr inbounds [80 x i8], ptr %47, i64 0, i64 0
  %49 = load ptr, ptr %1, align 8, !tbaa !5
  %50 = load i32, ptr %3, align 4, !tbaa !9
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds %struct.Student, ptr %49, i64 %51
  %53 = getelementptr inbounds %struct.Student, ptr %52, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !12
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.3, i32 noundef %42, ptr noundef %48, i32 noundef %54)
  br label %56

56:                                               ; preds = %41
  %57 = load i32, ptr %3, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %3, align 4, !tbaa !9
  br label %37, !llvm.loop !16

59:                                               ; preds = %40
  %60 = load ptr, ptr %1, align 8, !tbaa !5
  call void @free(ptr noundef %60) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %1) #5
  ret void
}

attributes #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 "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 = { 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 #5 = { nounwind }
attributes #6 = { 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 = !{!7, !7, i64 0}
!12 = !{!13, !10, i64 80}
!13 = !{!"Student", !7, i64 0, !10, i64 80}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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

@.str.2 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [27 x i8], align 1

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

; 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

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @example2() local_unnamed_addr #3 {
.preheader.preheader:
  %0 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.2)
  %1 = tail call noalias dereferenceable_or_null(840) ptr @malloc(i64 noundef 840) #4
  store i8 65, ptr %1, align 4, !tbaa !5
  %2 = getelementptr inbounds i8, ptr %1, i64 1
  store i8 0, ptr %2, align 1, !tbaa !5
  %3 = getelementptr inbounds i8, ptr %1, i64 80
  store i32 20, ptr %3, align 4, !tbaa !8
  %4 = getelementptr inbounds i8, ptr %1, i64 84
  store i8 66, ptr %4, align 4, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %1, i64 85
  store i8 0, ptr %5, align 1, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %1, i64 164
  store i32 21, ptr %6, align 4, !tbaa !8
  %7 = getelementptr inbounds i8, ptr %1, i64 168
  store i8 67, ptr %7, align 4, !tbaa !5
  %8 = getelementptr inbounds i8, ptr %1, i64 169
  store i8 0, ptr %8, align 1, !tbaa !5
  %9 = getelementptr inbounds i8, ptr %1, i64 248
  store i32 22, ptr %9, align 4, !tbaa !8
  %10 = getelementptr inbounds i8, ptr %1, i64 252
  store i8 68, ptr %10, align 4, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %1, i64 253
  store i8 0, ptr %11, align 1, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %1, i64 332
  store i32 23, ptr %12, align 4, !tbaa !8
  %13 = getelementptr inbounds i8, ptr %1, i64 336
  store i8 69, ptr %13, align 4, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %1, i64 337
  store i8 0, ptr %14, align 1, !tbaa !5
  %15 = getelementptr inbounds i8, ptr %1, i64 416
  store i32 24, ptr %15, align 4, !tbaa !8
  %16 = getelementptr inbounds i8, ptr %1, i64 420
  store i8 70, ptr %16, align 4, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %1, i64 421
  store i8 0, ptr %17, align 1, !tbaa !5
  %18 = getelementptr inbounds i8, ptr %1, i64 500
  store i32 25, ptr %18, align 4, !tbaa !8
  %19 = getelementptr inbounds i8, ptr %1, i64 504
  store i8 71, ptr %19, align 4, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %1, i64 505
  store i8 0, ptr %20, align 1, !tbaa !5
  %21 = getelementptr inbounds i8, ptr %1, i64 584
  store i32 26, ptr %21, align 4, !tbaa !8
  %22 = getelementptr inbounds i8, ptr %1, i64 588
  store i8 72, ptr %22, align 4, !tbaa !5
  %23 = getelementptr inbounds i8, ptr %1, i64 589
  store i8 0, ptr %23, align 1, !tbaa !5
  %24 = getelementptr inbounds i8, ptr %1, i64 668
  store i32 27, ptr %24, align 4, !tbaa !8
  %25 = getelementptr inbounds i8, ptr %1, i64 672
  store i8 73, ptr %25, align 4, !tbaa !5
  %26 = getelementptr inbounds i8, ptr %1, i64 673
  store i8 0, ptr %26, align 1, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %1, i64 752
  store i32 28, ptr %27, align 4, !tbaa !8
  %28 = getelementptr inbounds i8, ptr %1, i64 756
  store i8 74, ptr %28, align 4, !tbaa !5
  %29 = getelementptr inbounds i8, ptr %1, i64 757
  store i8 0, ptr %29, align 1, !tbaa !5
  %30 = getelementptr inbounds i8, ptr %1, i64 836
  store i32 29, ptr %30, align 4, !tbaa !8
  %31 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 0, ptr noundef %1, i32 noundef 20)
  %32 = load i32, ptr %6, align 4, !tbaa !8
  %33 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 1, ptr noundef nonnull %4, i32 noundef %32)
  %34 = load i32, ptr %9, align 4, !tbaa !8
  %35 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 2, ptr noundef nonnull %7, i32 noundef %34)
  %36 = load i32, ptr %12, align 4, !tbaa !8
  %37 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 3, ptr noundef nonnull %10, i32 noundef %36)
  %38 = load i32, ptr %15, align 4, !tbaa !8
  %39 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 4, ptr noundef nonnull %13, i32 noundef %38)
  %40 = load i32, ptr %18, align 4, !tbaa !8
  %41 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 5, ptr noundef nonnull %16, i32 noundef %40)
  %42 = load i32, ptr %21, align 4, !tbaa !8
  %43 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 6, ptr noundef nonnull %19, i32 noundef %42)
  %44 = load i32, ptr %24, align 4, !tbaa !8
  %45 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 7, ptr noundef nonnull %22, i32 noundef %44)
  %46 = load i32, ptr %27, align 4, !tbaa !8
  %47 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 8, ptr noundef nonnull %25, i32 noundef %46)
  %48 = load i32, ptr %30, align 4, !tbaa !8
  %49 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3, i32 noundef 9, ptr noundef nonnull %28, i32 noundef %48)
  tail call void @free(ptr noundef %1) #5
  ret void
}

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 = { 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 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, 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 #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 = { nounwind allocsize(0) }
attributes #5 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !10, i64 80}
!9 = !{!"Student", !6, i64 0, !10, i64 80}
!10 = !{!"int", !6, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,SROAPass,SimplifyCFGPass,TailCallElimPass