; LLVM IR from Mohammed-Shoaib/Coding-Problems :: HackerRank/C/Medium (M0005)

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

@stdin = external global ptr, align 8
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca [10 x i32], align 16
  %3 = alloca [1000 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 40, ptr %2) #6
  call void @llvm.memset.p0.i64(ptr align 16 %2, i8 0, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 1000, ptr %3) #6
  %6 = getelementptr inbounds [1000 x i8], ptr %3, i64 0, i64 0
  %7 = load ptr, ptr @stdin, align 8, !tbaa !5
  %8 = call ptr @fgets(ptr noundef %6, i32 noundef 1000, ptr noundef %7)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %42, %0
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [1000 x i8], ptr %3, i64 0, i64 0
  %13 = call i64 @strlen(ptr noundef %12) #7
  %14 = icmp ult i64 %11, %13
  br i1 %14, label %16, label %15

15:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  br label %45

16:                                               ; preds = %9
  %17 = call ptr @__ctype_b_loc() #8
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [1000 x i8], ptr %3, i64 0, i64 %20
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = sext i8 %22 to i32
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i16, ptr %18, i64 %24
  %26 = load i16, ptr %25, align 2, !tbaa !12
  %27 = zext i16 %26 to i32
  %28 = and i32 %27, 2048
  %29 = icmp ne i32 %28, 0
  br i1 %29, label %30, label %41

30:                                               ; preds = %16
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [1000 x i8], ptr %3, i64 0, i64 %32
  %34 = load i8, ptr %33, align 1, !tbaa !11
  %35 = sext i8 %34 to i32
  %36 = sub nsw i32 %35, 48
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %37
  %39 = load i32, ptr %38, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %38, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %30, %16
  br label %42

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

45:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #6
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %46

46:                                               ; preds = %56, %45
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = icmp slt i32 %47, 10
  br i1 %48, label %50, label %49

49:                                               ; preds = %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #6
  br label %59

50:                                               ; preds = %46
  %51 = load i32, ptr %5, align 4, !tbaa !9
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds [10 x i32], ptr %2, i64 0, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !9
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %54)
  br label %56

56:                                               ; preds = %50
  %57 = load i32, ptr %5, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %5, align 4, !tbaa !9
  br label %46, !llvm.loop !16

59:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 1000, ptr %3) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr %2) #6
  ret i32 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 nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

declare ptr @fgets(ptr noundef, i32 noundef, ptr noundef) #3

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() #5

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

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

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 willreturn memory(read) "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 willreturn memory(none) "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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }
attributes #8 = { nounwind willreturn memory(none) }

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


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

@stdin = external local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: nofree nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca [10 x i32], align 16
  %2 = alloca [1000 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 40, ptr nonnull %1) #6
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(40) %1, i8 0, i64 40, i1 false)
  call void @llvm.lifetime.start.p0(i64 1000, ptr nonnull %2) #6
  %3 = load ptr, ptr @stdin, align 8, !tbaa !5
  %4 = call ptr @fgets(ptr noundef nonnull %2, i32 noundef 1000, ptr noundef %3)
  %5 = call i64 @strlen(ptr noundef nonnull dereferenceable(1) %2) #7
  %.not11 = icmp eq i64 %5, 0
  br i1 %.not11, label %.preheader, label %.lr.ph

.lr.ph:                                           ; preds = %0
  %6 = tail call ptr @__ctype_b_loc() #8
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %xtraiter = and i64 %5, 1
  %8 = icmp eq i64 %5, 1
  br i1 %8, label %.preheader.loopexit.unr-lcssa, label %.lr.ph.new

.lr.ph.new:                                       ; preds = %.lr.ph
  %unroll_iter = and i64 %5, -2
  br label %40

.preheader.loopexit.unr-lcssa:                    ; preds = %64, %.lr.ph
  %indvars.iv.unr = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next.1, %64 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader.loopexit, label %.epil.preheader

.epil.preheader:                                  ; preds = %.preheader.loopexit.unr-lcssa
  %9 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %indvars.iv.unr
  %10 = load i8, ptr %9, align 1, !tbaa !9
  %11 = sext i8 %10 to i64
  %12 = getelementptr inbounds i16, ptr %7, i64 %11
  %13 = load i16, ptr %12, align 2, !tbaa !10
  %14 = and i16 %13, 2048
  %.not.epil = icmp eq i16 %14, 0
  br i1 %.not.epil, label %.preheader.loopexit, label %15

15:                                               ; preds = %.epil.preheader
  %16 = add nsw i64 %11, -48
  %17 = getelementptr inbounds [10 x i32], ptr %1, i64 0, i64 %16
  %18 = load i32, ptr %17, align 4, !tbaa !12
  %19 = add nsw i32 %18, 1
  store i32 %19, ptr %17, align 4, !tbaa !12
  br label %.preheader.loopexit

.preheader.loopexit:                              ; preds = %.epil.preheader, %15, %.preheader.loopexit.unr-lcssa
  %.pre = load i32, ptr %1, align 16, !tbaa !12
  %.phi.trans.insert = getelementptr inbounds i8, ptr %1, i64 4
  %.pre17 = load i32, ptr %.phi.trans.insert, align 4, !tbaa !12
  %.phi.trans.insert18 = getelementptr inbounds i8, ptr %1, i64 8
  %.pre19 = load i32, ptr %.phi.trans.insert18, align 8, !tbaa !12
  %.phi.trans.insert20 = getelementptr inbounds i8, ptr %1, i64 12
  %.pre21 = load i32, ptr %.phi.trans.insert20, align 4, !tbaa !12
  %.phi.trans.insert22 = getelementptr inbounds i8, ptr %1, i64 16
  %.pre23 = load i32, ptr %.phi.trans.insert22, align 16, !tbaa !12
  %.phi.trans.insert24 = getelementptr inbounds i8, ptr %1, i64 20
  %.pre25 = load i32, ptr %.phi.trans.insert24, align 4, !tbaa !12
  %.phi.trans.insert26 = getelementptr inbounds i8, ptr %1, i64 24
  %.pre27 = load i32, ptr %.phi.trans.insert26, align 8, !tbaa !12
  %.phi.trans.insert28 = getelementptr inbounds i8, ptr %1, i64 28
  %.pre29 = load i32, ptr %.phi.trans.insert28, align 4, !tbaa !12
  %.phi.trans.insert30 = getelementptr inbounds i8, ptr %1, i64 32
  %.pre31 = load i32, ptr %.phi.trans.insert30, align 16, !tbaa !12
  %.phi.trans.insert32 = getelementptr inbounds i8, ptr %1, i64 36
  %.pre33 = load i32, ptr %.phi.trans.insert32, align 4, !tbaa !12
  br label %.preheader

.preheader:                                       ; preds = %.preheader.loopexit, %0
  %20 = phi i32 [ %.pre33, %.preheader.loopexit ], [ 0, %0 ]
  %21 = phi i32 [ %.pre31, %.preheader.loopexit ], [ 0, %0 ]
  %22 = phi i32 [ %.pre29, %.preheader.loopexit ], [ 0, %0 ]
  %23 = phi i32 [ %.pre27, %.preheader.loopexit ], [ 0, %0 ]
  %24 = phi i32 [ %.pre25, %.preheader.loopexit ], [ 0, %0 ]
  %25 = phi i32 [ %.pre23, %.preheader.loopexit ], [ 0, %0 ]
  %26 = phi i32 [ %.pre21, %.preheader.loopexit ], [ 0, %0 ]
  %27 = phi i32 [ %.pre19, %.preheader.loopexit ], [ 0, %0 ]
  %28 = phi i32 [ %.pre17, %.preheader.loopexit ], [ 0, %0 ]
  %29 = phi i32 [ %.pre, %.preheader.loopexit ], [ 0, %0 ]
  %30 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %29)
  %31 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %28)
  %32 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %27)
  %33 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %26)
  %34 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %25)
  %35 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %24)
  %36 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %23)
  %37 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %22)
  %38 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %21)
  %39 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %20)
  call void @llvm.lifetime.end.p0(i64 1000, ptr nonnull %2) #6
  call void @llvm.lifetime.end.p0(i64 40, ptr nonnull %1) #6
  ret i32 0

40:                                               ; preds = %64, %.lr.ph.new
  %indvars.iv = phi i64 [ 0, %.lr.ph.new ], [ %indvars.iv.next.1, %64 ]
  %niter = phi i64 [ 0, %.lr.ph.new ], [ %niter.next.1, %64 ]
  %41 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %indvars.iv
  %42 = load i8, ptr %41, align 2, !tbaa !9
  %43 = sext i8 %42 to i64
  %44 = getelementptr inbounds i16, ptr %7, i64 %43
  %45 = load i16, ptr %44, align 2, !tbaa !10
  %46 = and i16 %45, 2048
  %.not = icmp eq i16 %46, 0
  br i1 %.not, label %52, label %47

47:                                               ; preds = %40
  %48 = add nsw i64 %43, -48
  %49 = getelementptr inbounds [10 x i32], ptr %1, i64 0, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !12
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %49, align 4, !tbaa !12
  br label %52

52:                                               ; preds = %40, %47
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %53 = getelementptr inbounds [1000 x i8], ptr %2, i64 0, i64 %indvars.iv.next
  %54 = load i8, ptr %53, align 1, !tbaa !9
  %55 = sext i8 %54 to i64
  %56 = getelementptr inbounds i16, ptr %7, i64 %55
  %57 = load i16, ptr %56, align 2, !tbaa !10
  %58 = and i16 %57, 2048
  %.not.1 = icmp eq i16 %58, 0
  br i1 %.not.1, label %64, label %59

59:                                               ; preds = %52
  %60 = add nsw i64 %55, -48
  %61 = getelementptr inbounds [10 x i32], ptr %1, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !12
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %61, align 4, !tbaa !12
  br label %64

64:                                               ; preds = %59, %52
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %.preheader.loopexit.unr-lcssa, label %40, !llvm.loop !14
}

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

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

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() local_unnamed_addr #5

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

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

attributes #0 = { 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 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #6 = { nounwind }
attributes #7 = { nounwind willreturn memory(read) }
attributes #8 = { nounwind willreturn memory(none) }

!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 = !{!"short", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass