; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0030 Easy/solutions (solution)

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

@.str.3 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [126 x i8], align 1
@__PRETTY_FUNCTION__.main = external hidden unnamed_addr constant [23 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [28 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @print_arr(ptr noundef, i32 noundef) #0

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

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

; 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
declare dso_local void @quicksort(ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @find_target_sum(ptr noundef, i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  %9 = call noalias ptr @calloc(i64 noundef 13, i64 noundef 4) #7
  store ptr %9, ptr %6, align 8, !tbaa !9
  %10 = load ptr, ptr %6, align 8, !tbaa !9
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %13

12:                                               ; preds = %2
  br label %14

13:                                               ; preds = %2
  call void @__assert_fail(ptr noundef @.str.3, ptr noundef @.str.4, i32 noundef 105, ptr noundef @__PRETTY_FUNCTION__.main) #8
  unreachable

14:                                               ; preds = %12
  store i32 0, ptr %7, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %25, %14
  %16 = load i32, ptr %7, align 4, !tbaa !5
  %17 = icmp slt i32 %16, 13
  br i1 %17, label %18, label %28

18:                                               ; preds = %15
  %19 = call i32 @rand() #6
  %20 = srem i32 %19, 1234
  %21 = load ptr, ptr %6, align 8, !tbaa !9
  %22 = load i32, ptr %7, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i32, ptr %21, i64 %23
  store i32 %20, ptr %24, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %18
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = add nsw i32 %26, 1
  store i32 %27, ptr %7, align 4, !tbaa !5
  br label %15, !llvm.loop !11

28:                                               ; preds = %15
  %29 = load ptr, ptr %6, align 8, !tbaa !9
  call void @print_arr(ptr noundef %29, i32 noundef 13)
  %30 = load ptr, ptr %6, align 8, !tbaa !9
  call void @quicksort(ptr noundef %30, i32 noundef 0, i32 noundef 12)
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  call void @print_arr(ptr noundef %31, i32 noundef 13)
  %32 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  %33 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.6, ptr noundef %8)
  %34 = load ptr, ptr %6, align 8, !tbaa !9
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = call zeroext i1 @find_target_sum(ptr noundef %34, i32 noundef 13, i32 noundef %35)
  br i1 %36, label %39, label %37

37:                                               ; preds = %28
  %38 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  br label %39

39:                                               ; preds = %37, %28
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  ret i32 0
}

; Function Attrs: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #3

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) #4

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

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

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 = { "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,1) "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 = { noreturn 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0,1) }
attributes #8 = { noreturn 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@.str.3 = external hidden unnamed_addr constant [12 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [126 x i8], align 1
@__PRETTY_FUNCTION__.main = external hidden unnamed_addr constant [23 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [28 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @print_arr(ptr noundef, i32 noundef) local_unnamed_addr #0

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @quicksort(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @find_target_sum(ptr noundef, i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr nocapture noundef readnone %1) local_unnamed_addr #0 {
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %3) #6
  %4 = tail call noalias dereferenceable_or_null(52) ptr @calloc(i64 noundef 13, i64 noundef 4) #7
  %.not = icmp eq ptr %4, null
  br i1 %.not, label %47, label %.preheader.preheader

.preheader.preheader:                             ; preds = %2
  %5 = tail call i32 @rand() #6
  %6 = srem i32 %5, 1234
  store i32 %6, ptr %4, align 4, !tbaa !5
  %7 = tail call i32 @rand() #6
  %8 = srem i32 %7, 1234
  %9 = getelementptr inbounds i8, ptr %4, i64 4
  store i32 %8, ptr %9, align 4, !tbaa !5
  %10 = tail call i32 @rand() #6
  %11 = srem i32 %10, 1234
  %12 = getelementptr inbounds i8, ptr %4, i64 8
  store i32 %11, ptr %12, align 4, !tbaa !5
  %13 = tail call i32 @rand() #6
  %14 = srem i32 %13, 1234
  %15 = getelementptr inbounds i8, ptr %4, i64 12
  store i32 %14, ptr %15, align 4, !tbaa !5
  %16 = tail call i32 @rand() #6
  %17 = srem i32 %16, 1234
  %18 = getelementptr inbounds i8, ptr %4, i64 16
  store i32 %17, ptr %18, align 4, !tbaa !5
  %19 = tail call i32 @rand() #6
  %20 = srem i32 %19, 1234
  %21 = getelementptr inbounds i8, ptr %4, i64 20
  store i32 %20, ptr %21, align 4, !tbaa !5
  %22 = tail call i32 @rand() #6
  %23 = srem i32 %22, 1234
  %24 = getelementptr inbounds i8, ptr %4, i64 24
  store i32 %23, ptr %24, align 4, !tbaa !5
  %25 = tail call i32 @rand() #6
  %26 = srem i32 %25, 1234
  %27 = getelementptr inbounds i8, ptr %4, i64 28
  store i32 %26, ptr %27, align 4, !tbaa !5
  %28 = tail call i32 @rand() #6
  %29 = srem i32 %28, 1234
  %30 = getelementptr inbounds i8, ptr %4, i64 32
  store i32 %29, ptr %30, align 4, !tbaa !5
  %31 = tail call i32 @rand() #6
  %32 = srem i32 %31, 1234
  %33 = getelementptr inbounds i8, ptr %4, i64 36
  store i32 %32, ptr %33, align 4, !tbaa !5
  %34 = tail call i32 @rand() #6
  %35 = srem i32 %34, 1234
  %36 = getelementptr inbounds i8, ptr %4, i64 40
  store i32 %35, ptr %36, align 4, !tbaa !5
  %37 = tail call i32 @rand() #6
  %38 = srem i32 %37, 1234
  %39 = getelementptr inbounds i8, ptr %4, i64 44
  store i32 %38, ptr %39, align 4, !tbaa !5
  %40 = tail call i32 @rand() #6
  %41 = srem i32 %40, 1234
  %42 = getelementptr inbounds i8, ptr %4, i64 48
  store i32 %41, ptr %42, align 4, !tbaa !5
  tail call void @print_arr(ptr noundef nonnull %4, i32 noundef 13)
  tail call void @quicksort(ptr noundef nonnull %4, i32 noundef 0, i32 noundef 12)
  tail call void @print_arr(ptr noundef nonnull %4, i32 noundef 13)
  %43 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %44 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.6, ptr noundef nonnull %3)
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = call zeroext i1 @find_target_sum(ptr noundef nonnull %4, i32 noundef 13, i32 noundef %45)
  br i1 %46, label %50, label %48

47:                                               ; preds = %2
  tail call void @__assert_fail(ptr noundef nonnull @.str.3, ptr noundef nonnull @.str.4, i32 noundef 105, ptr noundef nonnull @__PRETTY_FUNCTION__.main) #8
  unreachable

48:                                               ; preds = %.preheader.preheader
  %49 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  br label %50

50:                                               ; preds = %48, %.preheader.preheader
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #6
  ret i32 0
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: noreturn nounwind
declare void @__assert_fail(ptr noundef, ptr noundef, i32 noundef, ptr noundef) local_unnamed_addr #4

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

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

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 = { 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 #3 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) 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 #4 = { noreturn 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 #5 = { 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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0,1) }
attributes #8 = { noreturn 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/C++ TBAA"}


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