#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint32_t;
struct cmd_info {
  int dummy;
};

/* Variables and functions */
scalar_t__ cpuid_inline(int, unsigned long, unsigned long, int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, int, int, int, int);
int /*<<< orphan*/ stderr;
int strtol(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
unsigned long strtoul(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int cpuid(int argc, const char *argv[], const struct cmd_info *info) {
  unsigned long function;
  unsigned long index;
  int cpu;
  uint32_t data[4];
  cpu = strtol(argv[1], NULL, 0);
  function = strtoul(argv[2], NULL, 0);
  index = 0;
  if (argc == 4) {
    index = strtoul(argv[3], NULL, 0);
  }
  if (cpuid_inline(cpu, function, index, data) < 0) {
    fprintf(stderr, "Could not obtain cpuid result.\n");
    return -1;
  }
  printf("0x%08x 0x%08x 0x%08x 0x%08x\n", data[0], data[1], data[2], data[3]);
  return 0;
}


x86 assembly (clang, -O0):
<cpuid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    11b9 <cpuid+0x69>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtoul@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <cpuid_inline@plt>
cmp    $0x0,%rax
jge    11f8 <cpuid+0xa8>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <cpuid+0xc8>
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpuid>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %edi,%r12d
mov    0x8(%rsi),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,%r14d
mov    0x10(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtoul@plt>
mov    %rax,%r15
cmp    $0x4,%r12d
jne    119a <cpuid+0x4a>
mov    0x18(%rbp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtoul@plt>
mov    %rax,%rbx
mov    %rsp,%rcx
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <cpuid_inline@plt>
test   %rax,%rax
js     11d0 <cpuid+0x80>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xe59(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
jmp    11ea <cpuid+0x9a>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmd_info = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Could not obtain cpuid result.\0A\00"
@.str.1 = [29 x i8] c"0x%08x 0x%08x 0x%08x 0x%08x\0A\00"
define i32 @cpuid(i32 %0, i8** %1, %struct.cmd_info* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca %struct.cmd_info*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca [4 x i32]
store i32 %0, i32* %5
store i8** %1, i8*** %6
store %struct.cmd_info* %2, %struct.cmd_info** %7
%12 = load i8**, i8*** %6
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @strtol(i8* %14, i32* null, i32 0)
store i32 %15, i32* %10
%16 = load i8**, i8*** %6
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i64 @strtoul(i8* %18, i32* null, i32 0)
store i64 %19, i64* %8
store i64 0, i64* %9
%20 = load i32, i32* %5
%21 = icmp eq i32 %20, 4
br i1 %21, label %22, label %27
22: ; preds = %3
%23 = load i8**, i8*** %6
%24 = getelementptr inbounds i8*, i8** %23, i64 3
%25 = load i8*, i8** %24
%26 = call i64 @strtoul(i8* %25, i32* null, i32 0)
store i64 %26, i64* %9
br label %27
27: ; preds = %22, %3
%28 = load i32, i32* %10
%29 = load i64, i64* %8
%30 = load i64, i64* %9
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 0
%32 = call i64 @cpuid_inline(i32 %28, i64 %29, i64 %30, i32* %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
37: ; preds = %27
%38 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 2
%43 = load i32, i32* %42
%44 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 3
%45 = load i32, i32* %44
%46 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %39, i32 %41, i32 %43, i32 %45)
store i32 0, i32* %4
br label %47
47: ; preds = %37, %34
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
%struct.cmd_info = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Could not obtain cpuid result.\0A\00"
@.str.1 = [29 x i8] c"0x%08x 0x%08x 0x%08x 0x%08x\0A\00"
define i32 @cpuid(i32 %0, i8** nocapture %1, %struct.cmd_info* nocapture readnone %2) {
%4 = alloca [4 x i32]
%5 = bitcast [4 x i32]* %4 to i8*
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @strtol(i8* nocapture %7, i32* null, i32 0)
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i64 @strtoul(i8* nocapture %10, i32* null, i32 0)
%12 = icmp eq i32 %0, 4
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = getelementptr inbounds i8*, i8** %1, i64 3
%15 = load i8*, i8** %14
%16 = tail call i64 @strtoul(i8* nocapture %15, i32* null, i32 0)
br label %17
17: ; preds = %13, %3
%18 = phi i64 [ %16, %13 ], [ 0, %3 ]
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 0
%20 = call i64 @cpuid_inline(i32 %8, i64 %11, i64 %18, i32* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %34
25: ; preds = %17
%26 = load i32, i32* %19
%27 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 2
%30 = load i32, i32* %29
%31 = getelementptr inbounds [4 x i32], [4 x i32]* %4, i64 0, i64 3
%32 = load i32, i32* %31
%33 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %26, i32 %28, i32 %30, i32 %32)
br label %34
34: ; preds = %25, %22
%35 = phi i32 [ -1, %22 ], [ 0, %25 ]
ret i32 %35
}