void microdelay(int us) {
  volatile int j = 0;
  while (us-- > 0)
    for (j = 0; j < 10000; j++)
      ;
}


x86 assembly (clang, -O0):
<microdelay>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x4(%rbp)
cmp    $0x0,%eax
jle    114f <microdelay+0x4f>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    $0x2710,%eax
jge    114a <microdelay+0x4a>
jmp    113c <microdelay+0x3c>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1129 <microdelay+0x29>
jmp    110e <microdelay+0xe>
pop    %rbp
ret

x86 assembly (clang, -O2):
<microdelay>:
movl   $0x0,-0x4(%rsp)
test   %edi,%edi
jle    1146 <microdelay+0x46>
mov    %edi,%eax
test   $0x1,%dil
je     1141 <microdelay+0x41>
lea    -0x1(%rdi),%eax
movl   $0x0,-0x4(%rsp)
mov    -0x4(%rsp),%ecx
cmp    $0x270f,%ecx
jg     1141 <microdelay+0x41>
nopl   0x0(%rax,%rax,1)
addl   $0x1,-0x4(%rsp)
mov    -0x4(%rsp),%ecx
cmp    $0x2710,%ecx
jl     1130 <microdelay+0x30>
cmp    $0x1,%edi
jne    115a <microdelay+0x5a>
ret
nopw   0x0(%rax,%rax,1)
lea    -0x2(%rax),%ecx
cmp    $0x3,%eax
mov    %ecx,%eax
jl     1146 <microdelay+0x46>
movl   $0x0,-0x4(%rsp)
mov    -0x4(%rsp),%ecx
cmp    $0x270f,%ecx
jg     1181 <microdelay+0x81>
xchg   %ax,%ax
addl   $0x1,-0x4(%rsp)
mov    -0x4(%rsp),%ecx
cmp    $0x2710,%ecx
jl     1170 <microdelay+0x70>
movl   $0x0,-0x4(%rsp)
mov    -0x4(%rsp),%ecx
cmp    $0x270f,%ecx
jg     1150 <microdelay+0x50>
cs nopw 0x0(%rax,%rax,1)
nop
addl   $0x1,-0x4(%rsp)
mov    -0x4(%rsp),%ecx
cmp    $0x2710,%ecx
jl     11a0 <microdelay+0xa0>
jmp    1150 <microdelay+0x50>

LLVM IR (-O0):
define void @microdelay(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store volatile i32 0, i32* %3
br label %4
4: ; preds = %16, %1
%5 = load i32, i32* %2
%6 = add nsw i32 %5, -1
store i32 %6, i32* %2
%7 = icmp sgt i32 %5, 0
br i1 %7, label %8, label %17
8: ; preds = %4
store volatile i32 0, i32* %3
br label %9
9: ; preds = %13, %8
%10 = load volatile i32, i32* %3
%11 = icmp slt i32 %10, 10000
br i1 %11, label %12, label %16
12: ; preds = %9
br label %13
13: ; preds = %12
%14 = load volatile i32, i32* %3
%15 = add nsw i32 %14, 1
store volatile i32 %15, i32* %3
br label %9
16: ; preds = %9
br label %4
17: ; preds = %4
ret void
}

LLVM IR (-O2):
define void @microdelay(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store volatile i32 0, i32* %2
%4 = icmp sgt i32 %0, 0
br i1 %4, label %5, label %40
5: ; preds = %1
%6 = and i32 %0, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = add nsw i32 %0, -1
store volatile i32 0, i32* %2
%10 = load volatile i32, i32* %2
%11 = icmp slt i32 %10, 10000
br i1 %11, label %12, label %17
12: ; preds = %8, %12
%13 = load volatile i32, i32* %2
%14 = add nsw i32 %13, 1
store volatile i32 %14, i32* %2
%15 = load volatile i32, i32* %2
%16 = icmp slt i32 %15, 10000
br i1 %16, label %12, label %17
17: ; preds = %8, %12, %5
%18 = phi i32 [ %0, %5 ], [ %9, %12 ], [ %9, %8 ]
%19 = icmp eq i32 %0, 1
br i1 %19, label %40, label %31
20: ; preds = %35, %31
%21 = add nsw i32 %32, -2
store volatile i32 0, i32* %2
%22 = load volatile i32, i32* %2
%23 = icmp slt i32 %22, 10000
br i1 %23, label %24, label %29
24: ; preds = %20, %24
%25 = load volatile i32, i32* %2
%26 = add nsw i32 %25, 1
store volatile i32 %26, i32* %2
%27 = load volatile i32, i32* %2
%28 = icmp slt i32 %27, 10000
br i1 %28, label %24, label %29
29: ; preds = %24, %20
%30 = icmp sgt i32 %32, 2
br i1 %30, label %31, label %40
31: ; preds = %17, %29
%32 = phi i32 [ %21, %29 ], [ %18, %17 ]
store volatile i32 0, i32* %2
%33 = load volatile i32, i32* %2
%34 = icmp slt i32 %33, 10000
br i1 %34, label %35, label %20
35: ; preds = %31, %35
%36 = load volatile i32, i32* %2
%37 = add nsw i32 %36, 1
store volatile i32 %37, i32* %2
%38 = load volatile i32, i32* %2
%39 = icmp slt i32 %38, 10000
br i1 %39, label %35, label %20
40: ; preds = %17, %29, %1
ret void
}