; LLVM IR from bqi343/cp-notebook :: Contests/USACO Solutions/2012-13/Jan 2013/13 Jan G2.ll (Z4fillii)

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

@R = external dso_local global i32, align 4
@C = external dso_local global i32, align 4
@num = external dso_local global i32, align 4
@xdir = external dso_local global [4 x i32], align 16
@ydir = external dso_local global [4 x i32], align 16
@grid = external dso_local global [50 x [50 x i8]], align 16

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4fillii(i32 noundef %0, i32 noundef %1) #0 {
  %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 %3, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 0
  br i1 %7, label %19, label %8

8:                                                ; preds = %2
  %9 = load i32, ptr %3, align 4, !tbaa !5
  %10 = load i32, ptr @R, align 4, !tbaa !5
  %11 = icmp sge i32 %9, %10
  br i1 %11, label %19, label %12

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

15:                                               ; preds = %12
  %16 = load i32, ptr %4, align 4, !tbaa !5
  %17 = load i32, ptr @C, align 4, !tbaa !5
  %18 = icmp sge i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %15, %12, %8, %2
  br label %61

20:                                               ; preds = %15
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [50 x [50 x i8]], ptr @grid, i64 0, i64 %22
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [50 x i8], ptr %23, i64 0, i64 %25
  %27 = load i8, ptr %26, align 1, !tbaa !9
  %28 = sext i8 %27 to i32
  %29 = icmp ne i32 %28, 88
  br i1 %29, label %30, label %31

30:                                               ; preds = %20
  br label %61

31:                                               ; preds = %20
  %32 = load i32, ptr @num, align 4, !tbaa !5
  %33 = add nsw i32 97, %32
  %34 = trunc i32 %33 to i8
  %35 = load i32, ptr %3, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [50 x [50 x i8]], ptr @grid, i64 0, i64 %36
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [50 x i8], ptr %37, i64 0, i64 %39
  store i8 %34, ptr %40, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %41

41:                                               ; preds = %58, %31
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = icmp slt i32 %42, 4
  br i1 %43, label %45, label %44

44:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  br label %61

45:                                               ; preds = %41
  %46 = load i32, ptr %3, align 4, !tbaa !5
  %47 = load i32, ptr %5, align 4, !tbaa !5
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [4 x i32], ptr @xdir, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !5
  %51 = add nsw i32 %46, %50
  %52 = load i32, ptr %4, align 4, !tbaa !5
  %53 = load i32, ptr %5, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [4 x i32], ptr @ydir, i64 0, i64 %54
  %56 = load i32, ptr %55, align 4, !tbaa !5
  %57 = add nsw i32 %52, %56
  call void @_Z4fillii(i32 noundef %51, i32 noundef %57)
  br label %58

58:                                               ; preds = %45
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %5, align 4, !tbaa !5
  br label %41, !llvm.loop !10

61:                                               ; preds = %44, %30, %19
  ret void
}

; 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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"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"

@R = external dso_local local_unnamed_addr global i32, align 4
@C = external dso_local local_unnamed_addr global i32, align 4
@num = external dso_local local_unnamed_addr global i32, align 4
@xdir = external dso_local local_unnamed_addr global [4 x i32], align 16
@ydir = external dso_local local_unnamed_addr global [4 x i32], align 16
@grid = external dso_local local_unnamed_addr global [50 x [50 x i8]], align 16

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z4fillii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %.loopexit.loopexit, %2
  %.tr = phi i32 [ %0, %2 ], [ %30, %.loopexit.loopexit ]
  %.tr24 = phi i32 [ %1, %2 ], [ %32, %.loopexit.loopexit ]
  %3 = icmp slt i32 %.tr, 0
  br i1 %3, label %.loopexit, label %4

4:                                                ; preds = %tailrecurse
  %5 = load i32, ptr @R, align 4, !tbaa !5
  %6 = icmp sgt i32 %5, %.tr
  %7 = icmp sgt i32 %.tr24, -1
  %or.cond.not21 = and i1 %7, %6
  %8 = load i32, ptr @C, align 4
  %.not = icmp sgt i32 %8, %.tr24
  %or.cond18 = select i1 %or.cond.not21, i1 %.not, i1 false
  br i1 %or.cond18, label %9, label %.loopexit

9:                                                ; preds = %4
  %10 = zext nneg i32 %.tr to i64
  %11 = zext nneg i32 %.tr24 to i64
  %12 = getelementptr inbounds [50 x [50 x i8]], ptr @grid, i64 0, i64 %10, i64 %11
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %.not17 = icmp eq i8 %13, 88
  br i1 %.not17, label %.loopexit.loopexit, label %.loopexit

.loopexit.loopexit:                               ; preds = %9
  %14 = load i32, ptr @num, align 4, !tbaa !5
  %15 = trunc i32 %14 to i8
  %16 = add i8 %15, 97
  store i8 %16, ptr %12, align 1, !tbaa !9
  %17 = load i32, ptr @xdir, align 16, !tbaa !5
  %18 = add nsw i32 %17, %.tr
  %19 = load i32, ptr @ydir, align 16, !tbaa !5
  %20 = add nsw i32 %19, %.tr24
  tail call void @_Z4fillii(i32 noundef %18, i32 noundef %20)
  %21 = load i32, ptr getelementptr inbounds (i8, ptr @xdir, i64 4), align 4, !tbaa !5
  %22 = add nsw i32 %21, %.tr
  %23 = load i32, ptr getelementptr inbounds (i8, ptr @ydir, i64 4), align 4, !tbaa !5
  %24 = add nsw i32 %23, %.tr24
  tail call void @_Z4fillii(i32 noundef %22, i32 noundef %24)
  %25 = load i32, ptr getelementptr inbounds (i8, ptr @xdir, i64 8), align 8, !tbaa !5
  %26 = add nsw i32 %25, %.tr
  %27 = load i32, ptr getelementptr inbounds (i8, ptr @ydir, i64 8), align 8, !tbaa !5
  %28 = add nsw i32 %27, %.tr24
  tail call void @_Z4fillii(i32 noundef %26, i32 noundef %28)
  %29 = load i32, ptr getelementptr inbounds (i8, ptr @xdir, i64 12), align 4, !tbaa !5
  %30 = add nsw i32 %29, %.tr
  %31 = load i32, ptr getelementptr inbounds (i8, ptr @ydir, i64 12), align 4, !tbaa !5
  %32 = add nsw i32 %31, %.tr24
  br label %tailrecurse

.loopexit:                                        ; preds = %9, %tailrecurse, %4
  ret void
}

attributes #0 = { mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!7, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LoopRotatePass,LoopSimplifyPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass