; LLVM IR from thomasfla/Linux-ESPNOW :: Lab/EspNowPacket (EspNowPacket)

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

@esppacket = external dso_local global [400 x i8], align 16
@.str.4 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [16 x i8], align 1
@stdout = external global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [47 x i8], align 1
@mac_dest = external hidden global [6 x i8], align 1
@mac_src = external hidden global [6 x i8], align 1
@payload = external hidden global [20 x i8], align 16
@len = external hidden global i32, align 4
@.str.7 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [28 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local i32 @create_raw_socket(ptr noundef) #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
define dso_local i32 @main(i32 noundef %0, ptr noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 0, ptr %3, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %12 = load ptr, ptr %5, align 8, !tbaa !9
  %13 = getelementptr inbounds ptr, ptr %12, i64 1
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  store ptr %14, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 -1, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 -1, ptr %8, align 4, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !9
  %16 = call i32 @create_raw_socket(ptr noundef %15)
  store i32 %16, ptr %7, align 4, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !5
  %18 = icmp eq i32 -1, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %2
  call void @perror(ptr noundef @.str.4)
  br label %60

20:                                               ; preds = %2
  %21 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  %22 = load ptr, ptr @stdout, align 8, !tbaa !9
  %23 = call i32 @fflush(ptr noundef %22)
  %24 = call i32 @sleep(i32 noundef 1)
  %25 = load ptr, ptr %6, align 8, !tbaa !9
  %26 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, ptr noundef %25)
  br label %27

27:                                               ; preds = %59, %20
  br label %28

28:                                               ; preds = %27
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %29 = load i32, ptr @len, align 4, !tbaa !5
  %30 = call i32 @packetSetUp(ptr noundef @esppacket, ptr noundef @mac_dest, ptr noundef @mac_src, ptr noundef @payload, i32 noundef %29)
  store i32 %30, ptr %9, align 4, !tbaa !5
  %31 = load i32, ptr %7, align 4, !tbaa !5
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = call i64 @sendto(i32 noundef %31, ptr noundef @esppacket, i64 noundef %33, i32 noundef 0, ptr noundef null, i32 noundef 0)
  %35 = trunc i64 %34 to i32
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %8, align 4, !tbaa !5
  %37 = icmp eq i32 -1, %36
  br i1 %37, label %38, label %39

38:                                               ; preds = %28
  call void @perror(ptr noundef @.str.7)
  store i32 2, ptr %11, align 4
  br label %57

39:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !5
  store i32 0, ptr %10, align 4, !tbaa !5
  br label %40

40:                                               ; preds = %51, %39
  %41 = load i32, ptr %10, align 4, !tbaa !5
  %42 = load i32, ptr %9, align 4, !tbaa !5
  %43 = icmp slt i32 %41, %42
  br i1 %43, label %44, label %54

44:                                               ; preds = %40
  %45 = load i32, ptr %10, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [400 x i8], ptr @esppacket, i64 0, i64 %46
  %48 = load i8, ptr %47, align 1, !tbaa !11
  %49 = zext i8 %48 to i32
  %50 = call i32 (ptr, ...) @printf(ptr noundef @.str.8, i32 noundef %49)
  br label %51

51:                                               ; preds = %44
  %52 = load i32, ptr %10, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %10, align 4, !tbaa !5
  br label %40, !llvm.loop !12

54:                                               ; preds = %40
  %55 = call i32 (ptr, ...) @printf(ptr noundef @.str.9)
  %56 = call i32 @usleep(i32 noundef 100)
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  store i32 0, ptr %11, align 4
  br label %57

57:                                               ; preds = %54, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  %58 = load i32, ptr %11, align 4
  switch i32 %58, label %68 [
    i32 0, label %59
    i32 2, label %60
  ]

59:                                               ; preds = %57
  br label %27

60:                                               ; preds = %57, %19
  %61 = load i32, ptr %7, align 4, !tbaa !5
  %62 = icmp sgt i32 %61, 0
  br i1 %62, label %63, label %66

63:                                               ; preds = %60
  %64 = load i32, ptr %7, align 4, !tbaa !5
  %65 = call i32 @close(i32 noundef %64)
  br label %66

66:                                               ; preds = %63, %60
  %67 = call i32 (ptr, ...) @printf(ptr noundef @.str.10)
  store i32 0, ptr %3, align 4
  store i32 1, ptr %11, align 4
  br label %68

68:                                               ; preds = %66, %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  %69 = load i32, ptr %3, align 4
  ret i32 %69
}

declare void @perror(ptr noundef) #2

declare i32 @printf(ptr noundef, ...) #2

declare i32 @fflush(ptr noundef) #2

declare i32 @sleep(i32 noundef) #2

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

declare i64 @sendto(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i32 noundef) #2

declare i32 @usleep(i32 noundef) #2

declare i32 @close(i32 noundef) #2

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 = { "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 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !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"

@esppacket = external dso_local global [400 x i8], align 16
@.str.4 = external hidden unnamed_addr constant [28 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [16 x i8], align 1
@stdout = external local_unnamed_addr global ptr, align 8
@.str.6 = external hidden unnamed_addr constant [47 x i8], align 1
@mac_dest = external hidden global [6 x i8], align 1
@mac_src = external hidden global [6 x i8], align 1
@payload = external hidden global [20 x i8], align 16
@len = external hidden local_unnamed_addr global i32, align 4
@.str.7 = external hidden unnamed_addr constant [19 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [4 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.10 = external hidden unnamed_addr constant [28 x i8], align 1

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main(i32 noundef %0, ptr nocapture noundef readonly %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds i8, ptr %1, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = tail call i32 @create_raw_socket(ptr noundef %4)
  %6 = icmp eq i32 %5, -1
  br i1 %6, label %.thread20, label %7

.thread20:                                        ; preds = %2
  tail call void @perror(ptr noundef nonnull @.str.4) #3
  br label %31

7:                                                ; preds = %2
  %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %9 = load ptr, ptr @stdout, align 8, !tbaa !5
  %10 = tail call i32 @fflush(ptr noundef %9)
  %11 = tail call i32 @sleep(i32 noundef 1) #4
  %12 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, ptr noundef %4)
  br label %13

13:                                               ; preds = %._crit_edge, %7
  %14 = load i32, ptr @len, align 4, !tbaa !9
  %15 = tail call i32 @packetSetUp(ptr noundef nonnull @esppacket, ptr noundef nonnull @mac_dest, ptr noundef nonnull @mac_src, ptr noundef nonnull @payload, i32 noundef %14)
  %16 = sext i32 %15 to i64
  %17 = tail call i64 @sendto(i32 noundef %5, ptr noundef nonnull @esppacket, i64 noundef %16, i32 noundef 0, ptr noundef null, i32 noundef 0) #4
  %18 = and i64 %17, 4294967295
  %19 = icmp eq i64 %18, 4294967295
  br i1 %19, label %27, label %.preheader

.preheader:                                       ; preds = %13
  %20 = icmp sgt i32 %15, 0
  br i1 %20, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %.preheader
  %wide.trip.count = zext nneg i32 %15 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %.lr.ph ]
  %21 = getelementptr inbounds [400 x i8], ptr @esppacket, i64 0, i64 %indvars.iv
  %22 = load i8, ptr %21, align 1, !tbaa !11
  %23 = zext i8 %22 to i32
  %24 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8, i32 noundef %23)
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !12

._crit_edge:                                      ; preds = %.lr.ph, %.preheader
  %25 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.9)
  %26 = tail call i32 @usleep(i32 noundef 100) #4
  br label %13

27:                                               ; preds = %13
  tail call void @perror(ptr noundef nonnull @.str.7) #3
  %28 = icmp sgt i32 %5, 0
  br i1 %28, label %29, label %31

29:                                               ; preds = %27
  %30 = tail call i32 @close(i32 noundef %5) #4
  br label %31

31:                                               ; preds = %.thread20, %29, %27
  %32 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.10)
  ret i32 0
}

; Function Attrs: nofree nounwind
declare void @perror(ptr nocapture noundef readonly) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nofree nounwind
declare noundef i32 @fflush(ptr nocapture noundef) local_unnamed_addr #1

declare i32 @sleep(i32 noundef) local_unnamed_addr #2

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

declare i64 @sendto(i32 noundef, ptr noundef, i64 noundef, i32 noundef, ptr noundef, i32 noundef) local_unnamed_addr #2

declare i32 @usleep(i32 noundef) local_unnamed_addr #2

declare i32 @close(i32 noundef) local_unnamed_addr #2

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


; Effective O3 passes: GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass