; LLVM IR from wuzhouhui/leetcode :: 46_permutations (permute)

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

@res = external hidden global ptr, align 8
@occupy = external hidden global [512 x i8], align 16
@maxp = external hidden global i32, align 4
@p = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @permute(ptr noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !5
  br label %8

8:                                                ; preds = %3
  store ptr null, ptr @res, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 16 @occupy, i8 0, i64 512, i1 false)
  store i32 0, ptr @maxp, align 4, !tbaa !9
  store i32 0, ptr @p, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %8
  %10 = load i32, ptr %6, align 4, !tbaa !9
  %11 = icmp ne i32 %10, 0
  br i1 %11, label %14, label %12

12:                                               ; preds = %9
  %13 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %13, align 4, !tbaa !9
  store ptr null, ptr %4, align 8
  br label %37

14:                                               ; preds = %9
  %15 = load i32, ptr %6, align 4, !tbaa !9
  %16 = icmp eq i32 %15, 1
  br i1 %16, label %17, label %31

17:                                               ; preds = %14
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 1, ptr %18, align 4, !tbaa !9
  %19 = call noalias ptr @malloc(i64 noundef 8) #3
  store ptr %19, ptr @res, align 8, !tbaa !5
  %20 = call noalias ptr @malloc(i64 noundef 4) #3
  %21 = load ptr, ptr @res, align 8, !tbaa !5
  %22 = getelementptr inbounds ptr, ptr %21, i64 0
  store ptr %20, ptr %22, align 8, !tbaa !5
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = getelementptr inbounds i32, ptr %23, i64 0
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = load ptr, ptr @res, align 8, !tbaa !5
  %27 = getelementptr inbounds ptr, ptr %26, i64 0
  %28 = load ptr, ptr %27, align 8, !tbaa !5
  %29 = getelementptr inbounds i32, ptr %28, i64 0
  store i32 %25, ptr %29, align 4, !tbaa !9
  %30 = load ptr, ptr @res, align 8, !tbaa !5
  store ptr %30, ptr %4, align 8
  br label %37

31:                                               ; preds = %14
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %6, align 4, !tbaa !9
  call void @bktrack(ptr noundef %32, i32 noundef %33, i32 noundef 0)
  %34 = load i32, ptr @p, align 4, !tbaa !9
  %35 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %34, ptr %35, align 4, !tbaa !9
  %36 = load ptr, ptr @res, align 8, !tbaa !5
  store ptr %36, ptr %4, align 8
  br label %37

37:                                               ; preds = %31, %17, %12
  %38 = load ptr, ptr %4, align 8
  ret ptr %38
}

; 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 allocsize(0)
declare noalias ptr @malloc(i64 noundef) #2

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

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 nounwind willreturn memory(argmem: write) }
attributes #2 = { nounwind allocsize(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 allocsize(0) }

!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 = !{!10, !10, i64 0}
!10 = !{!"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"

@res = external hidden local_unnamed_addr global ptr, align 8
@occupy = external hidden local_unnamed_addr global [512 x i8], align 16
@maxp = external hidden local_unnamed_addr global i32, align 4
@p = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local ptr @permute(ptr noundef %0, i32 noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  store ptr null, ptr @res, align 8, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(512) @occupy, i8 0, i64 512, i1 false)
  store i32 0, ptr @maxp, align 4, !tbaa !9
  store i32 0, ptr @p, align 4, !tbaa !9
  switch i32 %1, label %9 [
    i32 0, label %4
    i32 1, label %5
  ]

4:                                                ; preds = %3
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %12

5:                                                ; preds = %3
  store i32 1, ptr %2, align 4, !tbaa !9
  %6 = tail call noalias dereferenceable_or_null(8) ptr @malloc(i64 noundef 8) #3
  store ptr %6, ptr @res, align 8, !tbaa !5
  %7 = tail call noalias dereferenceable_or_null(4) ptr @malloc(i64 noundef 4) #3
  store ptr %7, ptr %6, align 8, !tbaa !5
  %8 = load i32, ptr %0, align 4, !tbaa !9
  store i32 %8, ptr %7, align 4, !tbaa !9
  br label %12

9:                                                ; preds = %3
  tail call void @bktrack(ptr noundef %0, i32 noundef %1, i32 noundef 0)
  %10 = load i32, ptr @p, align 4, !tbaa !9
  store i32 %10, ptr %2, align 4, !tbaa !9
  %11 = load ptr, ptr @res, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %9, %5, %4
  %.0 = phi ptr [ %6, %5 ], [ %11, %9 ], [ null, %4 ]
  ret ptr %.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: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

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

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 nounwind willreturn memory(argmem: write) }
attributes #2 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) 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 #3 = { nounwind allocsize(0) }

!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 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}


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