; LLVM IR from tangjz/acm-icpc :: usaco/Training/Chapter 2 Bigger challenges (holstein)

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

@v = external dso_local global i32, align 4
@goal = external dso_local global [25 x i32], align 16
@g = external dso_local global i32, align 4
@num = external dso_local global [15 x [25 x i32]], align 16
@ans = external dso_local global i32, align 4
@output = external dso_local global [15 x i32], align 16
@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsii(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 norecurse uwtable
define dso_local noundef i32 @main() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %4 = load ptr, ptr @stdin, align 8, !tbaa !5
  %5 = call ptr @freopen(ptr noundef @.str, ptr noundef @.str.1, ptr noundef %4)
  %6 = load ptr, ptr @stdout, align 8, !tbaa !5
  %7 = call ptr @freopen(ptr noundef @.str.2, ptr noundef @.str.3, ptr noundef %6)
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #5
  %8 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @v)
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %18, %0
  %10 = load i32, ptr %2, align 4, !tbaa !9
  %11 = load i32, ptr @v, align 4, !tbaa !9
  %12 = icmp slt i32 %10, %11
  br i1 %12, label %13, label %21

13:                                               ; preds = %9
  %14 = load i32, ptr %2, align 4, !tbaa !9
  %15 = sext i32 %14 to i64
  %16 = getelementptr inbounds [25 x i32], ptr @goal, i64 0, i64 %15
  %17 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef %16)
  br label %18

18:                                               ; preds = %13
  %19 = load i32, ptr %2, align 4, !tbaa !9
  %20 = add nsw i32 %19, 1
  store i32 %20, ptr %2, align 4, !tbaa !9
  br label %9, !llvm.loop !11

21:                                               ; preds = %9
  %22 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef @g)
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %23

23:                                               ; preds = %44, %21
  %24 = load i32, ptr %2, align 4, !tbaa !9
  %25 = load i32, ptr @g, align 4, !tbaa !9
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %47

27:                                               ; preds = %23
  store i32 0, ptr %3, align 4, !tbaa !9
  br label %28

28:                                               ; preds = %40, %27
  %29 = load i32, ptr %3, align 4, !tbaa !9
  %30 = load i32, ptr @v, align 4, !tbaa !9
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %43

32:                                               ; preds = %28
  %33 = load i32, ptr %2, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds [15 x [25 x i32]], ptr @num, i64 0, i64 %34
  %36 = load i32, ptr %3, align 4, !tbaa !9
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [25 x i32], ptr %35, i64 0, i64 %37
  %39 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.4, ptr noundef %38)
  br label %40

40:                                               ; preds = %32
  %41 = load i32, ptr %3, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %3, align 4, !tbaa !9
  br label %28, !llvm.loop !13

43:                                               ; preds = %28
  br label %44

44:                                               ; preds = %43
  %45 = load i32, ptr %2, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr %2, align 4, !tbaa !9
  br label %23, !llvm.loop !14

47:                                               ; preds = %23
  %48 = load i32, ptr @g, align 4, !tbaa !9
  store i32 %48, ptr @ans, align 4, !tbaa !9
  call void @_Z3dfsii(i32 noundef 1, i32 noundef 0)
  %49 = load i32, ptr @ans, align 4, !tbaa !9
  %50 = call i32 (ptr, ...) @printf(ptr noundef @.str.4, i32 noundef %49)
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %61, %47
  %52 = load i32, ptr %2, align 4, !tbaa !9
  %53 = load i32, ptr @ans, align 4, !tbaa !9
  %54 = icmp slt i32 %52, %53
  br i1 %54, label %55, label %64

55:                                               ; preds = %51
  %56 = load i32, ptr %2, align 4, !tbaa !9
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds [15 x i32], ptr @output, i64 0, i64 %57
  %59 = load i32, ptr %58, align 4, !tbaa !9
  %60 = call i32 (ptr, ...) @printf(ptr noundef @.str.5, i32 noundef %59)
  br label %61

61:                                               ; preds = %55
  %62 = load i32, ptr %2, align 4, !tbaa !9
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %2, align 4, !tbaa !9
  br label %51, !llvm.loop !15

64:                                               ; preds = %51
  %65 = call i32 @putchar(i32 noundef 10)
  %66 = load ptr, ptr @stdin, align 8, !tbaa !5
  %67 = call i32 @fclose(ptr noundef %66)
  %68 = load ptr, ptr @stdout, align 8, !tbaa !5
  %69 = call i32 @fclose(ptr noundef %68)
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #5
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) #3

declare i32 @__isoc99_scanf(ptr noundef, ...) #3

declare i32 @printf(ptr noundef, ...) #3

; Function Attrs: inlinehint mustprogress uwtable
declare i32 @putchar(i32 noundef) #4

declare i32 @fclose(ptr noundef) #3

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 = { mustprogress norecurse 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 = { inlinehint 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 #5 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = distinct !{!15, !12}


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

@v = external dso_local global i32, align 4
@goal = external dso_local global [25 x i32], align 16
@g = external dso_local global i32, align 4
@num = external dso_local global [15 x [25 x i32]], align 16
@ans = external dso_local local_unnamed_addr global i32, align 4
@output = external dso_local local_unnamed_addr global [15 x i32], align 16
@.str = external hidden unnamed_addr constant [12 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1
@stdin = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [13 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [2 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.4 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress uwtable
declare dso_local void @_Z3dfsii(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress norecurse uwtable
define dso_local noundef i32 @main() local_unnamed_addr #1 {
  %1 = load ptr, ptr @stdin, align 8, !tbaa !5
  %2 = tail call ptr @freopen(ptr noundef nonnull @.str, ptr noundef nonnull @.str.1, ptr noundef %1)
  %3 = load ptr, ptr @stdout, align 8, !tbaa !5
  %4 = tail call ptr @freopen(ptr noundef nonnull @.str.2, ptr noundef nonnull @.str.3, ptr noundef %3)
  %5 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @v)
  %6 = load i32, ptr @v, align 4, !tbaa !9
  %7 = icmp sgt i32 %6, 0
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %0 ]
  %8 = getelementptr inbounds [25 x i32], ptr @goal, i64 0, i64 %indvars.iv
  %9 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull %8)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %10 = load i32, ptr @v, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = icmp slt i64 %indvars.iv.next, %11
  br i1 %12, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph, %0
  %13 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull @g)
  %14 = load i32, ptr @g, align 4, !tbaa !9
  %15 = icmp sgt i32 %14, 0
  %16 = load i32, ptr @v, align 4
  %17 = icmp sgt i32 %16, 0
  %or.cond = select i1 %15, i1 %17, i1 false
  br i1 %or.cond, label %.preheader, label %._crit_edge19

.preheader:                                       ; preds = %._crit_edge, %._crit_edge17
  %18 = phi i32 [ %26, %._crit_edge17 ], [ %14, %._crit_edge ]
  %19 = phi i32 [ %27, %._crit_edge17 ], [ %16, %._crit_edge ]
  %indvars.iv30 = phi i64 [ %indvars.iv.next31, %._crit_edge17 ], [ 0, %._crit_edge ]
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.lr.ph16, label %._crit_edge17

.lr.ph16:                                         ; preds = %.preheader, %.lr.ph16
  %indvars.iv27 = phi i64 [ %indvars.iv.next28, %.lr.ph16 ], [ 0, %.preheader ]
  %21 = getelementptr inbounds [15 x [25 x i32]], ptr @num, i64 0, i64 %indvars.iv30, i64 %indvars.iv27
  %22 = tail call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.4, ptr noundef nonnull %21)
  %indvars.iv.next28 = add nuw nsw i64 %indvars.iv27, 1
  %23 = load i32, ptr @v, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = icmp slt i64 %indvars.iv.next28, %24
  br i1 %25, label %.lr.ph16, label %._crit_edge17.loopexit, !llvm.loop !13

._crit_edge17.loopexit:                           ; preds = %.lr.ph16
  %.pre = load i32, ptr @g, align 4, !tbaa !9
  br label %._crit_edge17

._crit_edge17:                                    ; preds = %._crit_edge17.loopexit, %.preheader
  %26 = phi i32 [ %.pre, %._crit_edge17.loopexit ], [ %18, %.preheader ]
  %27 = phi i32 [ %23, %._crit_edge17.loopexit ], [ %19, %.preheader ]
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %28 = sext i32 %26 to i64
  %29 = icmp slt i64 %indvars.iv.next31, %28
  br i1 %29, label %.preheader, label %._crit_edge19, !llvm.loop !14

._crit_edge19:                                    ; preds = %._crit_edge17, %._crit_edge
  %.lcssa = phi i32 [ %14, %._crit_edge ], [ %26, %._crit_edge17 ]
  store i32 %.lcssa, ptr @ans, align 4, !tbaa !9
  tail call void @_Z3dfsii(i32 noundef 1, i32 noundef 0)
  %30 = load i32, ptr @ans, align 4, !tbaa !9
  %31 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %30)
  %32 = load i32, ptr @ans, align 4, !tbaa !9
  %33 = icmp sgt i32 %32, 0
  br i1 %33, label %.lr.ph22, label %._crit_edge23

.lr.ph22:                                         ; preds = %._crit_edge19, %.lr.ph22
  %indvars.iv33 = phi i64 [ %indvars.iv.next34, %.lr.ph22 ], [ 0, %._crit_edge19 ]
  %34 = getelementptr inbounds [15 x i32], ptr @output, i64 0, i64 %indvars.iv33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5, i32 noundef %35)
  %indvars.iv.next34 = add nuw nsw i64 %indvars.iv33, 1
  %37 = load i32, ptr @ans, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = icmp slt i64 %indvars.iv.next34, %38
  br i1 %39, label %.lr.ph22, label %._crit_edge23, !llvm.loop !16

._crit_edge23:                                    ; preds = %.lr.ph22, %._crit_edge19
  %40 = tail call i32 @putchar(i32 noundef 10)
  %41 = load ptr, ptr @stdin, align 8, !tbaa !5
  %42 = tail call i32 @fclose(ptr noundef %41)
  %43 = load ptr, ptr @stdout, align 8, !tbaa !5
  %44 = tail call i32 @fclose(ptr noundef %43)
  ret i32 0
}

declare ptr @freopen(ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #2

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

; Function Attrs: inlinehint mustprogress nofree nounwind uwtable
declare noundef i32 @putchar(i32 noundef) local_unnamed_addr #4

; Function Attrs: nofree nounwind
declare noundef i32 @fclose(ptr nocapture noundef) local_unnamed_addr #3

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 = { mustprogress norecurse 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 = { "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 = { nofree nounwind "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 = { inlinehint mustprogress nofree 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12, !15}
!15 = !{!"llvm.loop.unswitch.partial.disable"}
!16 = distinct !{!16, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimpleLoopUnswitchPass,SimplifyCFGPass,TailCallElimPass