; LLVM IR from antirez/ttt-rl :: ttt (play_random_game)

; 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.GameState = type { [9 x i8], i32 }

; 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: nounwind uwtable
declare dso_local void @init_game(ptr noundef) #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @learn_from_game(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i8 noundef signext) #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @get_random_move(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local signext i8 @play_random_game(ptr noundef %0, ptr noundef %1, ptr noundef %2) #1 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca %struct.GameState, align 4
  %8 = alloca i8, align 1
  %9 = alloca i32, align 4
  %10 = alloca i8, align 1
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr %8) #2
  store i8 0, ptr %8, align 1, !tbaa !9
  %11 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %11, align 4, !tbaa !10
  call void @init_game(ptr noundef %7)
  br label %12

12:                                               ; preds = %25, %3
  %13 = call i32 @check_game_over(ptr noundef %7, ptr noundef %8)
  %14 = icmp ne i32 %13, 0
  %15 = xor i1 %14, true
  br i1 %15, label %16, label %50

16:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %17 = getelementptr inbounds %struct.GameState, ptr %7, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %20, label %22

20:                                               ; preds = %16
  %21 = call i32 @get_random_move(ptr noundef %7)
  store i32 %21, ptr %9, align 4, !tbaa !10
  br label %25

22:                                               ; preds = %16
  %23 = load ptr, ptr %4, align 8, !tbaa !5
  %24 = call i32 @get_computer_move(ptr noundef %7, ptr noundef %23, i32 noundef 0)
  store i32 %24, ptr %9, align 4, !tbaa !10
  br label %25

25:                                               ; preds = %22, %20
  call void @llvm.lifetime.start.p0(i64 1, ptr %10) #2
  %26 = getelementptr inbounds %struct.GameState, ptr %7, i32 0, i32 1
  %27 = load i32, ptr %26, align 4, !tbaa !12
  %28 = icmp eq i32 %27, 0
  %29 = zext i1 %28 to i64
  %30 = select i1 %28, i32 88, i32 79
  %31 = trunc i32 %30 to i8
  store i8 %31, ptr %10, align 1, !tbaa !9
  %32 = load i8, ptr %10, align 1, !tbaa !9
  %33 = getelementptr inbounds %struct.GameState, ptr %7, i32 0, i32 0
  %34 = load i32, ptr %9, align 4, !tbaa !10
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [9 x i8], ptr %33, i64 0, i64 %35
  store i8 %32, ptr %36, align 1, !tbaa !9
  %37 = load i32, ptr %9, align 4, !tbaa !10
  %38 = load ptr, ptr %5, align 8, !tbaa !5
  %39 = load ptr, ptr %6, align 8, !tbaa !5
  %40 = load i32, ptr %39, align 4, !tbaa !10
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %39, align 4, !tbaa !10
  %42 = sext i32 %40 to i64
  %43 = getelementptr inbounds i32, ptr %38, i64 %42
  store i32 %37, ptr %43, align 4, !tbaa !10
  %44 = getelementptr inbounds %struct.GameState, ptr %7, i32 0, i32 1
  %45 = load i32, ptr %44, align 4, !tbaa !12
  %46 = icmp ne i32 %45, 0
  %47 = xor i1 %46, true
  %48 = zext i1 %47 to i32
  %49 = getelementptr inbounds %struct.GameState, ptr %7, i32 0, i32 1
  store i32 %48, ptr %49, align 4, !tbaa !12
  call void @llvm.lifetime.end.p0(i64 1, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %12, !llvm.loop !14

50:                                               ; preds = %12
  %51 = load ptr, ptr %4, align 8, !tbaa !5
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load i32, ptr %53, align 4, !tbaa !10
  %55 = load i8, ptr %8, align 1, !tbaa !9
  call void @learn_from_game(ptr noundef %51, ptr noundef %52, i32 noundef %54, i32 noundef 1, i8 noundef signext %55)
  %56 = load i8, ptr %8, align 1, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr %7) #2
  ret i8 %56
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = !{!13, !11, i64 12}
!13 = !{!"", !7, i64 0, !11, i64 12}
!14 = distinct !{!14, !15}
!15 = !{!"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"

%struct.GameState = type { [9 x i8], i32 }

; 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: nounwind uwtable
declare dso_local void @init_game(ptr noundef) local_unnamed_addr #1

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

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

; Function Attrs: nounwind uwtable
declare dso_local void @learn_from_game(ptr noundef, ptr noundef, i32 noundef, i32 noundef, i8 noundef signext) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local i32 @get_random_move(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local signext i8 @play_random_game(ptr noundef %0, ptr noundef %1, ptr nocapture noundef %2) local_unnamed_addr #1 {
  %4 = alloca %struct.GameState, align 4
  %5 = alloca i8, align 1
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %4) #2
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %5) #2
  store i8 0, ptr %5, align 1, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !8
  call void @init_game(ptr noundef nonnull %4)
  %6 = call i32 @check_game_over(ptr noundef nonnull %4, ptr noundef nonnull %5)
  %.not10 = icmp eq i32 %6, 0
  br i1 %.not10, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %3
  %7 = getelementptr inbounds i8, ptr %4, i64 12
  br label %8

8:                                                ; preds = %.lr.ph, %15
  %9 = load i32, ptr %7, align 4, !tbaa !10
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %11, label %13

11:                                               ; preds = %8
  %12 = call i32 @get_random_move(ptr noundef nonnull %4)
  br label %15

13:                                               ; preds = %8
  %14 = call i32 @get_computer_move(ptr noundef nonnull %4, ptr noundef %0, i32 noundef 0)
  br label %15

15:                                               ; preds = %13, %11
  %.0 = phi i32 [ %12, %11 ], [ %14, %13 ]
  %16 = load i32, ptr %7, align 4, !tbaa !10
  %17 = icmp eq i32 %16, 0
  %18 = select i1 %17, i8 88, i8 79
  %19 = sext i32 %.0 to i64
  %20 = getelementptr inbounds [9 x i8], ptr %4, i64 0, i64 %19
  store i8 %18, ptr %20, align 1, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !8
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %2, align 4, !tbaa !8
  %23 = sext i32 %21 to i64
  %24 = getelementptr inbounds i32, ptr %1, i64 %23
  store i32 %.0, ptr %24, align 4, !tbaa !8
  %25 = load i32, ptr %7, align 4, !tbaa !10
  %.not9 = icmp eq i32 %25, 0
  %26 = zext i1 %.not9 to i32
  store i32 %26, ptr %7, align 4, !tbaa !10
  %27 = call i32 @check_game_over(ptr noundef nonnull %4, ptr noundef nonnull %5)
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %8, label %._crit_edge, !llvm.loop !12

._crit_edge:                                      ; preds = %15, %3
  %28 = load i32, ptr %2, align 4, !tbaa !8
  %29 = load i8, ptr %5, align 1, !tbaa !5
  call void @learn_from_game(ptr noundef %0, ptr noundef %1, i32 noundef %28, i32 noundef 1, i8 noundef signext %29)
  %30 = load i8, ptr %5, align 1, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %5) #2
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %4) #2
  ret i8 %30
}

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


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass