; LLVM IR from beagleboard/librobotcontrol :: library/src (motor)

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

@channels = external hidden global i32, align 4
@dirA_chip = external hidden global [4 x i32], align 16
@dirA_pin = external hidden global [4 x i32], align 16
@dirB_chip = external hidden global [4 x i32], align 16
@dirB_pin = external hidden global [4 x i32], align 16
@init_flag = external hidden global i32, align 4

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @rc_motor_free_spin(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 i32 @rc_motor_cleanup() #1 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #3
  %4 = load i32, ptr @init_flag, align 4, !tbaa !5
  %5 = icmp ne i32 %4, 0
  br i1 %5, label %7, label %6

6:                                                ; preds = %0
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %37

7:                                                ; preds = %0
  %8 = call i32 @rc_motor_free_spin(i32 noundef 0)
  %9 = call i32 @rc_pwm_cleanup(i32 noundef 0)
  %10 = call i32 @rc_pwm_cleanup(i32 noundef 1)
  %11 = call i32 @rc_pwm_cleanup(i32 noundef 2)
  call void @rc_gpio_cleanup(i32 noundef 0, i32 noundef 20)
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %33, %7
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = load i32, ptr @channels, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %36

16:                                               ; preds = %12
  %17 = load i32, ptr %2, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [4 x i32], ptr @dirA_chip, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = load i32, ptr %2, align 4, !tbaa !5
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds [4 x i32], ptr @dirA_pin, i64 0, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !5
  call void @rc_gpio_cleanup(i32 noundef %20, i32 noundef %24)
  %25 = load i32, ptr %2, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [4 x i32], ptr @dirB_chip, i64 0, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr %2, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [4 x i32], ptr @dirB_pin, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  call void @rc_gpio_cleanup(i32 noundef %28, i32 noundef %32)
  br label %33

33:                                               ; preds = %16
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = add nsw i32 %34, 1
  store i32 %35, ptr %2, align 4, !tbaa !5
  br label %12, !llvm.loop !9

36:                                               ; preds = %12
  store i32 0, ptr %1, align 4
  store i32 1, ptr %3, align 4
  br label %37

37:                                               ; preds = %36, %6
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #3
  %38 = load i32, ptr %1, align 4
  ret i32 %38
}

declare i32 @rc_pwm_cleanup(i32 noundef) #2

declare void @rc_gpio_cleanup(i32 noundef, i32 noundef) #2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { "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 = distinct !{!9, !10}
!10 = !{!"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"

@channels = external hidden local_unnamed_addr global i32, align 4
@dirA_chip = external hidden local_unnamed_addr global [4 x i32], align 16
@dirA_pin = external hidden local_unnamed_addr global [4 x i32], align 16
@dirB_chip = external hidden local_unnamed_addr global [4 x i32], align 16
@dirB_pin = external hidden local_unnamed_addr global [4 x i32], align 16
@init_flag = external hidden local_unnamed_addr global i32, align 4

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @rc_motor_cleanup() local_unnamed_addr #0 {
  %1 = load i32, ptr @init_flag, align 4, !tbaa !5
  %.not = icmp eq i32 %1, 0
  br i1 %.not, label %.loopexit, label %2

2:                                                ; preds = %0
  %3 = tail call i32 @rc_motor_free_spin(i32 noundef 0)
  %4 = tail call i32 @rc_pwm_cleanup(i32 noundef 0) #2
  %5 = tail call i32 @rc_pwm_cleanup(i32 noundef 1) #2
  %6 = tail call i32 @rc_pwm_cleanup(i32 noundef 2) #2
  tail call void @rc_gpio_cleanup(i32 noundef 0, i32 noundef 20) #2
  %7 = load i32, ptr @channels, align 4, !tbaa !5
  %8 = icmp sgt i32 %7, 0
  br i1 %8, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %2, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %2 ]
  %9 = getelementptr inbounds [4 x i32], ptr @dirA_chip, i64 0, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = getelementptr inbounds [4 x i32], ptr @dirA_pin, i64 0, i64 %indvars.iv
  %12 = load i32, ptr %11, align 4, !tbaa !5
  tail call void @rc_gpio_cleanup(i32 noundef %10, i32 noundef %12) #2
  %13 = getelementptr inbounds [4 x i32], ptr @dirB_chip, i64 0, i64 %indvars.iv
  %14 = load i32, ptr %13, align 4, !tbaa !5
  %15 = getelementptr inbounds [4 x i32], ptr @dirB_pin, i64 0, i64 %indvars.iv
  %16 = load i32, ptr %15, align 4, !tbaa !5
  tail call void @rc_gpio_cleanup(i32 noundef %14, i32 noundef %16) #2
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %17 = load i32, ptr @channels, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = icmp slt i64 %indvars.iv.next, %18
  br i1 %19, label %.lr.ph, label %.loopexit, !llvm.loop !9

.loopexit:                                        ; preds = %.lr.ph, %2, %0
  ret i32 0
}

declare i32 @rc_pwm_cleanup(i32 noundef) local_unnamed_addr #1

declare void @rc_gpio_cleanup(i32 noundef, i32 noundef) local_unnamed_addr #1

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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