; LLVM IR from UtkarshPathrabe/Competitive-Coding :: SPOJ Solutions/NBIN.ll (Z5printx)

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

@pre_com = external dso_local global [100 x i64], align 16
@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; 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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z4findx(i64 noundef) #1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5printx(i64 noundef %0) #2 {
  %2 = alloca i64, align 8
  %3 = alloca [80 x i8], align 16
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store i64 %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 80, ptr %3) #5
  call void @llvm.memset.p0.i64(ptr align 16 %3, i8 0, i64 80, i1 false)
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #5
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = call noundef i32 @_Z4findx(i64 noundef %8)
  store i32 %9, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #5
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = add nsw i32 %10, 1
  store i32 %11, ptr %5, align 4, !tbaa !9
  %12 = load i32, ptr %4, align 4, !tbaa !9
  %13 = add nsw i32 %12, 1
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [80 x i8], ptr %3, i64 0, i64 %14
  store i8 1, ptr %15, align 1, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  %16 = load i64, ptr %2, align 8, !tbaa !5
  %17 = load i32, ptr %4, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !5
  %21 = sub nsw i64 %16, %20
  %22 = sub nsw i64 %21, 1
  store i64 %22, ptr %6, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %26, %1
  %24 = load i64, ptr %6, align 8, !tbaa !5
  %25 = icmp sgt i64 %24, 0
  br i1 %25, label %26, label %40

26:                                               ; preds = %23
  %27 = load i64, ptr %6, align 8, !tbaa !5
  %28 = call noundef i32 @_Z4findx(i64 noundef %27)
  store i32 %28, ptr %4, align 4, !tbaa !9
  %29 = load i32, ptr %4, align 4, !tbaa !9
  %30 = add nsw i32 %29, 1
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [80 x i8], ptr %3, i64 0, i64 %31
  store i8 1, ptr %32, align 1, !tbaa !11
  %33 = load i64, ptr %6, align 8, !tbaa !5
  %34 = load i32, ptr %4, align 4, !tbaa !9
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = sub nsw i64 %33, %37
  %39 = sub nsw i64 %38, 1
  store i64 %39, ptr %6, align 8, !tbaa !5
  br label %23, !llvm.loop !13

40:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  %41 = load i32, ptr %5, align 4, !tbaa !9
  store i32 %41, ptr %7, align 4, !tbaa !9
  br label %42

42:                                               ; preds = %57, %40
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = icmp sge i32 %43, 1
  br i1 %44, label %46, label %45

45:                                               ; preds = %42
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  br label %60

46:                                               ; preds = %42
  %47 = load i32, ptr %7, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds [80 x i8], ptr %3, i64 0, i64 %48
  %50 = load i8, ptr %49, align 1, !tbaa !11, !range !15, !noundef !16
  %51 = trunc i8 %50 to i1
  br i1 %51, label %52, label %54

52:                                               ; preds = %46
  %53 = call i32 (ptr, ...) @printf(ptr noundef @.str)
  br label %56

54:                                               ; preds = %46
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  br label %56

56:                                               ; preds = %54, %52
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %7, align 4, !tbaa !9
  %59 = add nsw i32 %58, -1
  store i32 %59, ptr %7, align 4, !tbaa !9
  br label %42, !llvm.loop !17

60:                                               ; preds = %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #5
  call void @llvm.lifetime.end.p0(i64 80, ptr %3) #5
  ret void
}

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

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

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


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

@pre_com = external dso_local local_unnamed_addr global [100 x i64], align 16
@.str = external hidden unnamed_addr constant [2 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [2 x i8], align 1

; 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: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z4findx(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5printx(i64 noundef %0) local_unnamed_addr #1 {
  %2 = alloca [80 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 80, ptr nonnull %2) #4
  call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(80) %2, i8 0, i64 80, i1 false)
  %3 = tail call noundef i32 @_Z4findx(i64 noundef %0)
  %4 = add nsw i32 %3, 1
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [80 x i8], ptr %2, i64 0, i64 %5
  store i8 1, ptr %6, align 1, !tbaa !5
  %7 = sext i32 %3 to i64
  %8 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %7
  %9 = load i64, ptr %8, align 8, !tbaa !9
  %10 = xor i64 %9, -1
  %11 = add i64 %10, %0
  %12 = icmp sgt i64 %11, 0
  br i1 %12, label %.lr.ph, label %.preheader

.preheader:                                       ; preds = %.lr.ph, %1
  %13 = icmp sgt i32 %3, -1
  br i1 %13, label %.lr.ph17.preheader, label %._crit_edge

.lr.ph17.preheader:                               ; preds = %.preheader
  %14 = zext nneg i32 %4 to i64
  br label %.lr.ph17

.lr.ph:                                           ; preds = %1, %.lr.ph
  %.01415 = phi i64 [ %23, %.lr.ph ], [ %11, %1 ]
  %15 = tail call noundef i32 @_Z4findx(i64 noundef %.01415)
  %16 = add nsw i32 %15, 1
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [80 x i8], ptr %2, i64 0, i64 %17
  store i8 1, ptr %18, align 1, !tbaa !5
  %19 = sext i32 %15 to i64
  %20 = getelementptr inbounds [100 x i64], ptr @pre_com, i64 0, i64 %19
  %21 = load i64, ptr %20, align 8, !tbaa !9
  %22 = xor i64 %21, -1
  %23 = add i64 %.01415, %22
  %24 = icmp sgt i64 %23, 0
  br i1 %24, label %.lr.ph, label %.preheader, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph17, %.preheader
  call void @llvm.lifetime.end.p0(i64 80, ptr nonnull %2) #4
  ret void

.lr.ph17:                                         ; preds = %.lr.ph17.preheader, %.lr.ph17
  %indvars.iv = phi i64 [ %14, %.lr.ph17.preheader ], [ %indvars.iv.next, %.lr.ph17 ]
  %25 = getelementptr inbounds [80 x i8], ptr %2, i64 0, i64 %indvars.iv
  %26 = load i8, ptr %25, align 1, !tbaa !5, !range !13, !noundef !14
  %27 = trunc nuw i8 %26 to i1
  %.str..str.1 = select i1 %27, ptr @.str, ptr @.str.1
  %28 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) %.str..str.1)
  %indvars.iv.next = add nsw i64 %indvars.iv, -1
  %29 = trunc nuw i64 %indvars.iv to i32
  %30 = icmp sgt i32 %29, 1
  br i1 %30, label %.lr.ph17, label %._crit_edge, !llvm.loop !15
}

; 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 i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #3

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


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