; LLVM IR from tangjz/acm-icpc :: codeforces/gym/103957D.ll (Z4readRii)

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

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z4readRii(ptr noundef nonnull align 4 dereferenceable(4) %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca [7 x i8], align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.start.p0(i64 7, ptr %7) #4
  %8 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 0
  %9 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str, ptr noundef %8)
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %10

10:                                               ; preds = %26, %2
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %12
  %14 = load i8, ptr %13, align 1, !tbaa !11
  %15 = icmp ne i8 %14, 0
  br i1 %15, label %16, label %23

16:                                               ; preds = %10
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp ne i32 %21, 46
  br label %23

23:                                               ; preds = %16, %10
  %24 = phi i1 [ false, %10 ], [ %22, %16 ]
  br i1 %24, label %25, label %29

25:                                               ; preds = %23
  br label %26

26:                                               ; preds = %25
  %27 = load i32, ptr %5, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %5, align 4, !tbaa !9
  br label %10, !llvm.loop !12

29:                                               ; preds = %23
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = load i32, ptr %5, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !11
  %35 = sext i8 %34 to i32
  %36 = icmp eq i32 %35, 46
  %37 = zext i1 %36 to i32
  %38 = add nsw i32 %30, %37
  store i32 %38, ptr %6, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %51, %29
  %40 = load i32, ptr %4, align 4, !tbaa !9
  %41 = icmp sgt i32 %40, 0
  br i1 %41, label %42, label %48

42:                                               ; preds = %39
  %43 = load i32, ptr %6, align 4, !tbaa !9
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %44
  %46 = load i8, ptr %45, align 1, !tbaa !11
  %47 = icmp ne i8 %46, 0
  br label %48

48:                                               ; preds = %42, %39
  %49 = phi i1 [ false, %39 ], [ %47, %42 ]
  br i1 %49, label %50, label %63

50:                                               ; preds = %48
  br label %51

51:                                               ; preds = %50
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %6, align 4, !tbaa !9
  %54 = sext i32 %52 to i64
  %55 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %54
  %56 = load i8, ptr %55, align 1, !tbaa !11
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %5, align 4, !tbaa !9
  %59 = sext i32 %57 to i64
  %60 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %59
  store i8 %56, ptr %60, align 1, !tbaa !11
  %61 = load i32, ptr %4, align 4, !tbaa !9
  %62 = add nsw i32 %61, -1
  store i32 %62, ptr %4, align 4, !tbaa !9
  br label %39, !llvm.loop !14

63:                                               ; preds = %48
  br label %64

64:                                               ; preds = %68, %63
  %65 = load i32, ptr %4, align 4, !tbaa !9
  %66 = icmp sgt i32 %65, 0
  br i1 %66, label %67, label %75

67:                                               ; preds = %64
  br label %68

68:                                               ; preds = %67
  %69 = load i32, ptr %5, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %5, align 4, !tbaa !9
  %71 = sext i32 %69 to i64
  %72 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %71
  store i8 48, ptr %72, align 1, !tbaa !11
  %73 = load i32, ptr %4, align 4, !tbaa !9
  %74 = add nsw i32 %73, -1
  store i32 %74, ptr %4, align 4, !tbaa !9
  br label %64, !llvm.loop !15

75:                                               ; preds = %64
  %76 = load i32, ptr %5, align 4, !tbaa !9
  %77 = sext i32 %76 to i64
  %78 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 %77
  store i8 0, ptr %78, align 1, !tbaa !11
  %79 = getelementptr inbounds [7 x i8], ptr %7, i64 0, i64 0
  %80 = load ptr, ptr %3, align 8, !tbaa !5
  %81 = call i32 (ptr, ptr, ...) @__isoc99_sscanf(ptr noundef %79, ptr noundef @.str.1, ptr noundef %80) #4
  call void @llvm.lifetime.end.p0(i64 7, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  ret void
}

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

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

; Function Attrs: nounwind
declare i32 @__isoc99_sscanf(ptr noundef, ptr noundef, ...) #3

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


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

