; LLVM IR from codemistic/Data-Structures-and-Algorithms :: CSES/Intro/CheesboardAndQueens.ll (Z3dfsii)

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

@grid = external dso_local global [8 x [8 x i8]], align 16
@_ZL3cnt = external hidden global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_Z7is_safeii(i32 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

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

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsii(i32 noundef %0, i32 noundef %1) #2 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %6 = load i32, ptr %4, align 4, !tbaa !5
  %7 = icmp sge i32 %6, 8
  br i1 %7, label %8, label %11

8:                                                ; preds = %2
  %9 = load i32, ptr @_ZL3cnt, align 4, !tbaa !5
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @_ZL3cnt, align 4, !tbaa !5
  br label %50

11:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %47, %11
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp slt i32 %13, 8
  br i1 %14, label %16, label %15

15:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %50

16:                                               ; preds = %12
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 %18
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds [8 x i8], ptr %19, i64 0, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !9
  %24 = sext i8 %23 to i32
  %25 = icmp ne i32 %24, 42
  br i1 %25, label %26, label %46

26:                                               ; preds = %16
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = load i32, ptr %4, align 4, !tbaa !5
  %29 = call noundef zeroext i1 @_Z7is_safeii(i32 noundef %27, i32 noundef %28)
  br i1 %29, label %30, label %46

30:                                               ; preds = %26
  %31 = load i32, ptr %5, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 %32
  %34 = load i32, ptr %4, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [8 x i8], ptr %33, i64 0, i64 %35
  store i8 71, ptr %36, align 1, !tbaa !9
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  call void @_Z3dfsii(i32 noundef %37, i32 noundef %39)
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 %41
  %43 = load i32, ptr %4, align 4, !tbaa !5
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [8 x i8], ptr %42, i64 0, i64 %44
  store i8 46, ptr %45, align 1, !tbaa !9
  br label %46

46:                                               ; preds = %30, %26, %16
  br label %47

47:                                               ; preds = %46
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %48, 1
  store i32 %49, ptr %5, align 4, !tbaa !5
  br label %12, !llvm.loop !10

50:                                               ; preds = %15, %8
  ret void
}

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

@grid = external dso_local local_unnamed_addr global [8 x [8 x i8]], align 16
@_ZL3cnt = external hidden local_unnamed_addr global i32, align 4

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3dfsii(i32 %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 7
  br i1 %3, label %8, label %.preheader

.preheader:                                       ; preds = %2
  %4 = sext i32 %1 to i64
  %5 = add nsw i32 %1, 1
  %6 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 0, i64 %4
  %7 = load i8, ptr %6, align 1, !tbaa !5
  %.not = icmp eq i8 %7, 42
  br i1 %.not, label %14, label %11

8:                                                ; preds = %2
  %9 = load i32, ptr @_ZL3cnt, align 4, !tbaa !8
  %10 = add nsw i32 %9, 1
  store i32 %10, ptr @_ZL3cnt, align 4, !tbaa !8
  br label %common.ret

11:                                               ; preds = %.preheader
  %12 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 0, i32 noundef %1)
  br i1 %12, label %13, label %14

13:                                               ; preds = %11
  store i8 71, ptr %6, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %6, align 1, !tbaa !5
  br label %14

14:                                               ; preds = %.preheader, %11, %13
  %15 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 1, i64 %4
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %.not.1 = icmp eq i8 %16, 42
  br i1 %.not.1, label %20, label %17

17:                                               ; preds = %14
  %18 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 1, i32 noundef %1)
  br i1 %18, label %19, label %20

19:                                               ; preds = %17
  store i8 71, ptr %15, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %15, align 1, !tbaa !5
  br label %20

20:                                               ; preds = %19, %17, %14
  %21 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 2, i64 %4
  %22 = load i8, ptr %21, align 1, !tbaa !5
  %.not.2 = icmp eq i8 %22, 42
  br i1 %.not.2, label %26, label %23

23:                                               ; preds = %20
  %24 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 2, i32 noundef %1)
  br i1 %24, label %25, label %26

25:                                               ; preds = %23
  store i8 71, ptr %21, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %21, align 1, !tbaa !5
  br label %26

26:                                               ; preds = %25, %23, %20
  %27 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 3, i64 %4
  %28 = load i8, ptr %27, align 1, !tbaa !5
  %.not.3 = icmp eq i8 %28, 42
  br i1 %.not.3, label %32, label %29

29:                                               ; preds = %26
  %30 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 3, i32 noundef %1)
  br i1 %30, label %31, label %32

31:                                               ; preds = %29
  store i8 71, ptr %27, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %27, align 1, !tbaa !5
  br label %32

32:                                               ; preds = %31, %29, %26
  %33 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 4, i64 %4
  %34 = load i8, ptr %33, align 1, !tbaa !5
  %.not.4 = icmp eq i8 %34, 42
  br i1 %.not.4, label %38, label %35

35:                                               ; preds = %32
  %36 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 4, i32 noundef %1)
  br i1 %36, label %37, label %38

37:                                               ; preds = %35
  store i8 71, ptr %33, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %33, align 1, !tbaa !5
  br label %38

38:                                               ; preds = %37, %35, %32
  %39 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 5, i64 %4
  %40 = load i8, ptr %39, align 1, !tbaa !5
  %.not.5 = icmp eq i8 %40, 42
  br i1 %.not.5, label %44, label %41

41:                                               ; preds = %38
  %42 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 5, i32 noundef %1)
  br i1 %42, label %43, label %44

43:                                               ; preds = %41
  store i8 71, ptr %39, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %39, align 1, !tbaa !5
  br label %44

44:                                               ; preds = %43, %41, %38
  %45 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 6, i64 %4
  %46 = load i8, ptr %45, align 1, !tbaa !5
  %.not.6 = icmp eq i8 %46, 42
  br i1 %.not.6, label %50, label %47

47:                                               ; preds = %44
  %48 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 6, i32 noundef %1)
  br i1 %48, label %49, label %50

49:                                               ; preds = %47
  store i8 71, ptr %45, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %45, align 1, !tbaa !5
  br label %50

50:                                               ; preds = %49, %47, %44
  %51 = getelementptr inbounds [8 x [8 x i8]], ptr @grid, i64 0, i64 7, i64 %4
  %52 = load i8, ptr %51, align 1, !tbaa !5
  %.not.7 = icmp eq i8 %52, 42
  br i1 %.not.7, label %common.ret, label %53

53:                                               ; preds = %50
  %54 = tail call noundef zeroext i1 @_Z7is_safeii(i32 noundef 7, i32 noundef %1)
  br i1 %54, label %55, label %common.ret

common.ret:                                       ; preds = %8, %53, %50, %55
  ret void

55:                                               ; preds = %53
  store i8 71, ptr %51, align 1, !tbaa !5
  tail call void @_Z3dfsii(i32 poison, i32 noundef %5)
  store i8 46, ptr %51, align 1, !tbaa !5
  br label %common.ret
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"int", !6, i64 0}


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