; LLVM IR from ravireddy07/cpp-book :: src/Algorithm-Based/extendedEuclid.ll (Z14extendedEuclidii)

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

%class.triplet = type { i32, i32, i32 }

; Function Attrs: mustprogress uwtable
define dso_local { i64, i32 } @_Z14extendedEuclidii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca %class.triplet, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca %class.triplet, align 4
  %7 = alloca { i64, i32 }, align 8
  %8 = alloca { i64, i32 }, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %9 = load i32, ptr %5, align 4, !tbaa !5
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %16

11:                                               ; preds = %2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = getelementptr inbounds %class.triplet, ptr %3, i32 0, i32 2
  store i32 %12, ptr %13, align 4, !tbaa !9
  %14 = getelementptr inbounds %class.triplet, ptr %3, i32 0, i32 1
  store i32 4, ptr %14, align 4, !tbaa !11
  %15 = getelementptr inbounds %class.triplet, ptr %3, i32 0, i32 0
  store i32 1, ptr %15, align 4, !tbaa !12
  br label %38

16:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 12, ptr %6) #3
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = srem i32 %18, %19
  %21 = call { i64, i32 } @_Z14extendedEuclidii(i32 noundef %17, i32 noundef %20)
  store { i64, i32 } %21, ptr %7, align 8
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %6, ptr align 8 %7, i64 12, i1 false)
  %22 = getelementptr inbounds %class.triplet, ptr %6, i32 0, i32 1
  %23 = load i32, ptr %22, align 4, !tbaa !11
  %24 = getelementptr inbounds %class.triplet, ptr %3, i32 0, i32 0
  store i32 %23, ptr %24, align 4, !tbaa !12
  %25 = getelementptr inbounds %class.triplet, ptr %6, i32 0, i32 0
  %26 = load i32, ptr %25, align 4, !tbaa !12
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = load i32, ptr %5, align 4, !tbaa !5
  %29 = sdiv i32 %27, %28
  %30 = getelementptr inbounds %class.triplet, ptr %6, i32 0, i32 1
  %31 = load i32, ptr %30, align 4, !tbaa !11
  %32 = mul nsw i32 %29, %31
  %33 = sub nsw i32 %26, %32
  %34 = getelementptr inbounds %class.triplet, ptr %3, i32 0, i32 1
  store i32 %33, ptr %34, align 4, !tbaa !11
  %35 = getelementptr inbounds %class.triplet, ptr %6, i32 0, i32 2
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = getelementptr inbounds %class.triplet, ptr %3, i32 0, i32 2
  store i32 %36, ptr %37, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 12, ptr %6) #3
  br label %38

38:                                               ; preds = %16, %11
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %8, ptr align 4 %3, i64 12, i1 false)
  %39 = load { i64, i32 }, ptr %8, align 8
  ret { i64, i32 } %39
}

; 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 nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #2

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

attributes #0 = { mustprogress 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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTS7triplet", !6, i64 0, !6, i64 4, !6, i64 8}
!11 = !{!10, !6, i64 4}
!12 = !{!10, !6, 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: mustprogress nofree nosync nounwind willreturn memory(none) uwtable
define dso_local { i64, i32 } @_Z14extendedEuclidii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %13, label %4

4:                                                ; preds = %2
  %5 = srem i32 %0, %1
  %6 = tail call { i64, i32 } @_Z14extendedEuclidii(i32 noundef %1, i32 noundef %5)
  %.fca.0.extract = extractvalue { i64, i32 } %6, 0
  %.fca.1.extract = extractvalue { i64, i32 } %6, 1
  %.sroa.03.4.extract.shift = lshr i64 %.fca.0.extract, 32
  %.sroa.03.4.extract.trunc = trunc nuw i64 %.sroa.03.4.extract.shift to i32
  %.sroa.03.0.extract.trunc = trunc i64 %.fca.0.extract to i32
  %7 = sdiv i32 %0, %1
  %8 = mul nsw i32 %7, %.sroa.03.4.extract.trunc
  %9 = sub nsw i32 %.sroa.03.0.extract.trunc, %8
  %10 = zext i32 %9 to i64
  %11 = shl nuw i64 %10, 32
  %12 = or disjoint i64 %11, %.sroa.03.4.extract.shift
  br label %13

13:                                               ; preds = %2, %4
  %.sroa.5.0 = phi i32 [ %.fca.1.extract, %4 ], [ %0, %2 ]
  %.sroa.013.0.insert.insert = phi i64 [ %12, %4 ], [ 17179869185, %2 ]
  %.fca.0.insert = insertvalue { i64, i32 } poison, i64 %.sroa.013.0.insert.insert, 0
  %.fca.1.insert = insertvalue { i64, i32 } %.fca.0.insert, i32 %.sroa.5.0, 1
  ret { i64, i32 } %.fca.1.insert
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn memory(none) 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" }

!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)"}


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