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

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

@stderr = external global ptr, align 8
@dutyA_fd = external hidden global [3 x i32], align 4
@dutyB_fd = external hidden global [3 x i32], align 4
@period_ns = external hidden global [3 x i32], align 4
@.str.27 = external hidden unnamed_addr constant [3 x i8], align 1
@init_flag = external hidden global [3 x i32], align 4
@.str.45 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [62 x i8], align 1
@.str.47 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.48 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.49 = external hidden unnamed_addr constant [80 x i8], align 1

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

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

; Function Attrs: nounwind
declare i32 @snprintf(ptr noundef, i64 noundef, ptr noundef, ...) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(none)
declare i64 @llvm.expect.i64(i64, i64) #2

declare void @perror(ptr noundef) #3

declare i64 @write(i32 noundef, ptr noundef, i64 noundef) #3

; 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_pwm_set_duty_ns(i32 noundef %0, i8 noundef signext %1, i32 noundef %2) #4 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca [128 x i8], align 16
  %11 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.start.p0(i64 128, ptr %10) #5
  %12 = load i32, ptr %5, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 0
  br i1 %13, label %17, label %14

14:                                               ; preds = %3
  %15 = load i32, ptr %5, align 4, !tbaa !5
  %16 = icmp sgt i32 %15, 2
  br label %17

17:                                               ; preds = %14, %3
  %18 = phi i1 [ true, %3 ], [ %16, %14 ]
  %19 = xor i1 %18, true
  %20 = xor i1 %19, true
  %21 = zext i1 %20 to i32
  %22 = sext i32 %21 to i64
  %23 = call i64 @llvm.expect.i64(i64 %22, i64 0)
  %24 = icmp ne i64 %23, 0
  br i1 %24, label %25, label %28

25:                                               ; preds = %17
  %26 = load ptr, ptr @stderr, align 8, !tbaa !10
  %27 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %26, ptr noundef @.str.47) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %104

28:                                               ; preds = %17
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [3 x i32], ptr @init_flag, i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  %33 = icmp eq i32 %32, 0
  %34 = xor i1 %33, true
  %35 = xor i1 %34, true
  %36 = zext i1 %35 to i32
  %37 = sext i32 %36 to i64
  %38 = call i64 @llvm.expect.i64(i64 %37, i64 0)
  %39 = icmp ne i64 %38, 0
  br i1 %39, label %40, label %44

40:                                               ; preds = %28
  %41 = load ptr, ptr @stderr, align 8, !tbaa !10
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %41, ptr noundef @.str.48, i32 noundef %42) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %104

44:                                               ; preds = %28
  %45 = load i32, ptr %7, align 4, !tbaa !5
  %46 = load i32, ptr %5, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [3 x i32], ptr @period_ns, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = icmp ugt i32 %45, %49
  %51 = xor i1 %50, true
  %52 = xor i1 %51, true
  %53 = zext i1 %52 to i32
  %54 = sext i32 %53 to i64
  %55 = call i64 @llvm.expect.i64(i64 %54, i64 0)
  %56 = icmp ne i64 %55, 0
  br i1 %56, label %57, label %64

57:                                               ; preds = %44
  %58 = load ptr, ptr @stderr, align 8, !tbaa !10
  %59 = load i32, ptr %5, align 4, !tbaa !5
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds [3 x i32], ptr @period_ns, i64 0, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !5
  %63 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %58, ptr noundef @.str.49, i32 noundef %62) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %104

64:                                               ; preds = %44
  %65 = getelementptr inbounds [128 x i8], ptr %10, i64 0, i64 0
  %66 = load i32, ptr %7, align 4, !tbaa !5
  %67 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %65, i64 noundef 128, ptr noundef @.str.27, i32 noundef %66) #5
  store i32 %67, ptr %8, align 4, !tbaa !5
  %68 = load i8, ptr %6, align 1, !tbaa !9
  %69 = sext i8 %68 to i32
  switch i32 %69, label %90 [
    i32 65, label %70
    i32 66, label %80
  ]

70:                                               ; preds = %64
  %71 = load i32, ptr %5, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [3 x i32], ptr @dutyA_fd, i64 0, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !5
  %75 = getelementptr inbounds [128 x i8], ptr %10, i64 0, i64 0
  %76 = load i32, ptr %8, align 4, !tbaa !5
  %77 = sext i32 %76 to i64
  %78 = call i64 @write(i32 noundef %74, ptr noundef %75, i64 noundef %77)
  %79 = trunc i64 %78 to i32
  store i32 %79, ptr %9, align 4, !tbaa !5
  br label %93

80:                                               ; preds = %64
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = sext i32 %81 to i64
  %83 = getelementptr inbounds [3 x i32], ptr @dutyB_fd, i64 0, i64 %82
  %84 = load i32, ptr %83, align 4, !tbaa !5
  %85 = getelementptr inbounds [128 x i8], ptr %10, i64 0, i64 0
  %86 = load i32, ptr %8, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = call i64 @write(i32 noundef %84, ptr noundef %85, i64 noundef %87)
  %89 = trunc i64 %88 to i32
  store i32 %89, ptr %9, align 4, !tbaa !5
  br label %93

90:                                               ; preds = %64
  %91 = load ptr, ptr @stderr, align 8, !tbaa !10
  %92 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %91, ptr noundef @.str.45) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %104

93:                                               ; preds = %80, %70
  %94 = load i32, ptr %9, align 4, !tbaa !5
  %95 = icmp eq i32 %94, -1
  %96 = xor i1 %95, true
  %97 = xor i1 %96, true
  %98 = zext i1 %97 to i32
  %99 = sext i32 %98 to i64
  %100 = call i64 @llvm.expect.i64(i64 %99, i64 0)
  %101 = icmp ne i64 %100, 0
  br i1 %101, label %102, label %103

102:                                              ; preds = %93
  call void @perror(ptr noundef @.str.46)
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %104

103:                                              ; preds = %93
  store i32 0, ptr %4, align 4
  store i32 1, ptr %11, align 4
  br label %104

104:                                              ; preds = %103, %102, %90, %57, %40, %25
  call void @llvm.lifetime.end.p0(i64 128, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  %105 = load i32, ptr %4, align 4
  ret i32 %105
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { nocallback nofree nosync nounwind willreturn memory(none) }
attributes #3 = { "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 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 #5 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"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"

@stderr = external local_unnamed_addr global ptr, align 8
@dutyA_fd = external hidden local_unnamed_addr global [3 x i32], align 4
@dutyB_fd = external hidden local_unnamed_addr global [3 x i32], align 4
@period_ns = external hidden local_unnamed_addr global [3 x i32], align 4
@.str.27 = external hidden unnamed_addr constant [3 x i8], align 1
@init_flag = external hidden local_unnamed_addr global [3 x i32], align 4
@.str.45 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [62 x i8], align 1
@.str.47 = external hidden unnamed_addr constant [68 x i8], align 1
@.str.48 = external hidden unnamed_addr constant [63 x i8], align 1
@.str.49 = external hidden unnamed_addr constant [80 x i8], align 1

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

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

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

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

; Function Attrs: nofree
declare noundef i64 @write(i32 noundef, ptr nocapture noundef readonly, i64 noundef) local_unnamed_addr #2

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

; Function Attrs: nofree nounwind uwtable
define dso_local range(i32 -1, 1) i32 @rc_pwm_set_duty_ns(i32 noundef %0, i8 noundef signext %1, i32 noundef %2) local_unnamed_addr #3 {
  %4 = alloca [128 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 128, ptr nonnull %4) #4
  %5 = icmp ugt i32 %0, 2
  br i1 %5, label %6, label %9, !prof !5

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

9:                                                ; preds = %3
  %10 = zext nneg i32 %0 to i64
  %11 = getelementptr inbounds [3 x i32], ptr @init_flag, i64 0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !10
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %17, !prof !5

14:                                               ; preds = %9
  %15 = load ptr, ptr @stderr, align 8, !tbaa !6
  %16 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %15, ptr noundef nonnull @.str.48, i32 noundef %0) #5
  br label %38

17:                                               ; preds = %9
  %18 = getelementptr inbounds [3 x i32], ptr @period_ns, i64 0, i64 %10
  %19 = load i32, ptr %18, align 4, !tbaa !10
  %20 = icmp ult i32 %19, %2
  br i1 %20, label %21, label %24, !prof !5

21:                                               ; preds = %17
  %22 = load ptr, ptr @stderr, align 8, !tbaa !6
  %23 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %22, ptr noundef nonnull @.str.49, i32 noundef %19) #5
  br label %38

24:                                               ; preds = %17
  %25 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %4, i64 noundef 128, ptr noundef nonnull @.str.27, i32 noundef %2) #4
  switch i8 %1, label %27 [
    i8 65, label %30
    i8 66, label %26
  ]

26:                                               ; preds = %24
  br label %30

27:                                               ; preds = %24
  %28 = load ptr, ptr @stderr, align 8, !tbaa !6
  %29 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %28, ptr noundef nonnull @.str.45) #5
  br label %38

30:                                               ; preds = %24, %26
  %dutyB_fd.sink = phi ptr [ @dutyB_fd, %26 ], [ @dutyA_fd, %24 ]
  %31 = getelementptr inbounds [3 x i32], ptr %dutyB_fd.sink, i64 0, i64 %10
  %32 = load i32, ptr %31, align 4, !tbaa !10
  %33 = sext i32 %25 to i64
  %34 = call i64 @write(i32 noundef %32, ptr noundef nonnull %4, i64 noundef %33) #4
  %35 = and i64 %34, 4294967295
  %36 = icmp eq i64 %35, 4294967295
  br i1 %36, label %37, label %38, !prof !5

37:                                               ; preds = %30
  tail call void @perror(ptr noundef nonnull @.str.46) #6
  br label %38

38:                                               ; preds = %30, %37, %27, %21, %14, %6
  %.015 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %21 ], [ -1, %27 ], [ -1, %37 ], [ 0, %30 ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %4) #4
  ret i32 %.015
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nofree 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 #4 = { nounwind }
attributes #5 = { cold nounwind }
attributes #6 = { cold }

!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 = !{!"branch_weights", !"expected", i32 1, i32 2000}
!6 = !{!7, !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !8, i64 0}


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