; 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.42 = external hidden unnamed_addr constant [65 x i8], align 1
@.str.43 = external hidden unnamed_addr constant [60 x i8], align 1
@.str.44 = external hidden unnamed_addr constant [60 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [62 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(i32 noundef %0, i8 noundef signext %1, double noundef %2) #4 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i8, align 1
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca [128 x i8], align 16
  %12 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store i8 %1, ptr %6, align 1, !tbaa !9
  store double %2, ptr %7, align 8, !tbaa !10
  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 4, ptr %10) #5
  call void @llvm.lifetime.start.p0(i64 128, ptr %11) #5
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = icmp slt i32 %13, 0
  br i1 %14, label %18, label %15

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

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

26:                                               ; preds = %18
  %27 = load ptr, ptr @stderr, align 8, !tbaa !12
  %28 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %27, ptr noundef @.str.42) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

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

41:                                               ; preds = %29
  %42 = load ptr, ptr @stderr, align 8, !tbaa !12
  %43 = load i32, ptr %5, align 4, !tbaa !5
  %44 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %42, ptr noundef @.str.43, i32 noundef %43) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

45:                                               ; preds = %29
  %46 = load double, ptr %7, align 8, !tbaa !10
  %47 = fcmp ogt double %46, 1.000000e+00
  br i1 %47, label %51, label %48

48:                                               ; preds = %45
  %49 = load double, ptr %7, align 8, !tbaa !10
  %50 = fcmp olt double %49, 0.000000e+00
  br label %51

51:                                               ; preds = %48, %45
  %52 = phi i1 [ true, %45 ], [ %50, %48 ]
  %53 = xor i1 %52, true
  %54 = xor i1 %53, true
  %55 = zext i1 %54 to i32
  %56 = sext i32 %55 to i64
  %57 = call i64 @llvm.expect.i64(i64 %56, i64 0)
  %58 = icmp ne i64 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %51
  %60 = load ptr, ptr @stderr, align 8, !tbaa !12
  %61 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %60, ptr noundef @.str.44) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

62:                                               ; preds = %51
  %63 = load double, ptr %7, align 8, !tbaa !10
  %64 = load i32, ptr %5, align 4, !tbaa !5
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds [3 x i32], ptr @period_ns, i64 0, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !5
  %68 = uitofp i32 %67 to double
  %69 = fmul double %63, %68
  %70 = fptosi double %69 to i32
  store i32 %70, ptr %10, align 4, !tbaa !5
  %71 = getelementptr inbounds [128 x i8], ptr %11, i64 0, i64 0
  %72 = load i32, ptr %10, align 4, !tbaa !5
  %73 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef %71, i64 noundef 128, ptr noundef @.str.27, i32 noundef %72) #5
  store i32 %73, ptr %8, align 4, !tbaa !5
  %74 = load i8, ptr %6, align 1, !tbaa !9
  %75 = sext i8 %74 to i32
  switch i32 %75, label %96 [
    i32 65, label %76
    i32 66, label %86
  ]

76:                                               ; preds = %62
  %77 = load i32, ptr %5, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [3 x i32], ptr @dutyA_fd, i64 0, i64 %78
  %80 = load i32, ptr %79, align 4, !tbaa !5
  %81 = getelementptr inbounds [128 x i8], ptr %11, i64 0, i64 0
  %82 = load i32, ptr %8, align 4, !tbaa !5
  %83 = sext i32 %82 to i64
  %84 = call i64 @write(i32 noundef %80, ptr noundef %81, i64 noundef %83)
  %85 = trunc i64 %84 to i32
  store i32 %85, ptr %9, align 4, !tbaa !5
  br label %99

86:                                               ; preds = %62
  %87 = load i32, ptr %5, align 4, !tbaa !5
  %88 = sext i32 %87 to i64
  %89 = getelementptr inbounds [3 x i32], ptr @dutyB_fd, i64 0, i64 %88
  %90 = load i32, ptr %89, align 4, !tbaa !5
  %91 = getelementptr inbounds [128 x i8], ptr %11, i64 0, i64 0
  %92 = load i32, ptr %8, align 4, !tbaa !5
  %93 = sext i32 %92 to i64
  %94 = call i64 @write(i32 noundef %90, ptr noundef %91, i64 noundef %93)
  %95 = trunc i64 %94 to i32
  store i32 %95, ptr %9, align 4, !tbaa !5
  br label %99

96:                                               ; preds = %62
  %97 = load ptr, ptr @stderr, align 8, !tbaa !12
  %98 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %97, ptr noundef @.str.45) #5
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

99:                                               ; preds = %86, %76
  %100 = load i32, ptr %9, align 4, !tbaa !5
  %101 = icmp eq i32 %100, -1
  %102 = xor i1 %101, true
  %103 = xor i1 %102, true
  %104 = zext i1 %103 to i32
  %105 = sext i32 %104 to i64
  %106 = call i64 @llvm.expect.i64(i64 %105, i64 0)
  %107 = icmp ne i64 %106, 0
  br i1 %107, label %108, label %109

108:                                              ; preds = %99
  call void @perror(ptr noundef @.str.46)
  store i32 -1, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

109:                                              ; preds = %99
  store i32 0, ptr %4, align 4
  store i32 1, ptr %12, align 4
  br label %110

110:                                              ; preds = %109, %108, %96, %59, %41, %26
  call void @llvm.lifetime.end.p0(i64 128, ptr %11) #5
  call void @llvm.lifetime.end.p0(i64 4, 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
  %111 = load i32, ptr %4, align 4
  ret i32 %111
}

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 = !{!"double", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"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.42 = external hidden unnamed_addr constant [65 x i8], align 1
@.str.43 = external hidden unnamed_addr constant [60 x i8], align 1
@.str.44 = external hidden unnamed_addr constant [60 x i8], align 1
@.str.45 = external hidden unnamed_addr constant [61 x i8], align 1
@.str.46 = external hidden unnamed_addr constant [62 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(i32 noundef %0, i8 noundef signext %1, double 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.42) #5
  br label %43

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.43, i32 noundef %0) #5
  br label %43

17:                                               ; preds = %9
  %18 = fcmp ogt double %2, 1.000000e+00
  %19 = fcmp olt double %2, 0.000000e+00
  %20 = or i1 %18, %19
  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.44) #5
  br label %43

24:                                               ; preds = %17
  %25 = getelementptr inbounds [3 x i32], ptr @period_ns, i64 0, i64 %10
  %26 = load i32, ptr %25, align 4, !tbaa !10
  %27 = uitofp i32 %26 to double
  %28 = fmul double %27, %2
  %29 = fptosi double %28 to i32
  %30 = call i32 (ptr, i64, ptr, ...) @snprintf(ptr noundef nonnull dereferenceable(1) %4, i64 noundef 128, ptr noundef nonnull @.str.27, i32 noundef %29) #4
  switch i8 %1, label %32 [
    i8 65, label %35
    i8 66, label %31
  ]

31:                                               ; preds = %24
  br label %35

32:                                               ; preds = %24
  %33 = load ptr, ptr @stderr, align 8, !tbaa !6
  %34 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %33, ptr noundef nonnull @.str.45) #5
  br label %43

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

42:                                               ; preds = %35
  tail call void @perror(ptr noundef nonnull @.str.46) #6
  br label %43

43:                                               ; preds = %35, %42, %32, %21, %14, %6
  %.0 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %21 ], [ -1, %32 ], [ -1, %42 ], [ 0, %35 ]
  call void @llvm.lifetime.end.p0(i64 128, ptr nonnull %4) #4
  ret i32 %.0
}

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,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass