; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0036 Easy/solutions (solution)

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

%struct.option = type { ptr, i32, ptr, i32 }

@invoc_name = external hidden global ptr, align 8
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@longopts = external hidden global [7 x %struct.option], align 16
@option_flags = external hidden global i32, align 4
@stderr = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [37 x i8], align 1
@optind = external global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #4
  %8 = load ptr, ptr %5, align 8, !tbaa !9
  %9 = getelementptr inbounds ptr, ptr %8, i64 0
  %10 = load ptr, ptr %9, align 8, !tbaa !9
  store ptr %10, ptr @invoc_name, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %37, %2
  %12 = load i32, ptr %4, align 4, !tbaa !5
  %13 = load ptr, ptr %5, align 8, !tbaa !9
  %14 = call i32 @getopt_long(i32 noundef %12, ptr noundef %13, ptr noundef @.str, ptr noundef @longopts, ptr noundef null) #4
  store i32 %14, ptr %6, align 4, !tbaa !5
  %15 = icmp ne i32 %14, -1
  br i1 %15, label %16, label %38

16:                                               ; preds = %11
  %17 = load i32, ptr %6, align 4, !tbaa !5
  switch i32 %17, label %36 [
    i32 99, label %18
    i32 108, label %21
    i32 119, label %24
    i32 118, label %27
    i32 104, label %30
    i32 120, label %31
    i32 63, label %32
  ]

18:                                               ; preds = %16
  %19 = load i32, ptr @option_flags, align 4, !tbaa !5
  %20 = or i32 %19, 1
  store i32 %20, ptr @option_flags, align 4, !tbaa !5
  br label %37

21:                                               ; preds = %16
  %22 = load i32, ptr @option_flags, align 4, !tbaa !5
  %23 = or i32 %22, 8
  store i32 %23, ptr @option_flags, align 4, !tbaa !5
  br label %37

24:                                               ; preds = %16
  %25 = load i32, ptr @option_flags, align 4, !tbaa !5
  %26 = or i32 %25, 2
  store i32 %26, ptr @option_flags, align 4, !tbaa !5
  br label %37

27:                                               ; preds = %16
  %28 = load i32, ptr @option_flags, align 4, !tbaa !5
  %29 = or i32 %28, 4
  store i32 %29, ptr @option_flags, align 4, !tbaa !5
  br label %37

30:                                               ; preds = %16
  call void @usage()
  call void @exit(i32 noundef 0) #5
  unreachable

31:                                               ; preds = %16
  call void @version()
  call void @exit(i32 noundef 0) #5
  unreachable

32:                                               ; preds = %16
  %33 = load ptr, ptr @stderr, align 8, !tbaa !9
  %34 = load ptr, ptr @invoc_name, align 8, !tbaa !9
  %35 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef @.str.1, ptr noundef %34) #4
  call void @exit(i32 noundef 1) #5
  unreachable

36:                                               ; preds = %16
  br label %37

37:                                               ; preds = %36, %27, %24, %21, %18
  br label %11, !llvm.loop !11

38:                                               ; preds = %11
  %39 = load i32, ptr @option_flags, align 4, !tbaa !5
  %40 = icmp ne i32 %39, 0
  br i1 %40, label %42, label %41

41:                                               ; preds = %38
  store i32 11, ptr @option_flags, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %41, %38
  %43 = load i32, ptr @optind, align 4, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !5
  %45 = icmp eq i32 %43, %44
  br i1 %45, label %46, label %47

46:                                               ; preds = %42
  call void @print_counts(ptr noundef null)
  br label %63

47:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #4
  %48 = load i32, ptr @optind, align 4, !tbaa !5
  store i32 %48, ptr %7, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %59, %47
  %50 = load i32, ptr %7, align 4, !tbaa !5
  %51 = load i32, ptr %4, align 4, !tbaa !5
  %52 = icmp slt i32 %50, %51
  br i1 %52, label %53, label %62

53:                                               ; preds = %49
  %54 = load ptr, ptr %5, align 8, !tbaa !9
  %55 = load i32, ptr %7, align 4, !tbaa !5
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds ptr, ptr %54, i64 %56
  %58 = load ptr, ptr %57, align 8, !tbaa !9
  call void @print_counts(ptr noundef %58)
  br label %59

59:                                               ; preds = %53
  %60 = load i32, ptr %7, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %7, align 4, !tbaa !5
  br label %49, !llvm.loop !13

62:                                               ; preds = %49
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #4
  br label %63

63:                                               ; preds = %62, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #4
  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: nounwind
declare i32 @getopt_long(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) #2

; Function Attrs: nounwind uwtable
declare hidden void @usage() #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) #3

; Function Attrs: nounwind uwtable
declare hidden void @version() #0

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind uwtable
declare hidden void @print_counts(ptr noundef) #0

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

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 = { 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 = { noreturn 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 }
attributes #5 = { noreturn 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !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"

%struct.option = type { ptr, i32, ptr, i32 }

@invoc_name = external hidden local_unnamed_addr global ptr, align 8
@.str = external hidden unnamed_addr constant [7 x i8], align 1
@longopts = external hidden global [7 x %struct.option], align 16
@option_flags = external hidden local_unnamed_addr global i32, align 4
@stderr = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [37 x i8], align 1
@optind = external local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr noundef %1) local_unnamed_addr #0 {
  %3 = load ptr, ptr %1, align 8, !tbaa !5
  store ptr %3, ptr @invoc_name, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %.backedge, %2
  %5 = tail call i32 @getopt_long(i32 noundef %0, ptr noundef nonnull %1, ptr noundef nonnull @.str, ptr noundef nonnull @longopts, ptr noundef null) #4
  switch i32 %5, label %.backedge [
    i32 -1, label %17
    i32 99, label %.sink.split
    i32 108, label %6
    i32 119, label %7
    i32 118, label %8
    i32 104, label %9
    i32 120, label %10
    i32 63, label %11
  ]

6:                                                ; preds = %4
  br label %.sink.split

7:                                                ; preds = %4
  br label %.sink.split

8:                                                ; preds = %4
  br label %.sink.split

9:                                                ; preds = %4
  tail call void @usage()
  tail call void @exit(i32 noundef 0) #5
  unreachable

10:                                               ; preds = %4
  tail call void @version()
  tail call void @exit(i32 noundef 0) #5
  unreachable

11:                                               ; preds = %4
  %12 = load ptr, ptr @stderr, align 8, !tbaa !5
  %13 = load ptr, ptr @invoc_name, align 8, !tbaa !5
  %14 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef nonnull @.str.1, ptr noundef %13) #6
  tail call void @exit(i32 noundef 1) #5
  unreachable

.sink.split:                                      ; preds = %4, %6, %7, %8
  %.sink16 = phi i32 [ 4, %8 ], [ 2, %7 ], [ 8, %6 ], [ 1, %4 ]
  %15 = load i32, ptr @option_flags, align 4, !tbaa !9
  %16 = or i32 %15, %.sink16
  store i32 %16, ptr @option_flags, align 4, !tbaa !9
  br label %.backedge

.backedge:                                        ; preds = %.sink.split, %4
  br label %4, !llvm.loop !11

17:                                               ; preds = %4
  %18 = load i32, ptr @option_flags, align 4, !tbaa !9
  %.not12 = icmp eq i32 %18, 0
  br i1 %.not12, label %19, label %20

19:                                               ; preds = %17
  store i32 11, ptr @option_flags, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %19, %17
  %21 = load i32, ptr @optind, align 4, !tbaa !9
  %22 = icmp eq i32 %21, %0
  br i1 %22, label %25, label %.preheader

.preheader:                                       ; preds = %20
  %23 = icmp slt i32 %21, %0
  br i1 %23, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %.preheader
  %24 = sext i32 %21 to i64
  br label %.lr.ph

25:                                               ; preds = %20
  tail call void @print_counts(ptr noundef null)
  br label %.loopexit

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %24, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %26 = getelementptr inbounds ptr, ptr %1, i64 %indvars.iv
  %27 = load ptr, ptr %26, align 8, !tbaa !5
  tail call void @print_counts(ptr noundef %27)
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %lftr.wideiv, %0
  br i1 %exitcond.not, label %.loopexit, label %.lr.ph, !llvm.loop !13

.loopexit:                                        ; preds = %.lr.ph, %.preheader, %25
  ret i32 0
}

; Function Attrs: nounwind
declare i32 @getopt_long(i32 noundef, ptr noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare hidden void @usage() local_unnamed_addr #0

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare hidden void @version() local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
declare hidden void @print_counts(ptr noundef) local_unnamed_addr #0

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 = { 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 #2 = { noreturn 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 = { 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 }
attributes #5 = { noreturn nounwind }
attributes #6 = { cold 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


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