; LLVM IR from smwikipedia/EmbeddedAndRTOSSamples :: C2.5 (full)

; 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.uart = type { ptr, i32 }

@v = external dso_local global [10 x i32], align 16
@uart = external dso_local global [4 x %struct.uart], align 16
@.str = external hidden unnamed_addr constant [39 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [24 x i8], align 1
@sum = external dso_local global i32, align 4
@.str.5 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [15 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @uputc(ptr noundef, i8 noundef signext) #0

; Function Attrs: nounwind uwtable
declare dso_local void @upgets(ptr noundef, ptr noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @uprints(ptr noundef, ptr noundef) #0

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

; Function Attrs: nounwind uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca [64 x i8], align 16
  %4 = alloca ptr, align 8
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #2
  call void @llvm.lifetime.start.p0(i64 64, ptr %3) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  %5 = call i32 @uart_init()
  store ptr @uart, ptr %4, align 8, !tbaa !5
  %6 = load ptr, ptr %4, align 8, !tbaa !5
  call void @uprints(ptr noundef %6, ptr noundef @.str)
  br label %7

7:                                                ; preds = %19, %0
  br label %8

8:                                                ; preds = %7
  %9 = load ptr, ptr %4, align 8, !tbaa !5
  %10 = getelementptr inbounds [64 x i8], ptr %3, i64 0, i64 0
  call void @upgets(ptr noundef %9, ptr noundef %10)
  %11 = load ptr, ptr %4, align 8, !tbaa !5
  call void @uprints(ptr noundef %11, ptr noundef @.str.1)
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  %13 = getelementptr inbounds [64 x i8], ptr %3, i64 0, i64 0
  call void @uprints(ptr noundef %12, ptr noundef %13)
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  call void @uprints(ptr noundef %14, ptr noundef @.str.2)
  %15 = getelementptr inbounds [64 x i8], ptr %3, i64 0, i64 0
  %16 = call i32 @strcmp(ptr noundef %15, ptr noundef @.str.3)
  %17 = icmp eq i32 %16, 0
  br i1 %17, label %18, label %19

18:                                               ; preds = %8
  br label %20

19:                                               ; preds = %8
  br label %7

20:                                               ; preds = %18
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  call void @uprints(ptr noundef %21, ptr noundef @.str.4)
  store i32 0, ptr @sum, align 4, !tbaa !9
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %22

22:                                               ; preds = %32, %20
  %23 = load i32, ptr %2, align 4, !tbaa !9
  %24 = icmp slt i32 %23, 10
  br i1 %24, label %25, label %35

25:                                               ; preds = %22
  %26 = load i32, ptr %2, align 4, !tbaa !9
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [10 x i32], ptr @v, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !9
  %30 = load i32, ptr @sum, align 4, !tbaa !9
  %31 = add nsw i32 %30, %29
  store i32 %31, ptr @sum, align 4, !tbaa !9
  br label %32

32:                                               ; preds = %25
  %33 = load i32, ptr %2, align 4, !tbaa !9
  %34 = add nsw i32 %33, 1
  store i32 %34, ptr %2, align 4, !tbaa !9
  br label %22, !llvm.loop !11

35:                                               ; preds = %22
  %36 = load ptr, ptr %4, align 8, !tbaa !5
  call void @uprints(ptr noundef %36, ptr noundef @.str.5)
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = load i32, ptr @sum, align 4, !tbaa !9
  %39 = sdiv i32 %38, 10
  %40 = add nsw i32 %39, 48
  %41 = trunc i32 %40 to i8
  call void @uputc(ptr noundef %37, i8 noundef signext %41)
  %42 = load ptr, ptr %4, align 8, !tbaa !5
  %43 = load i32, ptr @sum, align 4, !tbaa !9
  %44 = srem i32 %43, 10
  %45 = add nsw i32 %44, 48
  %46 = trunc i32 %45 to i8
  call void @uputc(ptr noundef %42, i8 noundef signext %46)
  %47 = load ptr, ptr %4, align 8, !tbaa !5
  call void @uprints(ptr noundef %47, ptr noundef @.str.6)
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 64, ptr %3) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #2
  %48 = load i32, ptr %1, align 4
  ret i32 %48
}

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 }

!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"}


; 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.uart = type { ptr, i32 }

@v = external dso_local local_unnamed_addr global [10 x i32], align 16
@uart = external dso_local global [4 x %struct.uart], align 16
@.str = external hidden unnamed_addr constant [39 x i8], align 1
@.str.1 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [24 x i8], align 1
@sum = external dso_local local_unnamed_addr global i32, align 4
@.str.5 = external hidden unnamed_addr constant [7 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [15 x i8], align 1

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

; Function Attrs: nounwind uwtable
declare dso_local void @uputc(ptr noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @upgets(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @uprints(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read) uwtable
declare dso_local i32 @strcmp(ptr nocapture noundef, ptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
  %1 = alloca [64 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 64, ptr nonnull %1) #4
  %2 = tail call i32 @uart_init()
  tail call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull @.str)
  br label %3

3:                                                ; preds = %3, %0
  call void @upgets(ptr noundef nonnull @uart, ptr noundef nonnull %1)
  call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull @.str.1)
  call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull %1)
  call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull @.str.2)
  %4 = call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %1, ptr noundef nonnull dereferenceable(1) @.str.3)
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %6, label %3

6:                                                ; preds = %3
  call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull @.str.4)
  %7 = load <8 x i32>, ptr @v, align 16, !tbaa !5
  %8 = load i32, ptr getelementptr inbounds (i8, ptr @v, i64 32), align 16, !tbaa !5
  %9 = load i32, ptr getelementptr inbounds (i8, ptr @v, i64 36), align 4, !tbaa !5
  %10 = call i32 @llvm.vector.reduce.add.v8i32(<8 x i32> %7)
  %op.rdx = add i32 %10, %8
  %op.rdx15 = add i32 %op.rdx, %9
  store i32 %op.rdx15, ptr @sum, align 4, !tbaa !5
  call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull @.str.5)
  %11 = load i32, ptr @sum, align 4, !tbaa !5
  %12 = sdiv i32 %11, 10
  %13 = trunc i32 %12 to i8
  %14 = add i8 %13, 48
  call void @uputc(ptr noundef nonnull @uart, i8 noundef signext %14)
  %15 = load i32, ptr @sum, align 4, !tbaa !5
  %16 = srem i32 %15, 10
  %17 = trunc nsw i32 %16 to i8
  %18 = add nsw i8 %17, 48
  call void @uputc(ptr noundef nonnull @uart, i8 noundef signext %18)
  call void @uprints(ptr noundef nonnull @uart, ptr noundef nonnull @.str.6)
  call void @llvm.lifetime.end.p0(i64 64, ptr nonnull %1) #4
  ret i32 0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v8i32(<8 x i32>) #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 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { mustprogress nofree nounwind willreturn memory(argmem: read) 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 nosync nounwind speculatable willreturn memory(none) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}


; Effective O3 passes: DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,LCSSAPass,LICMPass,LoopRotatePass,PostOrderFunctionAttrsPass,SLPVectorizerPass,SROAPass,SimplifyCFGPass,TailCallElimPass