; LLVM IR from AlgoGenesis/C :: Backtracking Algorithms/Chess-In-Terminal/src (main)

; 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.Game = type { i32, [8 x [8 x i16]], i32, i32, i32 }

@win_board = external dso_local global ptr, align 8
@win_dialog = external dso_local global ptr, align 8
@is_board_focused = external dso_local global i8, align 1

; 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: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @handle_board_signal(ptr noundef byval(%struct.Game) align 8, i32 noundef) #2

; Function Attrs: nounwind uwtable
define dso_local void @run_game_gui(ptr noundef %0) #2 {
  %2 = alloca ptr, align 8
  %3 = alloca %struct.Game, align 8
  %4 = alloca i32, align 4
  %5 = alloca %struct.Game, align 8
  %6 = alloca i32, align 4
  %7 = alloca %struct.Game, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 144, ptr %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %3, ptr align 4 %11, i64 144, i1 false), !tbaa.struct !9
  call void @setup(ptr noundef byval(%struct.Game) align 8 %3)
  call void @llvm.lifetime.end.p0(i64 144, ptr %3) #4
  br label %12

12:                                               ; preds = %44, %1
  %13 = load i8, ptr @is_board_focused, align 1, !tbaa !13, !range !15, !noundef !16
  %14 = trunc i8 %13 to i1
  br i1 %14, label %15, label %29

15:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  %16 = load ptr, ptr @win_board, align 8, !tbaa !5
  %17 = call i32 @wgetch(ptr noundef %16)
  store i32 %17, ptr %4, align 4, !tbaa !10
  %18 = load i32, ptr %4, align 4, !tbaa !10
  %19 = icmp eq i32 %18, 27
  br i1 %19, label %20, label %22

20:                                               ; preds = %15
  store i8 0, ptr @is_board_focused, align 1, !tbaa !13
  call void @switch_dialog_mode(i32 noundef 1)
  %21 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 144, ptr %5) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %5, ptr align 4 %21, i64 144, i1 false), !tbaa.struct !9
  call void @new_win_dialog(ptr noundef byval(%struct.Game) align 8 %5)
  call void @llvm.lifetime.end.p0(i64 144, ptr %5) #4
  br label %28

22:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %23 = load i32, ptr %4, align 4, !tbaa !10
  %24 = load ptr, ptr %2, align 8, !tbaa !5
  %25 = call i32 @board_driver(i32 noundef %23, ptr noundef %24)
  store i32 %25, ptr %6, align 4, !tbaa !10
  %26 = load ptr, ptr %2, align 8, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 144, ptr %7) #4
  call void @llvm.memcpy.p0.p0.i64(ptr align 8 %7, ptr align 4 %26, i64 144, i1 false), !tbaa.struct !9
  call void @handle_board_signal(ptr noundef byval(%struct.Game) align 8 %7, i32 noundef %27)
  call void @llvm.lifetime.end.p0(i64 144, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  br label %28

28:                                               ; preds = %22, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  br label %44

29:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  %30 = load ptr, ptr @win_dialog, align 8, !tbaa !5
  %31 = call i32 @wgetch(ptr noundef %30)
  store i32 %31, ptr %8, align 4, !tbaa !10
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  %32 = load i32, ptr %8, align 4, !tbaa !10
  %33 = load ptr, ptr %2, align 8, !tbaa !5
  %34 = call i32 @dialog_driver(i32 noundef %32, ptr noundef %33)
  store i32 %34, ptr %9, align 4, !tbaa !10
  %35 = load i32, ptr %9, align 4, !tbaa !10
  %36 = icmp eq i32 %35, 4
  br i1 %36, label %37, label %38

37:                                               ; preds = %29
  store i32 3, ptr %10, align 4
  br label %41

38:                                               ; preds = %29
  %39 = load ptr, ptr %2, align 8, !tbaa !5
  %40 = load i32, ptr %9, align 4, !tbaa !10
  call void @handle_dialog_signal(ptr noundef %39, i32 noundef %40)
  store i32 0, ptr %10, align 4
  br label %41

41:                                               ; preds = %38, %37
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  %42 = load i32, ptr %10, align 4
  switch i32 %42, label %46 [
    i32 0, label %43
    i32 3, label %45
  ]

43:                                               ; preds = %41
  br label %44

44:                                               ; preds = %43, %28
  br label %12

45:                                               ; preds = %41
  call void @teardown()
  ret void

46:                                               ; preds = %41
  unreachable
}

declare i32 @wgetch(ptr noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{i64 0, i64 4, !10, i64 4, i64 128, !12, i64 132, i64 4, !10, i64 136, i64 4, !10, i64 140, i64 4, !10}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!7, !7, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"_Bool", !7, i64 0}
!15 = !{i8 0, i8 2}
!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.Game = type { i32, [8 x [8 x i16]], i32, i32, i32 }

@win_board = external dso_local local_unnamed_addr global ptr, align 8
@win_dialog = external dso_local local_unnamed_addr global ptr, align 8
@is_board_focused = external dso_local local_unnamed_addr global i8, align 1

; 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: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

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

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

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

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @handle_board_signal(ptr noundef byval(%struct.Game) align 8, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local void @run_game_gui(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca %struct.Game, align 8
  %3 = alloca %struct.Game, align 8
  %4 = alloca %struct.Game, align 8
  call void @llvm.lifetime.start.p0(i64 144, ptr nonnull %2) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(144) %2, ptr noundef nonnull align 4 dereferenceable(144) %0, i64 144, i1 false), !tbaa.struct !5
  tail call void @setup(ptr noundef nonnull byval(%struct.Game) align 8 %2)
  call void @llvm.lifetime.end.p0(i64 144, ptr nonnull %2) #4
  br label %5

5:                                                ; preds = %.backedge, %1
  %6 = load i8, ptr @is_board_focused, align 1, !tbaa !11, !range !13, !noundef !14
  %7 = trunc nuw i8 %6 to i1
  br i1 %7, label %8, label %15

8:                                                ; preds = %5
  %9 = load ptr, ptr @win_board, align 8, !tbaa !15
  %10 = tail call i32 @wgetch(ptr noundef %9) #4
  %11 = icmp eq i32 %10, 27
  br i1 %11, label %12, label %13

12:                                               ; preds = %8
  store i8 0, ptr @is_board_focused, align 1, !tbaa !11
  tail call void @switch_dialog_mode(i32 noundef 1)
  call void @llvm.lifetime.start.p0(i64 144, ptr nonnull %3) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(144) %3, ptr noundef nonnull align 4 dereferenceable(144) %0, i64 144, i1 false), !tbaa.struct !5
  tail call void @new_win_dialog(ptr noundef nonnull byval(%struct.Game) align 8 %3)
  call void @llvm.lifetime.end.p0(i64 144, ptr nonnull %3) #4
  br label %.backedge

13:                                               ; preds = %8
  %14 = tail call i32 @board_driver(i32 noundef %10, ptr noundef %0)
  call void @llvm.lifetime.start.p0(i64 144, ptr nonnull %4) #4
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(144) %4, ptr noundef nonnull align 4 dereferenceable(144) %0, i64 144, i1 false), !tbaa.struct !5
  tail call void @handle_board_signal(ptr noundef nonnull byval(%struct.Game) align 8 %4, i32 noundef %14)
  call void @llvm.lifetime.end.p0(i64 144, ptr nonnull %4) #4
  br label %.backedge

15:                                               ; preds = %5
  %16 = load ptr, ptr @win_dialog, align 8, !tbaa !15
  %17 = tail call i32 @wgetch(ptr noundef %16) #4
  %18 = tail call i32 @dialog_driver(i32 noundef %17, ptr noundef %0)
  %19 = icmp eq i32 %18, 4
  br i1 %19, label %20, label %.thread

.thread:                                          ; preds = %15
  tail call void @handle_dialog_signal(ptr noundef %0, i32 noundef %18)
  br label %.backedge

.backedge:                                        ; preds = %.thread, %12, %13
  br label %5

20:                                               ; preds = %15
  tail call void @teardown()
  ret void
}

declare i32 @wgetch(ptr noundef) local_unnamed_addr #3

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = { 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 = !{i64 0, i64 4, !6, i64 4, i64 128, !10, i64 132, i64 4, !6, i64 136, i64 4, !6, i64 140, i64 4, !6}
!6 = !{!7, !7, i64 0}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!8, !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"_Bool", !8, i64 0}
!13 = !{i8 0, i8 2}
!14 = !{}
!15 = !{!16, !16, i64 0}
!16 = !{!"any pointer", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass