int CPLHaveRuntimeAVX() {
  int cpuinfo[4] = {0, 0, 0, 0};
  unsigned int nXCRLow;
  unsigned int nXCRHigh;
  __cpuid_count(1, 0, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]);
  if ((cpuinfo[2] & (1 << 27)) == 0) {
    return 0;
  }
  if ((cpuinfo[2] & (1 << 28)) == 0) {
    return 0;
  }
  __asm__("xgetbv" : "=a"(nXCRLow), "=d"(nXCRHigh) : "c"(0));
  if ((nXCRLow & ((1 << 1) | (2 << 1))) != ((1 << 1) | (2 << 1))) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<CPLHaveRuntimeAVX>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x14(%rbp),%r9d
mov    $0x1,%edi
xor    %esi,%esi
mov    $0x0,%al
call   1040 <__cpuid_count@plt>
mov    -0x18(%rbp),%eax
and    $0x8000000,%eax
cmp    $0x0,%eax
jne    1171 <CPLHaveRuntimeAVX+0x51>
movl   $0x0,-0x4(%rbp)
jmp    11bb <CPLHaveRuntimeAVX+0x9b>
mov    -0x18(%rbp),%eax
and    $0x10000000,%eax
cmp    $0x0,%eax
jne    118e <CPLHaveRuntimeAVX+0x6e>
movl   $0x0,-0x4(%rbp)
jmp    11bb <CPLHaveRuntimeAVX+0x9b>
xor    %ecx,%ecx
xgetbv
mov    %eax,-0x24(%rbp)
mov    %edx,-0x28(%rbp)
mov    -0x24(%rbp),%eax
and    $0x6,%eax
cmp    $0x6,%eax
je     11b4 <CPLHaveRuntimeAVX+0x94>
movl   $0x0,-0x4(%rbp)
jmp    11bb <CPLHaveRuntimeAVX+0x9b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CPLHaveRuntimeAVX>:
push   %rax
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
xor    %eax,%eax
call   1030 <__cpuid_count@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @CPLHaveRuntimeAVX() {
%1 = alloca i32
%2 = alloca [4 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = bitcast [4 x i32]* %2 to i8*
%6 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%7 = load i32, i32* %6
%8 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 1
%9 = load i32, i32* %8
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%11 = load i32, i32* %10
%12 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 3
%13 = load i32, i32* %12
%14 = call i32 (i32, i32, i32, i32, i32, i32, ...) bitcast (i32 (...)* @__cpuid_count to i32 (i32, i32, i32, i32, i32, i32, ...)*)(i32 1, i32 0, i32 %7, i32 %9, i32 %11, i32 %13)
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%16 = load i32, i32* %15
%17 = and i32 %16, 134217728
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %0
store i32 0, i32* %1
br label %35
20: ; preds = %0
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 2
%22 = load i32, i32* %21
%23 = and i32 %22, 268435456
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 0, i32* %1
br label %35
26: ; preds = %20
%27 = call { i32, i32 } asm "xgetbv", "={ax},={dx},{cx},~{dirflag},~{fpsr},~{flags}"(i32 0)
%28 = extractvalue { i32, i32 } %27, 0
%29 = extractvalue { i32, i32 } %27, 1
store i32 %28, i32* %3
store i32 %29, i32* %4
%30 = load i32, i32* %3
%31 = and i32 %30, 6
%32 = icmp ne i32 %31, 6
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 0, i32* %1
br label %35
34: ; preds = %26
store i32 1, i32* %1
br label %35
35: ; preds = %34, %33, %25, %19
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
define i32 @CPLHaveRuntimeAVX() {
%1 = tail call i32 (i32, i32, i32, i32, i32, i32, ...) bitcast (i32 (...)* @__cpuid_count to i32 (i32, i32, i32, i32, i32, i32, ...)*)(i32 1, i32 0, i32 0, i32 0, i32 0, i32 0)
ret i32 0
}