; LLVM IR from sca-secure-library-sca25519/sca25519 :: libopencm3/lib/pac55xx (gpio)

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

; 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 hidden i32 @get_ccs_port_base(i32 noundef) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.cttz.i32(i32, i1 immarg) #2

; 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 void @gpio_set_output_options(i32 noundef %0, i32 noundef %1, i16 noundef zeroext %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i16, align 2
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store i16 %2, ptr %6, align 2, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = call i32 @get_ccs_port_base(i32 noundef %11)
  store i32 %12, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %13 = load i32, ptr %7, align 4, !tbaa !5
  %14 = add i32 %13, 84
  %15 = zext i32 %14 to i64
  %16 = inttoptr i64 %15 to ptr
  %17 = load volatile i32, ptr %16, align 4, !tbaa !5
  store i32 %17, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %18 = load i16, ptr %6, align 2, !tbaa !9
  %19 = zext i16 %18 to i32
  %20 = call i32 @llvm.cttz.i32(i32 %19, i1 true)
  %21 = add i32 %20, 1
  %22 = icmp eq i32 %19, 0
  %23 = select i1 %22, i32 0, i32 %21
  store i32 %23, ptr %9, align 4, !tbaa !5
  br label %24

24:                                               ; preds = %27, %3
  %25 = load i32, ptr %9, align 4, !tbaa !5
  %26 = icmp ne i32 %25, 0
  br i1 %26, label %27, label %55

27:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = sub nsw i32 %28, 1
  store i32 %29, ptr %10, align 4, !tbaa !5
  %30 = load i32, ptr %10, align 4, !tbaa !5
  %31 = mul nsw i32 %30, 4
  %32 = shl i32 7, %31
  %33 = xor i32 %32, -1
  %34 = load i32, ptr %8, align 4, !tbaa !5
  %35 = and i32 %34, %33
  store i32 %35, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %5, align 4, !tbaa !5
  %37 = and i32 %36, 7
  %38 = load i32, ptr %10, align 4, !tbaa !5
  %39 = mul nsw i32 %38, 4
  %40 = shl i32 %37, %39
  %41 = load i32, ptr %8, align 4, !tbaa !5
  %42 = or i32 %41, %40
  store i32 %42, ptr %8, align 4, !tbaa !5
  %43 = load i32, ptr %10, align 4, !tbaa !5
  %44 = shl i32 1, %43
  %45 = load i16, ptr %6, align 2, !tbaa !9
  %46 = zext i16 %45 to i32
  %47 = xor i32 %46, %44
  %48 = trunc i32 %47 to i16
  store i16 %48, ptr %6, align 2, !tbaa !9
  %49 = load i16, ptr %6, align 2, !tbaa !9
  %50 = zext i16 %49 to i32
  %51 = call i32 @llvm.cttz.i32(i32 %50, i1 true)
  %52 = add i32 %51, 1
  %53 = icmp eq i32 %50, 0
  %54 = select i1 %53, i32 0, i32 %52
  store i32 %54, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %24, !llvm.loop !11

55:                                               ; preds = %24
  %56 = load i32, ptr %8, align 4, !tbaa !5
  %57 = load i32, ptr %7, align 4, !tbaa !5
  %58 = add i32 %57, 84
  %59 = zext i32 %58 to i64
  %60 = inttoptr i64 %59 to ptr
  store volatile i32 %56, ptr %60, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"short", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

; Function Attrs: nounwind uwtable
declare hidden i32 @get_ccs_port_base(i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @gpio_set_output_options(i32 noundef %0, i32 noundef %1, i16 noundef zeroext %2) local_unnamed_addr #0 {
  %4 = tail call i32 @get_ccs_port_base(i32 noundef %0)
  %5 = add i32 %4, 84
  %6 = zext i32 %5 to i64
  %7 = inttoptr i64 %6 to ptr
  %8 = load volatile i32, ptr %7, align 4, !tbaa !5
  %9 = icmp eq i16 %2, 0
  br i1 %9, label %select.unfold._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3
  %10 = tail call range(i16 0, 17) i16 @llvm.cttz.i16(i16 %2, i1 true)
  %11 = and i32 %1, 7
  br label %select.unfold

select.unfold:                                    ; preds = %.lr.ph, %select.unfold
  %.025 = phi i16 [ %2, %.lr.ph ], [ %20, %select.unfold ]
  %.015.in24.in = phi i16 [ %10, %.lr.ph ], [ %21, %select.unfold ]
  %.01623 = phi i32 [ %8, %.lr.ph ], [ %18, %select.unfold ]
  %12 = shl nuw nsw i16 %.015.in24.in, 2
  %13 = zext nneg i16 %12 to i32
  %14 = shl i32 7, %13
  %15 = xor i32 %14, -1
  %16 = and i32 %.01623, %15
  %17 = shl i32 %11, %13
  %18 = or i32 %16, %17
  %19 = shl nuw i16 1, %.015.in24.in
  %20 = xor i16 %.025, %19
  %21 = tail call range(i16 0, 17) i16 @llvm.cttz.i16(i16 %20, i1 true)
  %22 = icmp eq i16 %.025, %19
  br i1 %22, label %select.unfold._crit_edge, label %select.unfold

select.unfold._crit_edge:                         ; preds = %select.unfold, %3
  %.016.lcssa = phi i32 [ %8, %3 ], [ %18, %select.unfold ]
  store volatile i32 %.016.lcssa, ptr %7, align 4, !tbaa !5
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i16 @llvm.cttz.i16(i16, i1 immarg) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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"}


; Effective O3 passes: ADCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass