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

; 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.IronStruct = type { %struct.HistoryStruct, i32, i8, i8, i32, i8, i8, i8, i32, [2 x i32], i8, i32, i32, i16, i16, i8, i16, i8, i8, i8, i16, i32, i32, i8, i32, i32, i32 }
%struct.HistoryStruct = type { i8, [8 x i32] }

declare i32 @analogRead(i8 noundef zeroext) #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @Iron_temp(ptr noundef %0) #1 {
  %2 = alloca i16, align 2
  %3 = alloca ptr, align 8
  %4 = alloca i16, align 2
  %5 = alloca i16, align 2
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 2, ptr %4) #4
  %7 = load ptr, ptr %3, align 8, !tbaa !5
  %8 = getelementptr inbounds %struct.IronStruct, ptr %7, i32 0, i32 3
  %9 = load i8, ptr %8, align 1, !tbaa !9
  %10 = call i32 @analogRead(i8 noundef zeroext %9)
  %11 = trunc i32 %10 to i16
  store i16 %11, ptr %4, align 2, !tbaa !15
  call void @delay(i32 noundef 50)
  call void @llvm.lifetime.start.p0(i64 2, ptr %5) #4
  %12 = load ptr, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.IronStruct, ptr %12, i32 0, i32 3
  %14 = load i8, ptr %13, align 1, !tbaa !9
  %15 = call i32 @analogRead(i8 noundef zeroext %14)
  %16 = trunc i32 %15 to i16
  store i16 %16, ptr %5, align 2, !tbaa !15
  %17 = load i16, ptr %4, align 2, !tbaa !15
  %18 = zext i16 %17 to i32
  %19 = load i16, ptr %5, align 2, !tbaa !15
  %20 = zext i16 %19 to i32
  %21 = sub nsw i32 %18, %20
  %22 = call i32 @llvm.abs.i32(i32 %21, i1 true)
  %23 = icmp slt i32 %22, 50
  br i1 %23, label %24, label %37

24:                                               ; preds = %1
  %25 = load i16, ptr %5, align 2, !tbaa !15
  %26 = zext i16 %25 to i32
  %27 = add nsw i32 %26, 1
  %28 = load i16, ptr %4, align 2, !tbaa !15
  %29 = zext i16 %28 to i32
  %30 = add nsw i32 %29, %27
  %31 = trunc i32 %30 to i16
  store i16 %31, ptr %4, align 2, !tbaa !15
  %32 = load i16, ptr %4, align 2, !tbaa !15
  %33 = zext i16 %32 to i32
  %34 = ashr i32 %33, 1
  %35 = trunc i32 %34 to i16
  store i16 %35, ptr %4, align 2, !tbaa !15
  %36 = load i16, ptr %4, align 2, !tbaa !15
  store i16 %36, ptr %2, align 2
  store i32 1, ptr %6, align 4
  br label %59

37:                                               ; preds = %1
  %38 = load i16, ptr %4, align 2, !tbaa !15
  %39 = zext i16 %38 to i32
  %40 = load ptr, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds %struct.IronStruct, ptr %40, i32 0, i32 9
  %42 = getelementptr inbounds [2 x i32], ptr %41, i64 0, i64 1
  %43 = load i32, ptr %42, align 4, !tbaa !16
  %44 = sub nsw i32 %39, %43
  %45 = call i32 @llvm.abs.i32(i32 %44, i1 true)
  %46 = load i16, ptr %5, align 2, !tbaa !15
  %47 = zext i16 %46 to i32
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.IronStruct, ptr %48, i32 0, i32 9
  %50 = getelementptr inbounds [2 x i32], ptr %49, i64 0, i64 1
  %51 = load i32, ptr %50, align 4, !tbaa !16
  %52 = sub nsw i32 %47, %51
  %53 = call i32 @llvm.abs.i32(i32 %52, i1 true)
  %54 = icmp slt i32 %45, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %37
  %56 = load i16, ptr %4, align 2, !tbaa !15
  store i16 %56, ptr %2, align 2
  store i32 1, ptr %6, align 4
  br label %59

57:                                               ; preds = %37
  %58 = load i16, ptr %5, align 2, !tbaa !15
  store i16 %58, ptr %2, align 2
  store i32 1, ptr %6, align 4
  br label %59

59:                                               ; preds = %57, %55, %24
  call void @llvm.lifetime.end.p0(i64 2, ptr %5) #4
  call void @llvm.lifetime.end.p0(i64 2, ptr %4) #4
  %60 = load i16, ptr %2, align 2
  ret i16 %60
}

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

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

declare void @delay(i32 noundef) #0

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #3

attributes #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 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 #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !7, i64 41}
!10 = !{!"IronStruct", !11, i64 0, !12, i64 36, !7, i64 40, !7, i64 41, !12, i64 44, !13, i64 48, !13, i64 49, !13, i64 50, !12, i64 52, !7, i64 56, !13, i64 64, !12, i64 68, !12, i64 72, !14, i64 76, !14, i64 78, !13, i64 80, !14, i64 82, !7, i64 84, !7, i64 85, !7, i64 86, !14, i64 88, !12, i64 92, !12, i64 96, !7, i64 100, !12, i64 104, !12, i64 108, !12, i64 112}
!11 = !{!"HistoryStruct", !7, i64 0, !7, i64 4}
!12 = !{!"int", !7, i64 0}
!13 = !{!"_Bool", !7, i64 0}
!14 = !{!"short", !7, i64 0}
!15 = !{!14, !14, i64 0}
!16 = !{!12, !12, 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"

declare i32 @analogRead(i8 noundef zeroext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local zeroext i16 @Iron_temp(ptr nocapture noundef readonly %0) local_unnamed_addr #1 {
  %2 = getelementptr inbounds i8, ptr %0, i64 41
  %3 = load i8, ptr %2, align 1, !tbaa !5
  %4 = tail call i32 @analogRead(i8 noundef zeroext %3) #3
  tail call void @delay(i32 noundef 50) #3
  %5 = load i8, ptr %2, align 1, !tbaa !5
  %6 = tail call i32 @analogRead(i8 noundef zeroext %5) #3
  %7 = and i32 %4, 65535
  %8 = and i32 %6, 65535
  %9 = sub nsw i32 %7, %8
  %10 = tail call i32 @llvm.abs.i32(i32 %9, i1 true)
  %11 = icmp ult i32 %10, 50
  br i1 %11, label %12, label %17

12:                                               ; preds = %1
  %13 = add i32 %4, 1
  %14 = add i32 %13, %6
  %15 = trunc i32 %14 to i16
  %16 = lshr i16 %15, 1
  br label %25

17:                                               ; preds = %1
  %18 = getelementptr inbounds i8, ptr %0, i64 60
  %19 = load i32, ptr %18, align 4, !tbaa !13
  %20 = sub nsw i32 %7, %19
  %21 = tail call i32 @llvm.abs.i32(i32 %20, i1 true)
  %22 = sub nsw i32 %8, %19
  %23 = tail call i32 @llvm.abs.i32(i32 %22, i1 true)
  %24 = icmp ult i32 %21, %23
  %..v = select i1 %24, i32 %4, i32 %6
  %. = trunc i32 %..v to i16
  br label %25

25:                                               ; preds = %17, %12
  %.0 = phi i16 [ %16, %12 ], [ %., %17 ]
  ret i16 %.0
}

declare void @delay(i32 noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.abs.i32(i32, i1 immarg) #2

attributes #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 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 #2 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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, !8, i64 41}
!6 = !{!"IronStruct", !7, i64 0, !10, i64 36, !8, i64 40, !8, i64 41, !10, i64 44, !11, i64 48, !11, i64 49, !11, i64 50, !10, i64 52, !8, i64 56, !11, i64 64, !10, i64 68, !10, i64 72, !12, i64 76, !12, i64 78, !11, i64 80, !12, i64 82, !8, i64 84, !8, i64 85, !8, i64 86, !12, i64 88, !10, i64 92, !10, i64 96, !8, i64 100, !10, i64 104, !10, i64 108, !10, i64 112}
!7 = !{!"HistoryStruct", !8, i64 0, !8, i64 4}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!"_Bool", !8, i64 0}
!12 = !{!"short", !8, i64 0}
!13 = !{!10, !10, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass