; LLVM IR from control-flow-attestation/c-flat :: samples/soldering (arduino)

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

@R_BUTN_PIN_value = external dso_local global i8, align 1
@R_MAIN_PIN_value = external dso_local global i8, align 1
@R_SECD_PIN_value = external dso_local global i8, align 1
@R_BUTN_PIN_callback = external dso_local global ptr, align 8
@R_MAIN_PIN_callback = external dso_local global ptr, align 8
@.str.9 = external hidden unnamed_addr constant [23 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

declare void @serialWriteString(ptr noundef) #1

declare void @serialWrite(ptr noundef, i32 noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @handleInput() #2 {
  %1 = alloca i32, align 4
  %2 = alloca i8, align 1
  %3 = alloca i8, align 1
  %4 = alloca i8, align 1
  br label %5

5:                                                ; preds = %45, %0
  %6 = call zeroext i1 @serialHasData()
  br i1 %6, label %7, label %46

7:                                                ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %1) #3
  %8 = call i32 @serialRead()
  store i32 %8, ptr %1, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 1, ptr %2) #3
  %9 = load i32, ptr %1, align 4, !tbaa !5
  %10 = trunc i32 %9 to i8
  store i8 %10, ptr %2, align 1, !tbaa !9
  %11 = load i8, ptr %2, align 1, !tbaa !9
  %12 = sext i8 %11 to i32
  switch i32 %12, label %44 [
    i32 106, label %13
    i32 107, label %13
    i32 104, label %29
    i32 108, label %29
  ]

13:                                               ; preds = %7, %7
  call void @llvm.lifetime.start.p0(i64 1, ptr %3) #3
  %14 = load i8, ptr %2, align 1, !tbaa !9
  %15 = sext i8 %14 to i32
  %16 = icmp eq i32 %15, 106
  %17 = zext i1 %16 to i64
  %18 = select i1 %16, i32 0, i32 1
  %19 = trunc i32 %18 to i8
  store i8 %19, ptr %3, align 1, !tbaa !9
  %20 = load i8, ptr @R_BUTN_PIN_value, align 1, !tbaa !9
  %21 = zext i8 %20 to i32
  %22 = load i8, ptr %3, align 1, !tbaa !9
  %23 = zext i8 %22 to i32
  %24 = icmp ne i32 %21, %23
  br i1 %24, label %25, label %27

25:                                               ; preds = %13
  %26 = load ptr, ptr @R_BUTN_PIN_callback, align 8, !tbaa !10
  call void %26()
  br label %27

27:                                               ; preds = %25, %13
  %28 = load i8, ptr %3, align 1, !tbaa !9
  store i8 %28, ptr @R_BUTN_PIN_value, align 1, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %3) #3
  br label %45

29:                                               ; preds = %7, %7
  %30 = load i8, ptr @R_MAIN_PIN_value, align 1, !tbaa !9
  %31 = zext i8 %30 to i32
  %32 = icmp eq i32 %31, 1
  br i1 %32, label %33, label %41

33:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 1, ptr %4) #3
  %34 = load i8, ptr %2, align 1, !tbaa !9
  %35 = sext i8 %34 to i32
  %36 = icmp eq i32 %35, 104
  %37 = zext i1 %36 to i64
  %38 = select i1 %36, i32 0, i32 1
  %39 = trunc i32 %38 to i8
  store i8 %39, ptr %4, align 1, !tbaa !9
  store i8 0, ptr @R_MAIN_PIN_value, align 1, !tbaa !9
  %40 = load i8, ptr %4, align 1, !tbaa !9
  store i8 %40, ptr @R_SECD_PIN_value, align 1, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %4) #3
  br label %42

41:                                               ; preds = %29
  store i8 1, ptr @R_MAIN_PIN_value, align 1, !tbaa !9
  br label %42

42:                                               ; preds = %41, %33
  %43 = load ptr, ptr @R_MAIN_PIN_callback, align 8, !tbaa !10
  call void %43()
  br label %45

44:                                               ; preds = %7
  call void @serialWriteString(ptr noundef @.str.9)
  call void @serialWrite(ptr noundef %2, i32 noundef 1)
  call void @serialWriteNewLine()
  br label %45

45:                                               ; preds = %44, %42, %27
  call void @llvm.lifetime.end.p0(i64 1, ptr %2) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %1) #3
  br label %5, !llvm.loop !12

46:                                               ; preds = %5
  ret void
}

declare zeroext i1 @serialHasData() #1

declare i32 @serialRead() #1

declare void @serialWriteNewLine() #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

@R_BUTN_PIN_value = external dso_local local_unnamed_addr global i8, align 1
@R_MAIN_PIN_value = external dso_local local_unnamed_addr global i8, align 1
@R_SECD_PIN_value = external dso_local local_unnamed_addr global i8, align 1
@R_BUTN_PIN_callback = external dso_local local_unnamed_addr global ptr, align 8
@R_MAIN_PIN_callback = external dso_local local_unnamed_addr global ptr, align 8
@.str.9 = external hidden unnamed_addr constant [23 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

declare void @serialWriteString(ptr noundef) local_unnamed_addr #1

declare void @serialWrite(ptr noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local void @handleInput() local_unnamed_addr #2 {
  %1 = alloca i8, align 1
  %2 = tail call zeroext i1 @serialHasData() #3
  br i1 %2, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %0, %22
  %3 = call i32 @serialRead() #3
  call void @llvm.lifetime.start.p0(i64 1, ptr nonnull %1) #3
  %4 = trunc i32 %3 to i8
  store i8 %4, ptr %1, align 1, !tbaa !5
  %sext = shl i32 %3, 24
  %5 = ashr exact i32 %sext, 24
  switch i32 %5, label %21 [
    i32 106, label %6
    i32 107, label %6
    i32 104, label %13
    i32 108, label %13
  ]

6:                                                ; preds = %.lr.ph, %.lr.ph
  %7 = icmp ne i8 %4, 106
  %8 = zext i1 %7 to i8
  %9 = load i8, ptr @R_BUTN_PIN_value, align 1, !tbaa !5
  %.not = icmp eq i8 %9, %8
  br i1 %.not, label %12, label %10

10:                                               ; preds = %6
  %11 = load ptr, ptr @R_BUTN_PIN_callback, align 8, !tbaa !8
  call void %11() #3
  br label %12

12:                                               ; preds = %10, %6
  store i8 %8, ptr @R_BUTN_PIN_value, align 1, !tbaa !5
  br label %22

13:                                               ; preds = %.lr.ph, %.lr.ph
  %14 = load i8, ptr @R_MAIN_PIN_value, align 1, !tbaa !5
  %15 = icmp eq i8 %14, 1
  br i1 %15, label %16, label %19

16:                                               ; preds = %13
  %17 = icmp ne i8 %4, 104
  %18 = zext i1 %17 to i8
  store i8 %18, ptr @R_SECD_PIN_value, align 1, !tbaa !5
  br label %19

19:                                               ; preds = %13, %16
  %.sink = phi i8 [ 0, %16 ], [ 1, %13 ]
  store i8 %.sink, ptr @R_MAIN_PIN_value, align 1
  %20 = load ptr, ptr @R_MAIN_PIN_callback, align 8, !tbaa !8
  call void %20() #3
  br label %22

21:                                               ; preds = %.lr.ph
  call void @serialWriteString(ptr noundef nonnull @.str.9) #3
  call void @serialWrite(ptr noundef nonnull %1, i32 noundef 1) #3
  call void @serialWriteNewLine() #3
  br label %22

22:                                               ; preds = %21, %19, %12
  call void @llvm.lifetime.end.p0(i64 1, ptr nonnull %1) #3
  %23 = call zeroext i1 @serialHasData() #3
  br i1 %23, label %.lr.ph, label %._crit_edge, !llvm.loop !10

._crit_edge:                                      ; preds = %22, %0
  ret void
}

declare zeroext i1 @serialHasData() local_unnamed_addr #1

declare i32 @serialRead() local_unnamed_addr #1

declare void @serialWriteNewLine() local_unnamed_addr #1

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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