void delay_us(int n) {
  int count_1us = clk_get_cpu_rate() / 1000000 / 3;
  int count_max;
  int tmp;
  if (10 >= n) {
    count_1us -= 35;
  } else if (100 >= n) {
    count_1us -= 6;
  } else {
    count_1us -= 1;
  }
  count_max = n * count_1us;
  for (tmp = count_max; tmp > 0; tmp--) {
    __asm__("nop");
  }
  return;
}


x86 assembly (clang, -O0):
<delay_us>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <clk_get_cpu_rate@plt>
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    $0xa,%eax
cmp    -0x4(%rbp),%eax
jl     1151 <delay_us+0x41>
mov    -0x8(%rbp),%eax
sub    $0x23,%eax
mov    %eax,-0x8(%rbp)
jmp    117b <delay_us+0x6b>
mov    $0x64,%eax
cmp    -0x4(%rbp),%eax
jl     116d <delay_us+0x5d>
mov    -0x8(%rbp),%eax
sub    $0x6,%eax
mov    %eax,-0x8(%rbp)
jmp    1176 <delay_us+0x66>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    117b <delay_us+0x6b>
mov    -0x4(%rbp),%eax
imul   -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    11a4 <delay_us+0x94>
nop
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    118b <delay_us+0x7b>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<delay_us>:
push   %rbx
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <clk_get_cpu_rate@plt>
cltq
imul   $0x165e9f81,%rax,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x32,%rcx
add    %eax,%ecx
xor    %eax,%eax
cmp    $0x65,%ebx
setae  %al
cmp    $0xb,%ebx
lea    -0x6(%rax,%rax,4),%edx
mov    $0xffffffdd,%eax
cmovge %edx,%eax
add    %ecx,%eax
imul   %ebx,%eax
test   %eax,%eax
jle    1169 <delay_us+0x59>
add    $0x1,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
nop
add    $0xffffffff,%eax
cmp    $0x1,%eax
ja     1160 <delay_us+0x50>
pop    %rbx
ret

LLVM IR (-O0):
define void @delay_us(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = call i32 (...) @clk_get_cpu_rate()
%7 = sdiv i32 %6, 1000000
%8 = sdiv i32 %7, 3
store i32 %8, i32* %3
%9 = load i32, i32* %2
%10 = icmp sge i32 10, %9
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %3
%13 = sub nsw i32 %12, 35
store i32 %13, i32* %3
br label %24
14: ; preds = %1
%15 = load i32, i32* %2
%16 = icmp sge i32 100, %15
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* %3
%19 = sub nsw i32 %18, 6
store i32 %19, i32* %3
br label %23
20: ; preds = %14
%21 = load i32, i32* %3
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %3
br label %23
23: ; preds = %20, %17
br label %24
24: ; preds = %23, %11
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = mul nsw i32 %25, %26
store i32 %27, i32* %4
%28 = load i32, i32* %4
store i32 %28, i32* %5
br label %29
29: ; preds = %33, %24
%30 = load i32, i32* %5
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
br label %33
33: ; preds = %32
%34 = load i32, i32* %5
%35 = add nsw i32 %34, -1
store i32 %35, i32* %5
br label %29
36: ; preds = %29
ret void
}

LLVM IR (-O2):
define void @delay_us(i32 %0) {
%2 = tail call i32 (...) @clk_get_cpu_rate()
%3 = sdiv i32 %2, 3000000
%4 = icmp slt i32 %0, 11
%5 = icmp ult i32 %0, 101
%6 = select i1 %5, i32 -6, i32 -1
%7 = select i1 %4, i32 -35, i32 %6
%8 = add nsw i32 %3, %7
%9 = mul nsw i32 %8, %0
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %1, %11
%12 = phi i32 [ %13, %11 ], [ %9, %1 ]
tail call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"()
%13 = add nsw i32 %12, -1
%14 = icmp ugt i32 %12, 1
br i1 %14, label %11, label %15
15: ; preds = %11, %1
ret void
}