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

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

@mSendFunc = external hidden global ptr, align 8
@mPayLoadBuf = external hidden global ptr, align 8
@mPayLoadBufLen = external hidden global i32, align 4
@dhconnector_websocket_parse.pong_buf = external hidden global [2 x i8], align 1
@dhconnector_websocket_parse.str = external hidden constant [36 x i8], align 4
@dhconnector_websocket_parse.str.1 = external hidden constant [42 x i8], align 4
@dhconnector_websocket_parse.str.2 = external hidden constant [54 x i8], align 4
@dhconnector_websocket_parse.str.3 = external hidden constant [34 x i8], align 4
@mRepeatTimer = external hidden global %struct._ETSTIMER_, align 8

; Function Attrs: nounwind uwtable
declare hidden void @check_queue() #0

; Function Attrs: nounwind uwtable
declare hidden void @arm_timeout_timer(i32 noundef) #0

; Function Attrs: nounwind uwtable
define dso_local void @dhconnector_websocket_parse(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %7 = load i32, ptr %4, align 4, !tbaa !9
  %8 = icmp eq i32 %7, 2
  br i1 %8, label %9, label %29

9:                                                ; preds = %2
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds i8, ptr %10, i64 0
  %12 = load i8, ptr %11, align 1, !tbaa !11
  %13 = sext i8 %12 to i32
  %14 = icmp eq i32 %13, 137
  br i1 %14, label %15, label %28

15:                                               ; preds = %9
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 1
  %18 = load i8, ptr %17, align 1, !tbaa !11
  %19 = sext i8 %18 to i32
  %20 = icmp eq i32 %19, 0
  br i1 %20, label %21, label %28

21:                                               ; preds = %15
  %22 = call i32 (...) @dhconnector_websocket_api_check()
  %23 = icmp ne i32 %22, 0
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  call void @arm_timeout_timer(i32 noundef 120000)
  br label %25

25:                                               ; preds = %24, %21
  %26 = load ptr, ptr @mSendFunc, align 8, !tbaa !5
  %27 = call i32 %26(ptr noundef @dhconnector_websocket_parse.pong_buf, i32 noundef 2)
  br label %133

28:                                               ; preds = %15, %9
  br label %29

29:                                               ; preds = %28, %2
  br label %30

30:                                               ; preds = %130, %29
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds i8, ptr %31, i64 0
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = sext i8 %33 to i32
  %35 = and i32 %34, 128
  %36 = icmp eq i32 %35, 0
  br i1 %36, label %37, label %44

37:                                               ; preds = %30
  br label %38

38:                                               ; preds = %37
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds i8, ptr %39, i64 0
  %41 = load i8, ptr %40, align 1, !tbaa !11
  %42 = sext i8 %41 to i32
  call void (ptr, ...) @dhdebug_ram(ptr noundef @dhconnector_websocket_parse.str, i32 noundef %42)
  br label %43

43:                                               ; preds = %38
  call void @error()
  br label %133

44:                                               ; preds = %30
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds i8, ptr %45, i64 1
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = sext i8 %47 to i32
  %49 = and i32 %48, 128
  %50 = icmp ne i32 %49, 0
  br i1 %50, label %51, label %54

51:                                               ; preds = %44
  br label %52

52:                                               ; preds = %51
  call void (ptr, ...) @dhdebug_ram(ptr noundef @dhconnector_websocket_parse.str.1)
  br label %53

53:                                               ; preds = %52
  call void @error()
  br label %133

54:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds i8, ptr %55, i64 1
  %57 = load i8, ptr %56, align 1, !tbaa !11
  %58 = sext i8 %57 to i32
  %59 = and i32 %58, 127
  store i32 %59, ptr %5, align 4, !tbaa !9
  %60 = load i32, ptr %5, align 4, !tbaa !9
  %61 = icmp eq i32 %60, 127
  br i1 %61, label %62, label %66

62:                                               ; preds = %54
  br label %63

63:                                               ; preds = %62
  call void (ptr, ...) @dhdebug_ram(ptr noundef @dhconnector_websocket_parse.str.2)
  br label %64

64:                                               ; preds = %63
  br label %65

65:                                               ; preds = %64
  call void @error()
  store i32 1, ptr %6, align 4
  br label %127

66:                                               ; preds = %54
  %67 = load i32, ptr %5, align 4, !tbaa !9
  %68 = icmp eq i32 %67, 126
  br i1 %68, label %69, label %86

69:                                               ; preds = %66
  %70 = load ptr, ptr %3, align 8, !tbaa !5
  %71 = getelementptr inbounds i8, ptr %70, i64 2
  %72 = load i8, ptr %71, align 1, !tbaa !11
  %73 = sext i8 %72 to i32
  store i32 %73, ptr %5, align 4, !tbaa !9
  %74 = load i32, ptr %5, align 4, !tbaa !9
  %75 = shl i32 %74, 8
  store i32 %75, ptr %5, align 4, !tbaa !9
  %76 = load ptr, ptr %3, align 8, !tbaa !5
  %77 = getelementptr inbounds i8, ptr %76, i64 3
  %78 = load i8, ptr %77, align 1, !tbaa !11
  %79 = sext i8 %78 to i32
  %80 = load i32, ptr %5, align 4, !tbaa !9
  %81 = or i32 %80, %79
  store i32 %81, ptr %5, align 4, !tbaa !9
  %82 = load ptr, ptr %3, align 8, !tbaa !5
  %83 = getelementptr inbounds i8, ptr %82, i64 4
  store ptr %83, ptr %3, align 8, !tbaa !5
  %84 = load i32, ptr %4, align 4, !tbaa !9
  %85 = sub i32 %84, 4
  store i32 %85, ptr %4, align 4, !tbaa !9
  br label %95

86:                                               ; preds = %66
  %87 = load i32, ptr %5, align 4, !tbaa !9
  %88 = icmp ult i32 %87, 126
  br i1 %88, label %89, label %94

89:                                               ; preds = %86
  %90 = load ptr, ptr %3, align 8, !tbaa !5
  %91 = getelementptr inbounds i8, ptr %90, i64 2
  store ptr %91, ptr %3, align 8, !tbaa !5
  %92 = load i32, ptr %4, align 4, !tbaa !9
  %93 = sub i32 %92, 2
  store i32 %93, ptr %4, align 4, !tbaa !9
  br label %94

94:                                               ; preds = %89, %86
  br label %95

95:                                               ; preds = %94, %69
  br label %96

96:                                               ; preds = %95
  %97 = load i32, ptr %5, align 4, !tbaa !9
  %98 = load i32, ptr %4, align 4, !tbaa !9
  %99 = icmp ugt i32 %97, %98
  br i1 %99, label %100, label %104

100:                                              ; preds = %96
  br label %101

101:                                              ; preds = %100
  call void (ptr, ...) @dhdebug_ram(ptr noundef @dhconnector_websocket_parse.str.3)
  br label %102

102:                                              ; preds = %101
  br label %103

103:                                              ; preds = %102
  call void @error()
  store i32 1, ptr %6, align 4
  br label %127

104:                                              ; preds = %96
  call void @ets_timer_disarm(ptr noundef @mRepeatTimer)
  %105 = load ptr, ptr %3, align 8, !tbaa !5
  %106 = load i32, ptr %5, align 4, !tbaa !9
  %107 = load ptr, ptr @mPayLoadBuf, align 8, !tbaa !5
  %108 = call i32 @dhconnector_websocket_api_communicate(ptr noundef %105, i32 noundef %106, ptr noundef %107, i32 noundef 2048)
  store i32 %108, ptr @mPayLoadBufLen, align 4, !tbaa !9
  %109 = load i32, ptr @mPayLoadBufLen, align 4, !tbaa !9
  %110 = icmp sgt i32 %109, 0
  br i1 %110, label %111, label %113

111:                                              ; preds = %104
  %112 = call i32 @send_payload()
  br label %119

113:                                              ; preds = %104
  %114 = load i32, ptr @mPayLoadBufLen, align 4, !tbaa !9
  %115 = icmp eq i32 %114, -1
  br i1 %115, label %116, label %117

116:                                              ; preds = %113
  call void @error()
  store i32 1, ptr %6, align 4
  br label %127

117:                                              ; preds = %113
  call void @arm_timeout_timer(i32 noundef 120000)
  call void @check_queue()
  br label %118

118:                                              ; preds = %117
  br label %119

119:                                              ; preds = %118, %111
  %120 = load i32, ptr %5, align 4, !tbaa !9
  %121 = load i32, ptr %4, align 4, !tbaa !9
  %122 = sub i32 %121, %120
  store i32 %122, ptr %4, align 4, !tbaa !9
  %123 = load i32, ptr %5, align 4, !tbaa !9
  %124 = load ptr, ptr %3, align 8, !tbaa !5
  %125 = zext i32 %123 to i64
  %126 = getelementptr inbounds i8, ptr %124, i64 %125
  store ptr %126, ptr %3, align 8, !tbaa !5
  store i32 0, ptr %6, align 4
  br label %127

127:                                              ; preds = %119, %116, %103, %65
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  %128 = load i32, ptr %6, align 4
  switch i32 %128, label %134 [
    i32 0, label %129
    i32 1, label %133
  ]

129:                                              ; preds = %127
  br label %130

130:                                              ; preds = %129
  %131 = load i32, ptr %4, align 4, !tbaa !9
  %132 = icmp ne i32 %131, 0
  br i1 %132, label %30, label %133, !llvm.loop !12

133:                                              ; preds = %130, %127, %53, %43, %25
  ret void

134:                                              ; preds = %127
  unreachable
}

declare i32 @dhconnector_websocket_api_check(...) #1

declare void @dhdebug_ram(ptr noundef, ...) #1

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

declare void @ets_timer_disarm(ptr noundef) #1

declare i32 @dhconnector_websocket_api_communicate(ptr noundef, i32 noundef, 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) #2

; Function Attrs: nounwind uwtable
declare hidden i32 @send_payload() #0

; Function Attrs: nounwind uwtable
declare hidden void @error() #0

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 = { "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 = { 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 = !{!7, !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"

%struct._ETSTIMER_ = type { ptr, i32, i32, ptr, ptr }

@mPayLoadBuf = external hidden local_unnamed_addr global ptr, align 8
@mPayLoadBufLen = external hidden local_unnamed_addr global i32, align 4
@dhconnector_websocket_parse.str = external hidden constant [36 x i8], align 4
@dhconnector_websocket_parse.str.1 = external hidden constant [42 x i8], align 4
@dhconnector_websocket_parse.str.2 = external hidden constant [54 x i8], align 4
@dhconnector_websocket_parse.str.3 = external hidden constant [34 x i8], align 4
@mRepeatTimer = external hidden global %struct._ETSTIMER_, align 8

; Function Attrs: nounwind uwtable
declare hidden void @check_queue() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @arm_timeout_timer(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @dhconnector_websocket_parse(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  br label %3

3:                                                ; preds = %40, %2
  %.029 = phi ptr [ %0, %2 ], [ %43, %40 ]
  %.028 = phi i32 [ %1, %2 ], [ %41, %40 ]
  %4 = load i8, ptr %.029, align 1, !tbaa !5
  %5 = sext i8 %4 to i32
  %6 = and i32 %5, 128
  %7 = icmp eq i32 %6, 0
  br i1 %7, label %8, label %9

8:                                                ; preds = %3
  tail call void (ptr, ...) @dhdebug_ram(ptr noundef nonnull @dhconnector_websocket_parse.str, i32 noundef %5) #2
  br label %.thread.sink.split

9:                                                ; preds = %3
  %10 = getelementptr inbounds i8, ptr %.029, i64 1
  %11 = load i8, ptr %10, align 1, !tbaa !5
  %12 = sext i8 %11 to i32
  %13 = and i32 %12, 128
  %.not = icmp eq i32 %13, 0
  br i1 %.not, label %15, label %14

14:                                               ; preds = %9
  tail call void (ptr, ...) @dhdebug_ram(ptr noundef nonnull @dhconnector_websocket_parse.str.1) #2
  br label %.thread.sink.split

15:                                               ; preds = %9
  switch i8 %11, label %26 [
    i8 127, label %16
    i8 126, label %17
  ]

16:                                               ; preds = %15
  tail call void (ptr, ...) @dhdebug_ram(ptr noundef nonnull @dhconnector_websocket_parse.str.2) #2
  br label %.thread.sink.split

17:                                               ; preds = %15
  %18 = getelementptr inbounds i8, ptr %.029, i64 2
  %19 = load i8, ptr %18, align 1, !tbaa !5
  %20 = sext i8 %19 to i32
  %21 = shl nsw i32 %20, 8
  %22 = getelementptr inbounds i8, ptr %.029, i64 3
  %23 = load i8, ptr %22, align 1, !tbaa !5
  %24 = sext i8 %23 to i32
  %25 = or i32 %21, %24
  br label %26

26:                                               ; preds = %15, %17
  %.sink55 = phi i64 [ 4, %17 ], [ 2, %15 ]
  %.sink = phi i32 [ -4, %17 ], [ -2, %15 ]
  %.027 = phi i32 [ %25, %17 ], [ %12, %15 ]
  %27 = getelementptr inbounds i8, ptr %.029, i64 %.sink55
  %28 = add i32 %.028, %.sink
  %29 = icmp ugt i32 %.027, %28
  br i1 %29, label %30, label %31

30:                                               ; preds = %26
  tail call void (ptr, ...) @dhdebug_ram(ptr noundef nonnull @dhconnector_websocket_parse.str.3) #2
  br label %.thread.sink.split

31:                                               ; preds = %26
  tail call void @ets_timer_disarm(ptr noundef nonnull @mRepeatTimer) #2
  %32 = load ptr, ptr @mPayLoadBuf, align 8, !tbaa !8
  %33 = tail call i32 @dhconnector_websocket_api_communicate(ptr noundef nonnull %27, i32 noundef %.027, ptr noundef %32, i32 noundef 2048) #2
  store i32 %33, ptr @mPayLoadBufLen, align 4, !tbaa !10
  %34 = icmp sgt i32 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %31
  %36 = tail call i32 @send_payload()
  br label %40

37:                                               ; preds = %31
  %38 = icmp eq i32 %33, -1
  br i1 %38, label %.thread.sink.split, label %39

39:                                               ; preds = %37
  tail call void @arm_timeout_timer(i32 noundef 120000)
  tail call void @check_queue()
  br label %40

40:                                               ; preds = %35, %39
  %41 = sub i32 %28, %.027
  %42 = zext i32 %.027 to i64
  %43 = getelementptr inbounds i8, ptr %27, i64 %42
  %.not34 = icmp eq i32 %41, 0
  br i1 %.not34, label %.thread, label %3, !llvm.loop !12

.thread.sink.split:                               ; preds = %37, %8, %14, %16, %30
  tail call void @error()
  br label %.thread

.thread:                                          ; preds = %40, %.thread.sink.split
  ret void
}

declare void @dhdebug_ram(ptr noundef, ...) local_unnamed_addr #1

declare void @ets_timer_disarm(ptr noundef) local_unnamed_addr #1

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

; Function Attrs: nounwind uwtable
declare hidden i32 @send_payload() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @error() local_unnamed_addr #0

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 = { "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 }

!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 = !{!11, !11, i64 0}
!11 = !{!"int", !6, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass