; LLVM IR from AllenDowney/ExercisesInC :: exercises/ex02.5 (bigint)

; 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: nounwind uwtable
declare dso_local void @add_digits(i8 noundef signext, i8 noundef signext, i8 noundef signext, ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @add_bigint(ptr noundef %0, ptr noundef %1, i8 noundef signext %2, ptr noundef %3) #0 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i8, align 1
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  %10 = alloca i8, align 1
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i8, align 1
  %15 = alloca i8, align 1
  %16 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i8 %2, ptr %7, align 1, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  store i32 1, ptr %11, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 1, ptr %12, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  store i32 1, ptr %13, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 1, ptr %14) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %15) #2
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = load i8, ptr %17, align 1, !tbaa !9
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %22

21:                                               ; preds = %4
  store i8 48, ptr %14, align 1, !tbaa !9
  store i32 0, ptr %11, align 4, !tbaa !10
  br label %25

22:                                               ; preds = %4
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i8, ptr %23, align 1, !tbaa !9
  store i8 %24, ptr %14, align 1, !tbaa !9
  br label %25

25:                                               ; preds = %22, %21
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %28 = sext i8 %27 to i32
  %29 = icmp eq i32 %28, 0
  br i1 %29, label %30, label %31

30:                                               ; preds = %25
  store i8 48, ptr %15, align 1, !tbaa !9
  store i32 0, ptr %12, align 4, !tbaa !10
  br label %34

31:                                               ; preds = %25
  %32 = load ptr, ptr %6, align 8, !tbaa !5
  %33 = load i8, ptr %32, align 1, !tbaa !9
  store i8 %33, ptr %15, align 1, !tbaa !9
  br label %34

34:                                               ; preds = %31, %30
  %35 = load i8, ptr %14, align 1, !tbaa !9
  %36 = load i8, ptr %15, align 1, !tbaa !9
  %37 = load i8, ptr %7, align 1, !tbaa !9
  call void @add_digits(i8 noundef signext %35, i8 noundef signext %36, i8 noundef signext %37, ptr noundef %9, ptr noundef %10)
  %38 = load i8, ptr %9, align 1, !tbaa !9
  %39 = sext i8 %38 to i32
  %40 = icmp eq i32 %39, 48
  br i1 %40, label %41, label %47

41:                                               ; preds = %34
  %42 = load i8, ptr %10, align 1, !tbaa !9
  %43 = sext i8 %42 to i32
  %44 = icmp eq i32 %43, 48
  br i1 %44, label %45, label %47

45:                                               ; preds = %41
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %46, align 1, !tbaa !9
  store i32 1, ptr %16, align 4
  br label %63

47:                                               ; preds = %41, %34
  %48 = load i8, ptr %9, align 1, !tbaa !9
  %49 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 %48, ptr %49, align 1, !tbaa !9
  %50 = load ptr, ptr %5, align 8, !tbaa !5
  %51 = load i32, ptr %11, align 4, !tbaa !10
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i8, ptr %50, i64 %52
  %54 = load ptr, ptr %6, align 8, !tbaa !5
  %55 = load i32, ptr %12, align 4, !tbaa !10
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds i8, ptr %54, i64 %56
  %58 = load i8, ptr %10, align 1, !tbaa !9
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = load i32, ptr %13, align 4, !tbaa !10
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i8, ptr %59, i64 %61
  call void @add_bigint(ptr noundef %53, ptr noundef %57, i8 noundef signext %58, ptr noundef %62)
  store i32 0, ptr %16, align 4
  br label %63

63:                                               ; preds = %47, %45
  call void @llvm.lifetime.end.p0(i64 1, ptr %15) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  %64 = load i32, ptr %16, align 4
  switch i32 %64, label %66 [
    i32 0, label %65
    i32 1, label %65
  ]

65:                                               ; preds = %63, %63
  ret void

66:                                               ; preds = %63
  unreachable
}

; 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 = { 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/C++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !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
declare dso_local void @add_digits(i8 noundef signext, i8 noundef signext, i8 noundef signext, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @add_bigint(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i8 noundef signext %2, ptr nocapture noundef writeonly %3) local_unnamed_addr #0 {
  %5 = alloca i8, align 1
  %6 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %5) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %6) #2
  %7 = load i8, ptr %0, align 1, !tbaa !5
  %8 = icmp ne i8 %7, 0
  %.19 = select i1 %8, i8 %7, i8 48
  %9 = load i8, ptr %1, align 1, !tbaa !5
  %10 = icmp ne i8 %9, 0
  %.0 = select i1 %10, i8 %9, i8 48
  call void @add_digits(i8 noundef signext %.19, i8 noundef signext %.0, i8 noundef signext %2, ptr noundef nonnull %5, ptr noundef nonnull %6)
  %11 = load i8, ptr %5, align 1, !tbaa !5
  %12 = icmp eq i8 %11, 48
  %13 = load i8, ptr %6, align 1
  %14 = icmp eq i8 %13, 48
  %or.cond = select i1 %12, i1 %14, i1 false
  br i1 %or.cond, label %15, label %16

15:                                               ; preds = %4
  store i8 0, ptr %3, align 1, !tbaa !5
  br label %20

16:                                               ; preds = %4
  %.018 = zext i1 %10 to i64
  %. = zext i1 %8 to i64
  store i8 %11, ptr %3, align 1, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %0, i64 %.
  %18 = getelementptr inbounds i8, ptr %1, i64 %.018
  %19 = getelementptr inbounds i8, ptr %3, i64 1
  call void @add_bigint(ptr noundef nonnull %17, ptr noundef nonnull %18, i8 noundef signext %13, ptr noundef nonnull %19)
  br label %20

20:                                               ; preds = %16, %15
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %6) #2
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %5) #2
  ret void
}

; 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 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 = { 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,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass