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

; 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.rc_spi_state_t = type { [12 x i8], [12 x i8], [12 x i8], [12 x i8], [12 x i32], [12 x i32] }

@stderr = external global ptr, align 8
@state = external hidden global [6 x %struct.rc_spi_state_t], align 16
@.str.29 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [57 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [51 x i8], align 1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

declare i32 @rc_gpio_set_value(i32 noundef, i32 noundef, i32 noundef) #1

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

10:                                               ; preds = %3
  %11 = load i32, ptr %5, align 4, !tbaa !5
  %12 = icmp sgt i32 %11, 5
  br i1 %12, label %13, label %16

13:                                               ; preds = %10, %3
  %14 = load ptr, ptr @stderr, align 8, !tbaa !9
  %15 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %14, ptr noundef @.str.29, i32 noundef 5) #3
  store i32 -1, ptr %4, align 4
  br label %82

16:                                               ; preds = %10
  %17 = load i32, ptr %6, align 4, !tbaa !5
  %18 = icmp slt i32 %17, 0
  br i1 %18, label %22, label %19

19:                                               ; preds = %16
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = icmp sge i32 %20, 12
  br i1 %21, label %22, label %25

22:                                               ; preds = %19, %16
  %23 = load ptr, ptr @stderr, align 8, !tbaa !9
  %24 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %23, ptr noundef @.str.30, i32 noundef 11) #3
  store i32 -1, ptr %4, align 4
  br label %82

25:                                               ; preds = %19
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %27
  %29 = getelementptr inbounds %struct.rc_spi_state_t, ptr %28, i32 0, i32 0
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [12 x i8], ptr %29, i64 0, i64 %31
  %33 = load i8, ptr %32, align 1, !tbaa !11
  %34 = zext i8 %33 to i32
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %36, label %39

36:                                               ; preds = %25
  %37 = load ptr, ptr @stderr, align 8, !tbaa !9
  %38 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %37, ptr noundef @.str.31) #3
  store i32 -1, ptr %4, align 4
  br label %82

39:                                               ; preds = %25
  %40 = load i32, ptr %5, align 4, !tbaa !5
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %41
  %43 = getelementptr inbounds %struct.rc_spi_state_t, ptr %42, i32 0, i32 1
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds [12 x i8], ptr %43, i64 0, i64 %45
  %47 = load i8, ptr %46, align 1, !tbaa !11
  %48 = zext i8 %47 to i32
  %49 = icmp ne i32 %48, 2
  br i1 %49, label %50, label %53

50:                                               ; preds = %39
  %51 = load ptr, ptr @stderr, align 8, !tbaa !9
  %52 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %51, ptr noundef @.str.32) #3
  store i32 -1, ptr %4, align 4
  br label %82

53:                                               ; preds = %39
  %54 = load i32, ptr %5, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %55
  %57 = getelementptr inbounds %struct.rc_spi_state_t, ptr %56, i32 0, i32 2
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [12 x i8], ptr %57, i64 0, i64 %59
  %61 = load i8, ptr %60, align 1, !tbaa !11
  %62 = zext i8 %61 to i32
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %64
  %66 = getelementptr inbounds %struct.rc_spi_state_t, ptr %65, i32 0, i32 3
  %67 = load i32, ptr %6, align 4, !tbaa !5
  %68 = sext i32 %67 to i64
  %69 = getelementptr inbounds [12 x i8], ptr %66, i64 0, i64 %68
  %70 = load i8, ptr %69, align 1, !tbaa !11
  %71 = zext i8 %70 to i32
  %72 = load i32, ptr %7, align 4, !tbaa !5
  %73 = icmp ne i32 %72, 0
  %74 = xor i1 %73, true
  %75 = zext i1 %74 to i32
  %76 = call i32 @rc_gpio_set_value(i32 noundef %62, i32 noundef %71, i32 noundef %75)
  %77 = icmp eq i32 %76, -1
  br i1 %77, label %78, label %81

78:                                               ; preds = %53
  %79 = load ptr, ptr @stderr, align 8, !tbaa !9
  %80 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %79, ptr noundef @.str.33) #3
  store i32 -1, ptr %4, align 4
  br label %82

81:                                               ; preds = %53
  store i32 0, ptr %4, align 4
  br label %82

82:                                               ; preds = %81, %78, %50, %36, %22, %13
  %83 = load i32, ptr %4, align 4
  ret i32 %83
}

attributes #0 = { 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 #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 = !{!"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}


; 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.rc_spi_state_t = type { [12 x i8], [12 x i8], [12 x i8], [12 x i8], [12 x i32], [12 x i32] }

@stderr = external local_unnamed_addr global ptr, align 8
@state = external hidden local_unnamed_addr global [6 x %struct.rc_spi_state_t], align 16
@.str.29 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.30 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.31 = external hidden unnamed_addr constant [57 x i8], align 1
@.str.32 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.33 = external hidden unnamed_addr constant [51 x i8], align 1

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

declare i32 @rc_gpio_set_value(i32 noundef, i32 noundef, i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local range(i32 -1, 1) i32 @rc_spi_manual_select(i32 noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #2 {
  %or.cond = icmp ugt i32 %0, 5
  br i1 %or.cond, label %4, label %7

4:                                                ; preds = %3
  %5 = load ptr, ptr @stderr, align 8, !tbaa !5
  %6 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %5, ptr noundef nonnull @.str.29, i32 noundef 5) #3
  br label %43

7:                                                ; preds = %3
  %or.cond3 = icmp ugt i32 %1, 11
  br i1 %or.cond3, label %8, label %11

8:                                                ; preds = %7
  %9 = load ptr, ptr @stderr, align 8, !tbaa !5
  %10 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %9, ptr noundef nonnull @.str.30, i32 noundef 11) #3
  br label %43

11:                                               ; preds = %7
  %12 = zext nneg i32 %0 to i64
  %13 = getelementptr inbounds [6 x %struct.rc_spi_state_t], ptr @state, i64 0, i64 %12
  %14 = zext nneg i32 %1 to i64
  %15 = getelementptr inbounds [12 x i8], ptr %13, i64 0, i64 %14
  %16 = load i8, ptr %15, align 1, !tbaa !9
  %17 = icmp eq i8 %16, 0
  br i1 %17, label %18, label %21

18:                                               ; preds = %11
  %19 = load ptr, ptr @stderr, align 8, !tbaa !5
  %20 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %19, ptr noundef nonnull @.str.31) #3
  br label %43

21:                                               ; preds = %11
  %22 = getelementptr inbounds i8, ptr %13, i64 12
  %23 = getelementptr inbounds [12 x i8], ptr %22, i64 0, i64 %14
  %24 = load i8, ptr %23, align 1, !tbaa !9
  %.not = icmp eq i8 %24, 2
  br i1 %.not, label %28, label %25

25:                                               ; preds = %21
  %26 = load ptr, ptr @stderr, align 8, !tbaa !5
  %27 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %26, ptr noundef nonnull @.str.32) #3
  br label %43

28:                                               ; preds = %21
  %29 = getelementptr inbounds i8, ptr %13, i64 24
  %30 = getelementptr inbounds [12 x i8], ptr %29, i64 0, i64 %14
  %31 = load i8, ptr %30, align 1, !tbaa !9
  %32 = zext i8 %31 to i32
  %33 = getelementptr inbounds i8, ptr %13, i64 36
  %34 = getelementptr inbounds [12 x i8], ptr %33, i64 0, i64 %14
  %35 = load i8, ptr %34, align 1, !tbaa !9
  %36 = zext i8 %35 to i32
  %.not19 = icmp eq i32 %2, 0
  %37 = zext i1 %.not19 to i32
  %38 = tail call i32 @rc_gpio_set_value(i32 noundef %32, i32 noundef %36, i32 noundef %37) #4
  %39 = icmp eq i32 %38, -1
  br i1 %39, label %40, label %43

40:                                               ; preds = %28
  %41 = load ptr, ptr @stderr, align 8, !tbaa !5
  %42 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %41, ptr noundef nonnull @.str.33) #3
  br label %43

43:                                               ; preds = %28, %40, %25, %18, %8, %4
  %.0 = phi i32 [ -1, %4 ], [ -1, %8 ], [ -1, %18 ], [ -1, %25 ], [ -1, %40 ], [ 0, %28 ]
  ret i32 %.0
}

attributes #0 = { 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 #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 = { cold nounwind }
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 = !{!7, !7, i64 0}


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