; LLVM IR from LeoVen/C-Macro-Collections :: examples (hanoi)

; 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.anon = type { ptr, ptr, ptr }
%struct.stack = type { ptr, i64, i64, i32, ptr, ptr }
%struct.stack_fval = type { ptr, ptr, ptr, ptr, ptr, ptr }

@print_aux = external dso_local global %struct.anon, align 8

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @stk_count(ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @stk_init(ptr dead_on_unwind noalias writable sret(%struct.stack) align 8, i64 noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @stk_release(ptr noundef byval(%struct.stack) align 8) #2

; Function Attrs: nounwind uwtable
declare dso_local void @print_stacks() #2

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.stack, align 8
  %3 = alloca %struct.stack_fval, align 8
  %4 = alloca %struct.stack, align 8
  %5 = alloca %struct.stack_fval, align 8
  %6 = alloca %struct.stack, align 8
  %7 = alloca %struct.stack_fval, align 8
  %8 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 48, ptr %2) #3
  call void @llvm.memset.p0.i64(ptr align 8 %3, i8 0, i64 48, i1 false)
  %9 = getelementptr inbounds %struct.stack_fval, ptr %3, i32 0, i32 0
  %10 = getelementptr inbounds %struct.stack_fval, ptr %3, i32 0, i32 1
  %11 = getelementptr inbounds %struct.stack_fval, ptr %3, i32 0, i32 2
  %12 = getelementptr inbounds %struct.stack_fval, ptr %3, i32 0, i32 3
  %13 = getelementptr inbounds %struct.stack_fval, ptr %3, i32 0, i32 4
  %14 = getelementptr inbounds %struct.stack_fval, ptr %3, i32 0, i32 5
  call void @stk_init(ptr dead_on_unwind writable sret(%struct.stack) align 8 %2, i64 noundef 10, ptr noundef %3)
  call void @llvm.lifetime.start.p0(i64 48, ptr %4) #3
  call void @llvm.memset.p0.i64(ptr align 8 %5, i8 0, i64 48, i1 false)
  %15 = getelementptr inbounds %struct.stack_fval, ptr %5, i32 0, i32 0
  %16 = getelementptr inbounds %struct.stack_fval, ptr %5, i32 0, i32 1
  %17 = getelementptr inbounds %struct.stack_fval, ptr %5, i32 0, i32 2
  %18 = getelementptr inbounds %struct.stack_fval, ptr %5, i32 0, i32 3
  %19 = getelementptr inbounds %struct.stack_fval, ptr %5, i32 0, i32 4
  %20 = getelementptr inbounds %struct.stack_fval, ptr %5, i32 0, i32 5
  call void @stk_init(ptr dead_on_unwind writable sret(%struct.stack) align 8 %4, i64 noundef 10, ptr noundef %5)
  call void @llvm.lifetime.start.p0(i64 48, ptr %6) #3
  call void @llvm.memset.p0.i64(ptr align 8 %7, i8 0, i64 48, i1 false)
  %21 = getelementptr inbounds %struct.stack_fval, ptr %7, i32 0, i32 0
  %22 = getelementptr inbounds %struct.stack_fval, ptr %7, i32 0, i32 1
  %23 = getelementptr inbounds %struct.stack_fval, ptr %7, i32 0, i32 2
  %24 = getelementptr inbounds %struct.stack_fval, ptr %7, i32 0, i32 3
  %25 = getelementptr inbounds %struct.stack_fval, ptr %7, i32 0, i32 4
  %26 = getelementptr inbounds %struct.stack_fval, ptr %7, i32 0, i32 5
  call void @stk_init(ptr dead_on_unwind writable sret(%struct.stack) align 8 %6, i64 noundef 10, ptr noundef %7)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 6, ptr %8, align 4, !tbaa !5
  br label %27

27:                                               ; preds = %34, %0
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = icmp sge i32 %28, 1
  br i1 %29, label %31, label %30

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

31:                                               ; preds = %27
  %32 = load i32, ptr %8, align 4, !tbaa !5
  %33 = call zeroext i1 @stk_push(ptr noundef %2, i32 noundef %32)
  br label %34

34:                                               ; preds = %31
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = add nsw i32 %35, -1
  store i32 %36, ptr %8, align 4, !tbaa !5
  br label %27, !llvm.loop !9

37:                                               ; preds = %30
  store ptr %2, ptr @print_aux, align 8, !tbaa !11
  store ptr %6, ptr getelementptr inbounds (%struct.anon, ptr @print_aux, i32 0, i32 1), align 8, !tbaa !14
  store ptr %4, ptr getelementptr inbounds (%struct.anon, ptr @print_aux, i32 0, i32 2), align 8, !tbaa !15
  call void @print_stacks()
  %38 = call i64 @stk_count(ptr noundef %2)
  %39 = trunc i64 %38 to i32
  call void @hanoi(i32 noundef %39, ptr noundef %2, ptr noundef %6, ptr noundef %4)
  call void @stk_release(ptr noundef byval(%struct.stack) align 8 %2)
  call void @stk_release(ptr noundef byval(%struct.stack) align 8 %4)
  call void @stk_release(ptr noundef byval(%struct.stack) align 8 %6)
  call void @llvm.lifetime.end.p0(i64 48, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 48, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 48, ptr %2) #3
  ret i32 0
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !13, i64 0}
!12 = !{!"", !13, i64 0, !13, i64 8, !13, i64 16}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !13, i64 8}
!15 = !{!12, !13, i64 16}


; 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.anon = type { ptr, ptr, ptr }
%struct.stack = type { ptr, i64, i64, i32, ptr, ptr }
%struct.stack_fval = type { ptr, ptr, ptr, ptr, ptr, ptr }

@print_aux = external dso_local local_unnamed_addr global %struct.anon, align 8

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

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

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i64 @stk_count(ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @stk_init(ptr dead_on_unwind noalias writable sret(%struct.stack) align 8, i64 noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @stk_release(ptr noundef byval(%struct.stack) align 8) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local void @print_stacks() local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #2 {
  %1 = alloca %struct.stack, align 8
  %2 = alloca %struct.stack_fval, align 8
  %3 = alloca %struct.stack, align 8
  %4 = alloca %struct.stack_fval, align 8
  %5 = alloca %struct.stack, align 8
  %6 = alloca %struct.stack_fval, align 8
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %1) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %2, i8 0, i64 48, i1 false)
  call void @stk_init(ptr dead_on_unwind nonnull writable sret(%struct.stack) align 8 %1, i64 noundef 10, ptr noundef nonnull %2)
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %3) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %4, i8 0, i64 48, i1 false)
  call void @stk_init(ptr dead_on_unwind nonnull writable sret(%struct.stack) align 8 %3, i64 noundef 10, ptr noundef nonnull %4)
  call void @llvm.lifetime.start.p0(i64 48, ptr nonnull %5) #3
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(48) %6, i8 0, i64 48, i1 false)
  call void @stk_init(ptr dead_on_unwind nonnull writable sret(%struct.stack) align 8 %5, i64 noundef 10, ptr noundef nonnull %6)
  %7 = call zeroext i1 @stk_push(ptr noundef nonnull %1, i32 noundef 6)
  %8 = call zeroext i1 @stk_push(ptr noundef nonnull %1, i32 noundef 5)
  %9 = call zeroext i1 @stk_push(ptr noundef nonnull %1, i32 noundef 4)
  %10 = call zeroext i1 @stk_push(ptr noundef nonnull %1, i32 noundef 3)
  %11 = call zeroext i1 @stk_push(ptr noundef nonnull %1, i32 noundef 2)
  %12 = call zeroext i1 @stk_push(ptr noundef nonnull %1, i32 noundef 1)
  store ptr %1, ptr @print_aux, align 8, !tbaa !5
  store ptr %5, ptr getelementptr inbounds (i8, ptr @print_aux, i64 8), align 8, !tbaa !10
  store ptr %3, ptr getelementptr inbounds (i8, ptr @print_aux, i64 16), align 8, !tbaa !11
  call void @print_stacks()
  %13 = call i64 @stk_count(ptr noundef nonnull %1)
  %14 = trunc i64 %13 to i32
  call void @hanoi(i32 noundef %14, ptr noundef nonnull %1, ptr noundef nonnull %5, ptr noundef nonnull %3)
  call void @stk_release(ptr noundef nonnull byval(%struct.stack) align 8 %1)
  call void @stk_release(ptr noundef nonnull byval(%struct.stack) align 8 %3)
  call void @stk_release(ptr noundef nonnull byval(%struct.stack) align 8 %5)
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %5) #3
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 48, ptr nonnull %1) #3
  ret i32 0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 #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, !7, i64 0}
!6 = !{!"", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = !{!6, !7, i64 16}


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