; LLVM IR from devicehive/esp8266-firmware :: firmware-src/sources/commands (uart_cmd)

; 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.gpio_command_params = type { i16, i16, i16, i16, i16, i16, i16, i16, i16, i16, i16, i32, i32, %union.anon, i16, i32, i8, i8, i8, [264 x i8], i32, i32, i32, i32, i32, i32, i32, i32, float }
%union.anon = type { [16 x i32] }

@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [40 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

declare ptr @parse_params_pins_set(ptr noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) #1

declare void @dh_command_fail(ptr noundef, ptr noundef) #1

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

declare void @dh_uart_set_mode(i32 noundef) #1

declare void @dh_uart_send_buf(ptr noundef, i32 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
define dso_local void @dh_handle_uart_read(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca %struct.gpio_command_params, align 4
  %10 = alloca i32, align 4
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  store i32 %3, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 408, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = icmp ne i32 %15, 0
  br i1 %16, label %17, label %53

17:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = call ptr @parse_params_pins_set(ptr noundef %18, i32 noundef %19, ptr noundef %9, i32 noundef 1, i32 noundef 0, i32 noundef 360448, ptr noundef %10)
  store ptr %20, ptr %11, align 8, !tbaa !5
  %21 = load ptr, ptr %11, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %26

23:                                               ; preds = %17
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load ptr, ptr %11, align 8, !tbaa !5
  call void @dh_command_fail(ptr noundef %24, ptr noundef %25)
  store i32 1, ptr %12, align 4
  br label %50

26:                                               ; preds = %17
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = load i32, ptr %10, align 4, !tbaa !9
  %29 = call i32 @uart_init(ptr noundef %27, i32 noundef %28, ptr noundef %9, i32 noundef 0)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %31, label %32

31:                                               ; preds = %26
  store i32 1, ptr %12, align 4
  br label %50

32:                                               ; preds = %26
  %33 = load i32, ptr %10, align 4, !tbaa !9
  %34 = and i32 %33, 262144
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %49

36:                                               ; preds = %32
  %37 = getelementptr inbounds %struct.gpio_command_params, ptr %9, i32 0, i32 21
  %38 = load i32, ptr %37, align 4, !tbaa !11
  %39 = icmp ugt i32 %38, 1000
  br i1 %39, label %40, label %42

40:                                               ; preds = %36
  %41 = load ptr, ptr %5, align 8, !tbaa !5
  call void @dh_command_fail(ptr noundef %41, ptr noundef @.str.1)
  store i32 1, ptr %12, align 4
  br label %50

42:                                               ; preds = %36
  %43 = load i32, ptr %10, align 4, !tbaa !9
  %44 = and i32 %43, 65536
  %45 = icmp ne i32 %44, 0
  br i1 %45, label %48, label %46

46:                                               ; preds = %42
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  call void @dh_command_fail(ptr noundef %47, ptr noundef @.str.2)
  store i32 1, ptr %12, align 4
  br label %50

48:                                               ; preds = %42
  br label %49

49:                                               ; preds = %48, %32
  store i32 0, ptr %12, align 4
  br label %50

50:                                               ; preds = %49, %46, %40, %31, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  %51 = load i32, ptr %12, align 4
  switch i32 %51, label %80 [
    i32 0, label %52
  ]

52:                                               ; preds = %50
  br label %53

53:                                               ; preds = %52, %4
  %54 = load i32, ptr %10, align 4, !tbaa !9
  %55 = and i32 %54, 65536
  %56 = icmp ne i32 %55, 0
  br i1 %56, label %57, label %71

57:                                               ; preds = %53
  call void @dh_uart_set_mode(i32 noundef 2)
  %58 = getelementptr inbounds %struct.gpio_command_params, ptr %9, i32 0, i32 19
  %59 = getelementptr inbounds [264 x i8], ptr %58, i64 0, i64 0
  %60 = getelementptr inbounds %struct.gpio_command_params, ptr %9, i32 0, i32 20
  %61 = load i32, ptr %60, align 4, !tbaa !15
  call void @dh_uart_send_buf(ptr noundef %59, i32 noundef %61)
  call void @system_soft_wdt_feed()
  %62 = load i32, ptr %10, align 4, !tbaa !9
  %63 = and i32 %62, 262144
  %64 = icmp ne i32 %63, 0
  br i1 %64, label %65, label %68

65:                                               ; preds = %57
  %66 = getelementptr inbounds %struct.gpio_command_params, ptr %9, i32 0, i32 21
  %67 = load i32, ptr %66, align 4, !tbaa !11
  br label %69

68:                                               ; preds = %57
  br label %69

69:                                               ; preds = %68, %65
  %70 = phi i32 [ %67, %65 ], [ 250, %68 ]
  call void @delay_ms(i32 noundef %70)
  call void @system_soft_wdt_feed()
  br label %71

71:                                               ; preds = %69, %53
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store ptr null, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %72 = call i32 @dh_uart_get_buf(ptr noundef %13)
  store i32 %72, ptr %14, align 4, !tbaa !9
  %73 = load i32, ptr %14, align 4, !tbaa !9
  %74 = icmp ugt i32 %73, 264
  br i1 %74, label %75, label %76

75:                                               ; preds = %71
  store i32 264, ptr %14, align 4, !tbaa !9
  br label %76

76:                                               ; preds = %75, %71
  %77 = load ptr, ptr %5, align 8, !tbaa !5
  %78 = load ptr, ptr %13, align 8, !tbaa !5
  %79 = load i32, ptr %14, align 4, !tbaa !9
  call void @dh_command_done_buf(ptr noundef %77, ptr noundef %78, i32 noundef %79)
  call void @dh_uart_set_mode(i32 noundef 2)
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #3
  store i32 0, ptr %12, align 4
  br label %80

80:                                               ; preds = %76, %50
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 408, ptr %9) #3
  %81 = load i32, ptr %12, align 4
  switch i32 %81, label %83 [
    i32 0, label %82
    i32 1, label %82
  ]

82:                                               ; preds = %80, %80
  ret void

83:                                               ; preds = %80
  unreachable
}

declare void @system_soft_wdt_feed() #1

declare void @delay_ms(i32 noundef) #1

declare i32 @dh_uart_get_buf(ptr noundef) #1

declare void @dh_command_done_buf(ptr noundef, ptr noundef, i32 noundef) #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 = !{!"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 = !{!12, !10, i64 376}
!12 = !{!"", !13, i64 0, !13, i64 2, !13, i64 4, !13, i64 6, !13, i64 8, !13, i64 10, !13, i64 12, !13, i64 14, !13, i64 16, !13, i64 18, !13, i64 20, !10, i64 24, !10, i64 28, !7, i64 32, !13, i64 96, !10, i64 100, !7, i64 104, !7, i64 105, !7, i64 106, !7, i64 107, !10, i64 372, !10, i64 376, !10, i64 380, !10, i64 384, !10, i64 388, !10, i64 392, !10, i64 396, !10, i64 400, !14, i64 404}
!13 = !{!"short", !7, i64 0}
!14 = !{!"float", !7, i64 0}
!15 = !{!12, !10, i64 372}


; 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.gpio_command_params = type { i16, i16, i16, i16, i16, i16, i16, i16, i16, i16, i16, i32, i32, %union.anon, i16, i32, i8, i8, i8, [264 x i8], i32, i32, i32, i32, i32, i32, i32, i32, float }
%union.anon = type { [16 x i32] }

@.str.1 = external hidden unnamed_addr constant [21 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [40 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

declare ptr @parse_params_pins_set(ptr noundef, i32 noundef, ptr noundef, i32 noundef, i32 noundef, i32 noundef, ptr noundef) local_unnamed_addr #1

declare void @dh_command_fail(ptr noundef, ptr noundef) local_unnamed_addr #1

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

declare void @dh_uart_set_mode(i32 noundef) local_unnamed_addr #1

declare void @dh_uart_send_buf(ptr noundef, i32 noundef) 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
define dso_local void @dh_handle_uart_read(ptr noundef %0, ptr nocapture noundef readnone %1, ptr noundef %2, i32 noundef %3) local_unnamed_addr #2 {
  %5 = alloca %struct.gpio_command_params, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  call void @llvm.lifetime.start.p0(i64 408, ptr nonnull %5) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %6) #4
  store i32 0, ptr %6, align 4, !tbaa !5
  %.not = icmp eq i32 %3, 0
  br i1 %.not, label %.thread, label %8

8:                                                ; preds = %4
  %9 = call ptr @parse_params_pins_set(ptr noundef %2, i32 noundef %3, ptr noundef nonnull %5, i32 noundef 1, i32 noundef 0, i32 noundef 360448, ptr noundef nonnull %6) #4
  %.not14 = icmp eq ptr %9, null
  br i1 %.not14, label %11, label %10

10:                                               ; preds = %8
  call void @dh_command_fail(ptr noundef %0, ptr noundef nonnull %9) #4
  br label %.critedge

11:                                               ; preds = %8
  %12 = load i32, ptr %6, align 4, !tbaa !5
  %13 = call i32 @uart_init(ptr noundef %0, i32 noundef %12, ptr noundef nonnull %5, i32 noundef 0)
  %.not15 = icmp eq i32 %13, 0
  br i1 %.not15, label %14, label %.critedge

14:                                               ; preds = %11
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = and i32 %15, 262144
  %.not16 = icmp eq i32 %16, 0
  br i1 %.not16, label %25, label %17

17:                                               ; preds = %14
  %18 = getelementptr inbounds i8, ptr %5, i64 376
  %19 = load i32, ptr %18, align 4, !tbaa !9
  %20 = icmp ugt i32 %19, 1000
  br i1 %20, label %21, label %22

21:                                               ; preds = %17
  call void @dh_command_fail(ptr noundef %0, ptr noundef nonnull @.str.1) #4
  br label %.critedge

22:                                               ; preds = %17
  %23 = and i32 %15, 65536
  %.not17 = icmp eq i32 %23, 0
  br i1 %.not17, label %24, label %25

24:                                               ; preds = %22
  call void @dh_command_fail(ptr noundef %0, ptr noundef nonnull @.str.2) #4
  br label %.critedge

25:                                               ; preds = %22, %14
  %26 = and i32 %15, 65536
  %.not18 = icmp eq i32 %26, 0
  br i1 %.not18, label %.thread, label %27

27:                                               ; preds = %25
  call void @dh_uart_set_mode(i32 noundef 2) #4
  %28 = getelementptr inbounds i8, ptr %5, i64 107
  %29 = getelementptr inbounds i8, ptr %5, i64 372
  %30 = load i32, ptr %29, align 4, !tbaa !13
  call void @dh_uart_send_buf(ptr noundef nonnull %28, i32 noundef %30) #4
  call void @system_soft_wdt_feed() #4
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = and i32 %31, 262144
  %.not19 = icmp eq i32 %32, 0
  %33 = getelementptr inbounds i8, ptr %5, i64 376
  %34 = load i32, ptr %33, align 4
  %35 = select i1 %.not19, i32 250, i32 %34
  call void @delay_ms(i32 noundef %35) #4
  call void @system_soft_wdt_feed() #4
  br label %.thread

.thread:                                          ; preds = %4, %27, %25
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %7) #4
  store ptr null, ptr %7, align 8, !tbaa !14
  %36 = call i32 @dh_uart_get_buf(ptr noundef nonnull %7) #4
  %spec.store.select = call i32 @llvm.umin.i32(i32 %36, i32 264)
  %37 = load ptr, ptr %7, align 8, !tbaa !14
  call void @dh_command_done_buf(ptr noundef %0, ptr noundef %37, i32 noundef %spec.store.select) #4
  call void @dh_uart_set_mode(i32 noundef 2) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %7) #4
  br label %.critedge

.critedge:                                        ; preds = %10, %21, %24, %11, %.thread
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %6) #4
  call void @llvm.lifetime.end.p0(i64 408, ptr nonnull %5) #4
  ret void
}

declare void @system_soft_wdt_feed() local_unnamed_addr #1

declare void @delay_ms(i32 noundef) local_unnamed_addr #1

declare i32 @dh_uart_get_buf(ptr noundef) local_unnamed_addr #1

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

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

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 = { 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"}
!9 = !{!10, !6, i64 376}
!10 = !{!"", !11, i64 0, !11, i64 2, !11, i64 4, !11, i64 6, !11, i64 8, !11, i64 10, !11, i64 12, !11, i64 14, !11, i64 16, !11, i64 18, !11, i64 20, !6, i64 24, !6, i64 28, !7, i64 32, !11, i64 96, !6, i64 100, !7, i64 104, !7, i64 105, !7, i64 106, !7, i64 107, !6, i64 372, !6, i64 376, !6, i64 380, !6, i64 384, !6, i64 388, !6, i64 392, !6, i64 396, !6, i64 400, !12, i64 404}
!11 = !{!"short", !7, i64 0}
!12 = !{!"float", !7, i64 0}
!13 = !{!10, !6, i64 372}
!14 = !{!15, !15, i64 0}
!15 = !{!"any pointer", !7, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass