; LLVM IR from zeroth-robotics/zeroth-bot :: kos-zbot/firmware/feetech (feetech)

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

@.str = external hidden unnamed_addr constant [20 x i8], align 1
@mailbox_fd = external hidden global i32, align 4
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1
@mem_fd = external hidden global i32, align 4
@shared_mem = external hidden global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@ion_fd = external hidden global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local i32 @servo_init() #0 {
  %1 = alloca i32, align 4
  %2 = call i32 (ptr, i32, ...) @open(ptr noundef @.str, i32 noundef 2)
  store i32 %2, ptr @mailbox_fd, align 4, !tbaa !5
  %3 = load i32, ptr @mailbox_fd, align 4, !tbaa !5
  %4 = icmp sle i32 %3, 0
  br i1 %4, label %5, label %6

5:                                                ; preds = %0
  store i32 -1, ptr %1, align 4
  br label %35

6:                                                ; preds = %0
  %7 = call i32 (ptr, i32, ...) @open(ptr noundef @.str.1, i32 noundef 1052674)
  store i32 %7, ptr @mem_fd, align 4, !tbaa !5
  %8 = load i32, ptr @mem_fd, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %13

10:                                               ; preds = %6
  %11 = load i32, ptr @mailbox_fd, align 4, !tbaa !5
  %12 = call i32 @close(i32 noundef %11)
  store i32 -1, ptr %1, align 4
  br label %35

13:                                               ; preds = %6
  %14 = load i32, ptr @mem_fd, align 4, !tbaa !5
  %15 = call ptr @mmap(ptr noundef null, i64 noundef 256, i32 noundef 3, i32 noundef 1, i32 noundef %14, i64 noundef 2681208832) #3
  store ptr %15, ptr @shared_mem, align 8, !tbaa !9
  %16 = load ptr, ptr @shared_mem, align 8, !tbaa !9
  %17 = icmp eq ptr %16, inttoptr (i64 -1 to ptr)
  br i1 %17, label %18, label %23

18:                                               ; preds = %13
  %19 = load i32, ptr @mem_fd, align 4, !tbaa !5
  %20 = call i32 @close(i32 noundef %19)
  %21 = load i32, ptr @mailbox_fd, align 4, !tbaa !5
  %22 = call i32 @close(i32 noundef %21)
  store i32 -1, ptr %1, align 4
  br label %35

23:                                               ; preds = %13
  %24 = call i32 (ptr, i32, ...) @open(ptr noundef @.str.2, i32 noundef 2)
  store i32 %24, ptr @ion_fd, align 4, !tbaa !5
  %25 = load i32, ptr @ion_fd, align 4, !tbaa !5
  %26 = icmp slt i32 %25, 0
  br i1 %26, label %27, label %34

27:                                               ; preds = %23
  %28 = load ptr, ptr @shared_mem, align 8, !tbaa !9
  %29 = call i32 @munmap(ptr noundef %28, i64 noundef 256) #3
  %30 = load i32, ptr @mem_fd, align 4, !tbaa !5
  %31 = call i32 @close(i32 noundef %30)
  %32 = load i32, ptr @mailbox_fd, align 4, !tbaa !5
  %33 = call i32 @close(i32 noundef %32)
  store i32 -1, ptr %1, align 4
  br label %35

34:                                               ; preds = %23
  store i32 0, ptr %1, align 4
  br label %35

35:                                               ; preds = %34, %27, %18, %10, %5
  %36 = load i32, ptr %1, align 4
  ret i32 %36
}

declare i32 @open(ptr noundef, i32 noundef, ...) #1

declare i32 @close(i32 noundef) #1

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) #2

; Function Attrs: nounwind
declare i32 @munmap(ptr noundef, i64 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 = { "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 "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}


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

@.str = external hidden unnamed_addr constant [20 x i8], align 1
@mailbox_fd = external hidden local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [9 x i8], align 1
@mem_fd = external hidden local_unnamed_addr global i32, align 4
@shared_mem = external hidden local_unnamed_addr global ptr, align 8
@.str.2 = external hidden unnamed_addr constant [9 x i8], align 1
@ion_fd = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @servo_init() local_unnamed_addr #0 {
  %1 = tail call i32 (ptr, i32, ...) @open(ptr noundef nonnull @.str, i32 noundef 2) #4
  store i32 %1, ptr @mailbox_fd, align 4, !tbaa !5
  %2 = icmp slt i32 %1, 1
  br i1 %2, label %19, label %3

3:                                                ; preds = %0
  %4 = tail call i32 (ptr, i32, ...) @open(ptr noundef nonnull @.str.1, i32 noundef 1052674) #4
  store i32 %4, ptr @mem_fd, align 4, !tbaa !5
  %5 = icmp slt i32 %4, 0
  br i1 %5, label %.sink.split, label %6

6:                                                ; preds = %3
  %7 = tail call ptr @mmap(ptr noundef null, i64 noundef 256, i32 noundef 3, i32 noundef 1, i32 noundef %4, i64 noundef 2681208832) #4
  store ptr %7, ptr @shared_mem, align 8, !tbaa !9
  %8 = icmp eq ptr %7, inttoptr (i64 -1 to ptr)
  br i1 %8, label %.sink.split.sink.split, label %9

9:                                                ; preds = %6
  %10 = tail call i32 (ptr, i32, ...) @open(ptr noundef nonnull @.str.2, i32 noundef 2) #4
  store i32 %10, ptr @ion_fd, align 4, !tbaa !5
  %11 = icmp slt i32 %10, 0
  br i1 %11, label %12, label %19

12:                                               ; preds = %9
  %13 = load ptr, ptr @shared_mem, align 8, !tbaa !9
  %14 = tail call i32 @munmap(ptr noundef %13, i64 noundef 256) #4
  br label %.sink.split.sink.split

.sink.split.sink.split:                           ; preds = %6, %12
  %15 = load i32, ptr @mem_fd, align 4, !tbaa !5
  %16 = tail call i32 @close(i32 noundef %15) #4
  br label %.sink.split

.sink.split:                                      ; preds = %.sink.split.sink.split, %3
  %17 = load i32, ptr @mailbox_fd, align 4, !tbaa !5
  %18 = tail call i32 @close(i32 noundef %17) #4
  br label %19

19:                                               ; preds = %.sink.split, %9, %0
  %.0 = phi i32 [ -1, %0 ], [ 0, %9 ], [ -1, %.sink.split ]
  ret i32 %.0
}

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

declare i32 @close(i32 noundef) local_unnamed_addr #2

; Function Attrs: nounwind
declare ptr @mmap(ptr noundef, i64 noundef, i32 noundef, i32 noundef, i32 noundef, i64 noundef) local_unnamed_addr #3

; Function Attrs: nounwind
declare i32 @munmap(ptr noundef, i64 noundef) local_unnamed_addr #3

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


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