@.str = external hidden unnamed_addr constant [3 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z4readRii(ptr noundef nonnull align 4 dereferenceable(4) %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca [7 x i8], align 1
  call void @llvm.lifetime.start.p0(i64 7, ptr nonnull %3) #4
  %4 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %3)
  br label %5

5:                                                ; preds = %8, %2
  %indvars.iv42 = phi i32 [ %indvars.iv.next43, %8 ], [ %1, %2 ]
  %indvars.iv36 = phi i32 [ %indvars.iv.next37, %8 ], [ 0, %2 ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %8 ], [ 0, %2 ]
  %6 = getelementptr inbounds [7 x i8], ptr %3, i64 0, i64 %indvars.iv
  %7 = load i8, ptr %6, align 1, !tbaa !5
  switch i8 %7, label %8 [
    i8 0, label %.critedge
    i8 46, label %.critedge
  ]

8:                                                ; preds = %5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %indvars.iv.next37 = add nuw i32 %indvars.iv36, 1
  %indvars.iv.next43 = add i32 %indvars.iv42, 1
  br label %5, !llvm.loop !8

.critedge:                                        ; preds = %5, %5
  %9 = trunc nuw nsw i64 %indvars.iv to i32
  %10 = icmp sgt i32 %1, 0
  br i1 %10, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.critedge
  %11 = icmp eq i8 %7, 46
  %12 = zext i1 %11 to i32
  %13 = add i32 %indvars.iv36, %12
  %14 = zext i32 %13 to i64
  %wide.trip.count = zext i32 %indvars.iv42 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %17
  %indvars.iv40 = phi i64 [ %indvars.iv, %.lr.ph.preheader ], [ %indvars.iv.next41, %17 ]
  %indvars.iv38 = phi i64 [ %14, %.lr.ph.preheader ], [ %indvars.iv.next39, %17 ]
  %.01723 = phi i32 [ %1, %.lr.ph.preheader ], [ %19, %17 ]
  %15 = getelementptr inbounds [7 x i8], ptr %3, i64 0, i64 %indvars.iv38
  %16 = load i8, ptr %15, align 1, !tbaa !5
  %.not22 = icmp eq i8 %16, 0
  br i1 %.not22, label %.critedge2, label %17

17:                                               ; preds = %.lr.ph
  %indvars.iv.next39 = add nuw nsw i64 %indvars.iv38, 1
  %indvars.iv.next41 = add nuw i64 %indvars.iv40, 1
  %18 = getelementptr inbounds [7 x i8], ptr %3, i64 0, i64 %indvars.iv40
  store i8 %16, ptr %18, align 1, !tbaa !5
  %19 = add nsw i32 %.01723, -1
  %exitcond.not = icmp eq i64 %indvars.iv.next41, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

.critedge2:                                       ; preds = %.lr.ph
  %20 = trunc nuw nsw i64 %indvars.iv40 to i32
  %21 = icmp sgt i32 %.01723, 0
  br i1 %21, label %.lr.ph32.preheader, label %._crit_edge

.lr.ph32.preheader:                               ; preds = %.critedge2
  %22 = and i64 %indvars.iv40, 4294967295
  %scevgep = getelementptr i8, ptr %3, i64 %22
  %23 = zext nneg i32 %.01723 to i64
  call void @llvm.memset.p0.i64(ptr align 1 %scevgep, i8 48, i64 %23, i1 false), !tbaa !5
  %24 = add i32 %.01723, %20
  br label %._crit_edge

._crit_edge:                                      ; preds = %17, %.critedge, %.lr.ph32.preheader, %.critedge2
  %.2.lcssa = phi i32 [ %20, %.critedge2 ], [ %24, %.lr.ph32.preheader ], [ %9, %.critedge ], [ %indvars.iv42, %17 ]
  %25 = zext nneg i32 %.2.lcssa to i64
  %26 = getelementptr inbounds [7 x i8], ptr %3, i64 0, i64 %25
  store i8 0, ptr %26, align 1, !tbaa !5
  %27 = call i32 (ptr, ptr, ...) @__isoc99_sscanf(ptr noundef nonnull %3, ptr noundef nonnull @.str.1, ptr noundef nonnull %0) #4
  call void @llvm.lifetime.end.p0(i64 7, ptr nonnull %3) #4
  ret void
}

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

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

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

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

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

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


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass