; LLVM IR from haytham-hammioui/CS50x-2024 :: Week 5 Data Structures/Problem set 5/inheritance (inheritance)

; 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.person = type { [2 x ptr], [2 x i8] }

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

; Function Attrs: nounwind uwtable
define dso_local ptr @create_family(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #4
  %4 = call noalias ptr @malloc(i64 noundef 24) #5
  store ptr %4, ptr %3, align 8, !tbaa !9
  %5 = load i32, ptr %2, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, 1
  br i1 %6, label %7, label %46

7:                                                ; preds = %1
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sub nsw i32 %8, 1
  %10 = call ptr @create_family(i32 noundef %9)
  %11 = load ptr, ptr %3, align 8, !tbaa !9
  %12 = getelementptr inbounds %struct.person, ptr %11, i32 0, i32 0
  %13 = getelementptr inbounds [2 x ptr], ptr %12, i64 0, i64 0
  store ptr %10, ptr %13, align 8, !tbaa !9
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = sub nsw i32 %14, 1
  %16 = call ptr @create_family(i32 noundef %15)
  %17 = load ptr, ptr %3, align 8, !tbaa !9
  %18 = getelementptr inbounds %struct.person, ptr %17, i32 0, i32 0
  %19 = getelementptr inbounds [2 x ptr], ptr %18, i64 0, i64 1
  store ptr %16, ptr %19, align 8, !tbaa !9
  %20 = load ptr, ptr %3, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.person, ptr %20, i32 0, i32 0
  %22 = getelementptr inbounds [2 x ptr], ptr %21, i64 0, i64 0
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = getelementptr inbounds %struct.person, ptr %23, i32 0, i32 1
  %25 = call i32 @rand() #4
  %26 = srem i32 %25, 2
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [2 x i8], ptr %24, i64 0, i64 %27
  %29 = load i8, ptr %28, align 1, !tbaa !11
  %30 = load ptr, ptr %3, align 8, !tbaa !9
  %31 = getelementptr inbounds %struct.person, ptr %30, i32 0, i32 1
  %32 = getelementptr inbounds [2 x i8], ptr %31, i64 0, i64 0
  store i8 %29, ptr %32, align 8, !tbaa !11
  %33 = load ptr, ptr %3, align 8, !tbaa !9
  %34 = getelementptr inbounds %struct.person, ptr %33, i32 0, i32 0
  %35 = getelementptr inbounds [2 x ptr], ptr %34, i64 0, i64 1
  %36 = load ptr, ptr %35, align 8, !tbaa !9
  %37 = getelementptr inbounds %struct.person, ptr %36, i32 0, i32 1
  %38 = call i32 @rand() #4
  %39 = srem i32 %38, 2
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [2 x i8], ptr %37, i64 0, i64 %40
  %42 = load i8, ptr %41, align 1, !tbaa !11
  %43 = load ptr, ptr %3, align 8, !tbaa !9
  %44 = getelementptr inbounds %struct.person, ptr %43, i32 0, i32 1
  %45 = getelementptr inbounds [2 x i8], ptr %44, i64 0, i64 1
  store i8 %42, ptr %45, align 1, !tbaa !11
  br label %61

46:                                               ; preds = %1
  %47 = load ptr, ptr %3, align 8, !tbaa !9
  %48 = getelementptr inbounds %struct.person, ptr %47, i32 0, i32 0
  %49 = getelementptr inbounds [2 x ptr], ptr %48, i64 0, i64 0
  store ptr null, ptr %49, align 8, !tbaa !9
  %50 = load ptr, ptr %3, align 8, !tbaa !9
  %51 = getelementptr inbounds %struct.person, ptr %50, i32 0, i32 0
  %52 = getelementptr inbounds [2 x ptr], ptr %51, i64 0, i64 1
  store ptr null, ptr %52, align 8, !tbaa !9
  %53 = call signext i8 @random_allele()
  %54 = load ptr, ptr %3, align 8, !tbaa !9
  %55 = getelementptr inbounds %struct.person, ptr %54, i32 0, i32 1
  %56 = getelementptr inbounds [2 x i8], ptr %55, i64 0, i64 0
  store i8 %53, ptr %56, align 8, !tbaa !11
  %57 = call signext i8 @random_allele()
  %58 = load ptr, ptr %3, align 8, !tbaa !9
  %59 = getelementptr inbounds %struct.person, ptr %58, i32 0, i32 1
  %60 = getelementptr inbounds [2 x i8], ptr %59, i64 0, i64 1
  store i8 %57, ptr %60, align 1, !tbaa !11
  br label %61

61:                                               ; preds = %46, %7
  %62 = load ptr, ptr %3, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #4
  ret ptr %62
}

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

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

; Function Attrs: nounwind
declare i32 @rand() #3

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @random_allele() #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 }
attributes #5 = { 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 = !{!7, !7, i64 0}


; 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: nounwind uwtable
define dso_local noalias noundef ptr @create_family(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  %3 = icmp sgt i32 %0, 1
  br i1 %3, label %7, label %23

common.ret:                                       ; preds = %23, %7
  %.sink15 = phi ptr [ %9, %7 ], [ null, %23 ]
  %.sink14 = phi ptr [ %10, %7 ], [ null, %23 ]
  %.sink13 = phi i8 [ %16, %7 ], [ %24, %23 ]
  %.sink = phi i8 [ %22, %7 ], [ %25, %23 ]
  store ptr %.sink15, ptr %2, align 8
  %4 = getelementptr inbounds i8, ptr %2, i64 8
  store ptr %.sink14, ptr %4, align 8
  %5 = getelementptr inbounds i8, ptr %2, i64 16
  store i8 %.sink13, ptr %5, align 8
  %6 = getelementptr inbounds i8, ptr %2, i64 17
  store i8 %.sink, ptr %6, align 1
  ret ptr %2

7:                                                ; preds = %1
  %8 = add nsw i32 %0, -1
  %9 = tail call ptr @create_family(i32 noundef %8)
  %10 = tail call ptr @create_family(i32 noundef %8)
  %11 = getelementptr inbounds i8, ptr %9, i64 16
  %12 = tail call i32 @rand() #4
  %13 = srem i32 %12, 2
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [2 x i8], ptr %11, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %10, i64 16
  %18 = tail call i32 @rand() #4
  %19 = srem i32 %18, 2
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [2 x i8], ptr %17, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !5
  br label %common.ret

23:                                               ; preds = %1
  %24 = tail call signext i8 @random_allele()
  %25 = tail call signext i8 @random_allele()
  br label %common.ret
}

; 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: nounwind
declare i32 @rand() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @random_allele() local_unnamed_addr #0

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 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 "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 allocsize(0) }
attributes #4 = { 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"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,MergedLoadStoreMotionPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass