; LLVM IR from freddiev4/dailyprogrammerchallenges :: Intermediate Challenges/Challenge 0004 Intermediate/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.Token = type { i8, ptr }

@stderr = external global ptr, align 8
@.str.33 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.34 = external hidden unnamed_addr constant [31 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: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @chartoTYPE(i8 noundef signext) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @AST_new() #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @AST_settype(ptr noundef, i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @AST_setnum(ptr noundef, double noundef) #2

; Function Attrs: nounwind uwtable
define dso_local { i8, ptr } @next_token(ptr noundef %0) #2 {
  %2 = alloca %struct.Token, align 8
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca double, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.memset.p0.i64(ptr align 8 %2, i8 0, i64 16, i1 false)
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #5
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %9, ptr %4, align 8, !tbaa !5
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = icmp eq ptr %10, null
  br i1 %11, label %22, label %12

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = load ptr, ptr %13, align 8, !tbaa !5
  %15 = icmp eq ptr %14, null
  br i1 %15, label %22, label %16

16:                                               ; preds = %12
  %17 = load ptr, ptr %3, align 8, !tbaa !5
  %18 = load ptr, ptr %17, align 8, !tbaa !5
  %19 = load i8, ptr %18, align 1, !tbaa !9
  %20 = sext i8 %19 to i32
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %22, label %23

22:                                               ; preds = %16, %12, %1
  store i32 1, ptr %5, align 4
  br label %83

23:                                               ; preds = %16
  br label %24

24:                                               ; preds = %36, %23
  %25 = call ptr @__ctype_b_loc() #6
  %26 = load ptr, ptr %25, align 8, !tbaa !5
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i8, ptr %27, align 1, !tbaa !9
  %29 = sext i8 %28 to i32
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i16, ptr %26, i64 %30
  %32 = load i16, ptr %31, align 2, !tbaa !10
  %33 = zext i16 %32 to i32
  %34 = and i32 %33, 8192
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %24
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = getelementptr inbounds i8, ptr %37, i32 1
  store ptr %38, ptr %4, align 8, !tbaa !5
  br label %24, !llvm.loop !12

39:                                               ; preds = %24
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = load i8, ptr %40, align 1, !tbaa !9
  %42 = call signext i8 @matchDelim(i8 noundef signext %41)
  %43 = sext i8 %42 to i32
  %44 = icmp ne i32 %43, 0
  br i1 %44, label %45, label %60

45:                                               ; preds = %39
  %46 = call ptr @AST_new()
  %47 = getelementptr inbounds %struct.Token, ptr %2, i32 0, i32 1
  store ptr %46, ptr %47, align 8, !tbaa !14
  %48 = getelementptr inbounds %struct.Token, ptr %2, i32 0, i32 1
  %49 = load ptr, ptr %48, align 8, !tbaa !14
  %50 = load ptr, ptr %4, align 8, !tbaa !5
  %51 = load i8, ptr %50, align 1, !tbaa !9
  %52 = call i32 @chartoTYPE(i8 noundef signext %51)
  %53 = call ptr @AST_settype(ptr noundef %49, i32 noundef %52)
  %54 = load ptr, ptr %4, align 8, !tbaa !5
  %55 = load i8, ptr %54, align 1, !tbaa !9
  %56 = getelementptr inbounds %struct.Token, ptr %2, i32 0, i32 0
  store i8 %55, ptr %56, align 8, !tbaa !16
  %57 = load ptr, ptr %4, align 8, !tbaa !5
  %58 = getelementptr inbounds i8, ptr %57, i32 1
  store ptr %58, ptr %4, align 8, !tbaa !5
  %59 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %58, ptr %59, align 8, !tbaa !5
  store i32 1, ptr %5, align 4
  br label %83

60:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #5
  store ptr null, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #5
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = call double @strtod(ptr noundef %61, ptr noundef %6) #5
  store double %62, ptr %7, align 8, !tbaa !17
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = icmp ne ptr %63, %64
  br i1 %65, label %66, label %78

66:                                               ; preds = %60
  %67 = call ptr @AST_new()
  %68 = getelementptr inbounds %struct.Token, ptr %2, i32 0, i32 1
  store ptr %67, ptr %68, align 8, !tbaa !14
  %69 = getelementptr inbounds %struct.Token, ptr %2, i32 0, i32 1
  %70 = load ptr, ptr %69, align 8, !tbaa !14
  %71 = call ptr @AST_settype(ptr noundef %70, i32 noundef 0)
  %72 = getelementptr inbounds %struct.Token, ptr %2, i32 0, i32 1
  %73 = load ptr, ptr %72, align 8, !tbaa !14
  %74 = load double, ptr %7, align 8, !tbaa !17
  %75 = call ptr @AST_setnum(ptr noundef %73, double noundef %74)
  %76 = load ptr, ptr %6, align 8, !tbaa !5
  %77 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %76, ptr %77, align 8, !tbaa !5
  store i32 1, ptr %5, align 4
  br label %82

78:                                               ; preds = %60
  %79 = load ptr, ptr @stderr, align 8, !tbaa !5
  %80 = load ptr, ptr %4, align 8, !tbaa !5
  %81 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %79, ptr noundef @.str.33, ptr noundef @.str.34, ptr noundef %80) #5
  store i32 1, ptr %5, align 4
  br label %82

82:                                               ; preds = %78, %66
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #5
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #5
  br label %83

83:                                               ; preds = %82, %45, %22
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #5
  %84 = load { i8, ptr }, ptr %2, align 8
  ret { i8, ptr } %84
}

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

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

; Function Attrs: nounwind uwtable
declare hidden signext i8 @matchDelim(i8 noundef signext) #2

; Function Attrs: nounwind
declare double @strtod(ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { 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 #5 = { nounwind }
attributes #6 = { 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 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !6, i64 8}
!15 = !{!"Token", !7, i64 0, !6, i64 8}
!16 = !{!15, !7, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"double", !7, i64 0}


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

@stderr = external local_unnamed_addr global ptr, align 8
@.str.33 = external hidden unnamed_addr constant [10 x i8], align 1
@.str.34 = external hidden unnamed_addr constant [31 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: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @chartoTYPE(i8 noundef signext) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @AST_new() local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @AST_settype(ptr noundef, i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
declare dso_local ptr @AST_setnum(ptr noundef, double noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local { i8, ptr } @next_token(ptr nocapture noundef %0) local_unnamed_addr #2 {
  %2 = alloca ptr, align 8
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  %4 = icmp eq ptr %3, null
  br i1 %4, label %36, label %5

5:                                                ; preds = %1
  %6 = load i8, ptr %3, align 1, !tbaa !9
  %7 = icmp eq i8 %6, 0
  br i1 %7, label %36, label %.preheader

.preheader:                                       ; preds = %5
  %8 = tail call ptr @__ctype_b_loc() #5
  %9 = load ptr, ptr %8, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %10, %.preheader
  %.0 = phi ptr [ %16, %10 ], [ %3, %.preheader ]
  %11 = load i8, ptr %.0, align 1, !tbaa !9
  %12 = sext i8 %11 to i64
  %13 = getelementptr inbounds i16, ptr %9, i64 %12
  %14 = load i16, ptr %13, align 2, !tbaa !10
  %15 = and i16 %14, 8192
  %.not = icmp eq i16 %15, 0
  %16 = getelementptr inbounds i8, ptr %.0, i64 1
  br i1 %.not, label %17, label %10, !llvm.loop !12

17:                                               ; preds = %10
  %18 = tail call signext i8 @matchDelim(i8 noundef signext %11)
  %.not20 = icmp eq i8 %18, 0
  br i1 %.not20, label %25, label %19

19:                                               ; preds = %17
  %20 = tail call ptr @AST_new()
  %21 = load i8, ptr %.0, align 1, !tbaa !9
  %22 = tail call i32 @chartoTYPE(i8 noundef signext %21)
  %23 = tail call ptr @AST_settype(ptr noundef %20, i32 noundef %22)
  %24 = load i8, ptr %.0, align 1, !tbaa !9
  store ptr %16, ptr %0, align 8, !tbaa !5
  br label %36

25:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #6
  store ptr null, ptr %2, align 8, !tbaa !5
  %26 = call double @strtod(ptr noundef nonnull %.0, ptr noundef nonnull %2) #6
  %27 = load ptr, ptr %2, align 8, !tbaa !5
  %.not21 = icmp eq ptr %.0, %27
  br i1 %.not21, label %32, label %28

28:                                               ; preds = %25
  %29 = tail call ptr @AST_new()
  %30 = tail call ptr @AST_settype(ptr noundef %29, i32 noundef 0)
  %31 = tail call ptr @AST_setnum(ptr noundef %29, double noundef %26)
  store ptr %27, ptr %0, align 8, !tbaa !5
  br label %35

32:                                               ; preds = %25
  %33 = load ptr, ptr @stderr, align 8, !tbaa !5
  %34 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef nonnull @.str.33, ptr noundef nonnull @.str.34, ptr noundef nonnull %.0) #7
  br label %35

35:                                               ; preds = %32, %28
  %.sroa.316.0 = phi ptr [ %29, %28 ], [ null, %32 ]
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #6
  br label %36

36:                                               ; preds = %1, %5, %35, %19
  %.sroa.316.1 = phi ptr [ %20, %19 ], [ %.sroa.316.0, %35 ], [ null, %5 ], [ null, %1 ]
  %.sroa.0.0 = phi i8 [ %24, %19 ], [ 0, %35 ], [ 0, %5 ], [ 0, %1 ]
  %.fca.0.insert = insertvalue { i8, ptr } poison, i8 %.sroa.0.0, 0
  %.fca.1.insert = insertvalue { i8, ptr } %.fca.0.insert, ptr %.sroa.316.1, 1
  ret { i8, ptr } %.fca.1.insert
}

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

; Function Attrs: nounwind uwtable
declare hidden signext i8 @matchDelim(i8 noundef signext) local_unnamed_addr #2

; Function Attrs: mustprogress nofree nounwind willreturn
declare double @strtod(ptr noundef readonly, ptr nocapture noundef) local_unnamed_addr #4

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { 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 #3 = { 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 #4 = { mustprogress nofree nounwind willreturn "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) }
attributes #6 = { nounwind }
attributes #7 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"short", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


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