; LLVM IR from freddiev4/dailyprogrammerchallenges :: Easy Challenges/Challenge 0003 Easy/solutions (solution2)

; 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 [5 x %struct.option], align 16
@optarg = external global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@stderr = external global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [49 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [37 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @encode(i32 noundef, i32 noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @decode(i32 noundef, i32 noundef) #0

; 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
  %8 = alloca i32, align 4
  %9 = 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) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 3, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !5
  %10 = load ptr, ptr %5, align 8, !tbaa !9
  %11 = getelementptr inbounds ptr, ptr %10, i64 0
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  store ptr %12, ptr @invoc_name, align 8, !tbaa !9
  br label %13

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

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  store i32 0, ptr %9, align 4, !tbaa !5
  %19 = load i32, ptr %6, align 4, !tbaa !5
  switch i32 %19, label %34 [
    i32 100, label %20
    i32 107, label %21
    i32 104, label %32
    i32 118, label %33
  ]

20:                                               ; preds = %18
  store i32 1, ptr %8, align 4, !tbaa !5
  br label %38

21:                                               ; preds = %18
  %22 = load ptr, ptr @optarg, align 8, !tbaa !9
  %23 = call i32 (ptr, ptr, ...) @__isoc99_sscanf(ptr noundef %22, ptr noundef @.str.1, ptr noundef %7) #5
  store i32 %23, ptr %9, align 4, !tbaa !5
  %24 = load i32, ptr %9, align 4, !tbaa !5
  %25 = icmp ne i32 %24, 1
  br i1 %25, label %26, label %31

26:                                               ; preds = %21
  store i32 3, ptr %7, align 4, !tbaa !5
  %27 = load ptr, ptr @stderr, align 8, !tbaa !9
  %28 = load ptr, ptr @optarg, align 8, !tbaa !9
  %29 = load i32, ptr %7, align 4, !tbaa !5
  %30 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef @.str.2, ptr noundef %28, i32 noundef %29) #5
  br label %31

31:                                               ; preds = %26, %21
  br label %38

32:                                               ; preds = %18
  call void @usage()
  call void @exit(i32 noundef 0) #6
  unreachable

33:                                               ; preds = %18
  call void @version()
  call void @exit(i32 noundef 0) #6
  unreachable

34:                                               ; preds = %18
  %35 = load ptr, ptr @stderr, align 8, !tbaa !9
  %36 = load ptr, ptr @invoc_name, align 8, !tbaa !9
  %37 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %35, ptr noundef @.str.3, ptr noundef %36) #5
  call void @exit(i32 noundef 1) #6
  unreachable

38:                                               ; preds = %31, %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %13, !llvm.loop !11

39:                                               ; preds = %13
  %40 = load i32, ptr %8, align 4, !tbaa !5
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %52

42:                                               ; preds = %39
  br label %43

43:                                               ; preds = %46, %42
  %44 = call i32 @getchar()
  store i32 %44, ptr %6, align 4, !tbaa !5
  %45 = icmp ne i32 -1, %44
  br i1 %45, label %46, label %51

46:                                               ; preds = %43
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = load i32, ptr %7, align 4, !tbaa !5
  %49 = call i32 @encode(i32 noundef %47, i32 noundef %48)
  %50 = call i32 @putchar(i32 noundef %49)
  br label %43, !llvm.loop !13

51:                                               ; preds = %43
  br label %62

52:                                               ; preds = %39
  br label %53

53:                                               ; preds = %56, %52
  %54 = call i32 @getchar()
  store i32 %54, ptr %6, align 4, !tbaa !5
  %55 = icmp ne i32 -1, %54
  br i1 %55, label %56, label %61

56:                                               ; preds = %53
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = call i32 @decode(i32 noundef %57, i32 noundef %58)
  %60 = call i32 @putchar(i32 noundef %59)
  br label %53, !llvm.loop !14

61:                                               ; preds = %53
  br label %62

62:                                               ; preds = %61, %51
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #5
  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
declare i32 @__isoc99_sscanf(ptr noundef, ptr noundef, ...) #2

; Function Attrs: nounwind
declare i32 @fprintf(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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint nounwind uwtable
declare i32 @getchar() #4

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

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 = { inlinehint 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 #5 = { nounwind }
attributes #6 = { 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}
!14 = distinct !{!14, !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 [5 x %struct.option], align 16
@optarg = external local_unnamed_addr global ptr, align 8
@.str.1 = external hidden unnamed_addr constant [3 x i8], align 1
@stderr = external local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [49 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [37 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @encode(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local i32 @decode(i32 noundef, i32 noundef) local_unnamed_addr #0

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

.outer:                                           ; preds = %6, %2
  %5 = phi i1 [ true, %2 ], [ false, %6 ]
  br label %6

6:                                                ; preds = %.backedge, %.outer
  %7 = call i32 @getopt_long(i32 noundef %0, ptr noundef nonnull %1, ptr noundef nonnull @.str, ptr noundef nonnull @longopts, ptr noundef null) #6
  switch i32 %7, label %17 [
    i32 -1, label %21
    i32 100, label %.outer
    i32 107, label %8
    i32 104, label %15
    i32 118, label %16
  ], !llvm.loop !11

8:                                                ; preds = %6
  %9 = load ptr, ptr @optarg, align 8, !tbaa !9
  %10 = call i32 (ptr, ptr, ...) @__isoc99_sscanf(ptr noundef %9, ptr noundef nonnull @.str.1, ptr noundef nonnull %3) #6
  %.not12 = icmp eq i32 %10, 1
  br i1 %.not12, label %.backedge, label %11

.backedge:                                        ; preds = %8, %11
  br label %6, !llvm.loop !11

11:                                               ; preds = %8
  store i32 3, ptr %3, align 4, !tbaa !5
  %12 = load ptr, ptr @stderr, align 8, !tbaa !9
  %13 = load ptr, ptr @optarg, align 8, !tbaa !9
  %14 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %12, ptr noundef nonnull @.str.2, ptr noundef %13, i32 noundef 3) #7
  br label %.backedge

15:                                               ; preds = %6
  call void @usage()
  call void @exit(i32 noundef 0) #8
  unreachable

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

17:                                               ; preds = %6
  %18 = load ptr, ptr @stderr, align 8, !tbaa !9
  %19 = load ptr, ptr @invoc_name, align 8, !tbaa !9
  %20 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %18, ptr noundef nonnull @.str.3, ptr noundef %19) #7
  call void @exit(i32 noundef 1) #8
  unreachable

21:                                               ; preds = %6
  %22 = call i32 @getchar()
  %.not1119 = icmp eq i32 %22, -1
  br i1 %5, label %.preheader, label %.preheader13

.preheader13:                                     ; preds = %21
  br i1 %.not1119, label %.loopexit, label %.lr.ph

.preheader:                                       ; preds = %21
  br i1 %.not1119, label %.loopexit, label %.lr.ph20

.lr.ph20:                                         ; preds = %.preheader, %.lr.ph20
  %23 = phi i32 [ %27, %.lr.ph20 ], [ %22, %.preheader ]
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = call i32 @encode(i32 noundef %23, i32 noundef %24)
  %26 = call i32 @putchar(i32 noundef %25)
  %27 = call i32 @getchar()
  %.not11 = icmp eq i32 %27, -1
  br i1 %.not11, label %.loopexit, label %.lr.ph20, !llvm.loop !13

.lr.ph:                                           ; preds = %.preheader13, %.lr.ph
  %28 = phi i32 [ %32, %.lr.ph ], [ %22, %.preheader13 ]
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = call i32 @decode(i32 noundef %28, i32 noundef %29)
  %31 = call i32 @putchar(i32 noundef %30)
  %32 = call i32 @getchar()
  %.not10 = icmp eq i32 %32, -1
  br i1 %.not10, label %.loopexit, label %.lr.ph, !llvm.loop !14

.loopexit:                                        ; preds = %.lr.ph, %.lr.ph20, %.preheader13, %.preheader
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %3) #6
  ret i32 0
}

; Function Attrs: mustprogress 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) local_unnamed_addr #2

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

; 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 @usage() local_unnamed_addr #0

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

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

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

; Function Attrs: inlinehint nofree nounwind uwtable
declare noundef i32 @getchar() local_unnamed_addr #5

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

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 = { mustprogress 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 = { 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 = { 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 #5 = { inlinehint 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 #6 = { nounwind }
attributes #7 = { cold nounwind }
attributes #8 = { 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}
!14 = distinct !{!14, !12}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass