; LLVM IR from brpapa/algorithms :: solutions/uva/166.ll (Z6greedyi)

; 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.std::array" = type { [6 x i32] }

@coins = external dso_local global %"struct.std::array", align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6greedyi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 5, ptr %4, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %24, %1
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = icmp sge i32 %6, 0
  br i1 %7, label %9, label %8

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

9:                                                ; preds = %5
  %10 = load i32, ptr %2, align 4, !tbaa !5
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef %12) #2
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = sdiv i32 %10, %14
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = add nsw i32 %16, %15
  store i32 %17, ptr %3, align 4, !tbaa !5
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sext i32 %18 to i64
  %20 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef %19) #2
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = load i32, ptr %2, align 4, !tbaa !5
  %23 = srem i32 %22, %21
  store i32 %23, ptr %2, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %9
  %25 = load i32, ptr %4, align 4, !tbaa !5
  %26 = add nsw i32 %25, -1
  store i32 %26, ptr %4, align 4, !tbaa !5
  br label %5, !llvm.loop !9

27:                                               ; preds = %8
  %28 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %28
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24), i64 noundef) #0 align 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 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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.std::array" = type { [6 x i32] }

@coins = external dso_local global %"struct.std::array", align 4

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z6greedyi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 5) #1
  %3 = load i32, ptr %2, align 4, !tbaa !5
  %4 = sdiv i32 %0, %3
  %5 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 5) #1
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = srem i32 %0, %6
  %8 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 4) #1
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = sdiv i32 %7, %9
  %11 = add nsw i32 %10, %4
  %12 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 4) #1
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = srem i32 %7, %13
  %15 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 3) #1
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = sdiv i32 %14, %16
  %18 = add nsw i32 %17, %11
  %19 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 3) #1
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = srem i32 %14, %20
  %22 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 2) #1
  %23 = load i32, ptr %22, align 4, !tbaa !5
  %24 = sdiv i32 %21, %23
  %25 = add nsw i32 %24, %18
  %26 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 2) #1
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = srem i32 %21, %27
  %29 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 1) #1
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = sdiv i32 %28, %30
  %32 = add nsw i32 %31, %25
  %33 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 1) #1
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %35 = srem i32 %28, %34
  %36 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 0) #1
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = sdiv i32 %35, %37
  %39 = add nsw i32 %38, %32
  %40 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24) @coins, i64 noundef 0) #1
  ret i32 %39
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt5arrayIiLm6EEixEm(ptr noundef nonnull align 4 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

attributes #0 = { mustprogress 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 = { 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"}


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