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

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ ERANGE;
int EXIT_FAILURE;
int EXIT_SUCCESS;
long INT32_MAX;
long INT32_MIN;
int /*<<< orphan*/ cpuid(int *, int *, int *, int *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ stderr;
long strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int usage(char *);
int main(int argc, char **argv) {
  long leaf;
  uint32_t eax, ebx, ecx, edx;
  if (argc != 2) {
    return usage(argv[0]);
  }
  leaf = strtol(argv[1], NULL, 0);
  printf("%ld\n", leaf);
  if (errno == EINVAL) {
    fprintf(stderr, "Invalid leaf: %s\n", argv[1]);
    return EXIT_FAILURE;
  } else if (errno == ERANGE || leaf > INT32_MAX || leaf < INT32_MIN) {
    fprintf(stderr, "Leaf can't fit in 32 bit integer: %s\n", argv[1]);
    return EXIT_FAILURE;
  }
  eax = (uint32_t)leaf;
  cpuid(&eax, &ebx, &ecx, &edx);
  printf("EAX: %08x\nEBX: %08x\nECX: %08x\nEDX: %08x\n", eax, ebx, ecx, edx);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1184 <main+0x34>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1060 <usage@plt>
mov    %eax,-0x4(%rbp)
jmp    12af <main+0x15f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
jne    11f7 <main+0xa7>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe24(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12af <main+0x15f>
mov    0x2dd2(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
je     1239 <main+0xe9>
mov    -0x18(%rbp),%rax
mov    0x2dac(%rip),%rcx
cmp    (%rcx),%rax
jg     1239 <main+0xe9>
mov    -0x18(%rbp),%rax
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%rax
jge    1267 <main+0x117>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdc6(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12af <main+0x15f>
jmp    126c <main+0x11c>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
lea    -0x1c(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1070 <cpuid@plt>
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x28(%rbp),%r8d
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11af <main+0x5f>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %rax,%r14
lea    0xe8a(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e1f(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <main+0x6e>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe5b(%rip),%rsi
jmp    123c <main+0xec>
mov    (%rbx),%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r14
jmp    1060 <usage@plt>
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
je     1228 <main+0xd8>
mov    0x2df7(%rip),%rax
cmp    (%rax),%r14
jg     1228 <main+0xd8>
mov    0x2ddb(%rip),%rax
cmp    (%rax),%r14
jl     1228 <main+0xd8>
mov    %r14d,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
call   1070 <cpuid@plt>
mov    0x8(%rsp),%esi
mov    0x14(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xe25(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d82(%rip),%rax
jmp    1248 <main+0xf8>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xddb(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%ld\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"Invalid leaf: %s\0A\00"
@EXIT_FAILURE = global i32 0
@ERANGE = global i64 0
@INT32_MAX = global i64 0
@INT32_MIN = global i64 0
@.str.2 = [38 x i8] c"Leaf can't fit in 32 bit integer: %s\0A\00"
@.str.3 = [41 x i8] c"EAX: %08x\0AEBX: %08x\0AECX: %08x\0AEDX: %08x\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @usage(i8* %16)
store i32 %17, i32* %3
br label %66
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @strtol(i8* %21, i32* null, i32 0)
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = trunc i64 %23 to i32
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINVAL
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %36
29: ; preds = %18
%30 = load i32, i32* @stderr
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
store i32 %35, i32* %3
br label %66
36: ; preds = %18
%37 = load i64, i64* @errno
%38 = load i64, i64* @ERANGE
%39 = icmp eq i64 %37, %38
br i1 %39, label %48, label %40
40: ; preds = %36
%41 = load i64, i64* %6
%42 = load i64, i64* @INT32_MAX
%43 = icmp sgt i64 %41, %42
br i1 %43, label %48, label %44
44: ; preds = %40
%45 = load i64, i64* %6
%46 = load i64, i64* @INT32_MIN
%47 = icmp slt i64 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %44, %40, %36
%49 = load i32, i32* @stderr
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
store i32 %54, i32* %3
br label %66
55: ; preds = %44
br label %56
56: ; preds = %55
%57 = load i64, i64* %6
%58 = trunc i64 %57 to i32
store i32 %58, i32* %7
%59 = call i32 @cpuid(i32* %7, i32* %8, i32* %9, i32* %10)
%60 = load i32, i32* %7
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = load i32, i32* %10
%64 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %60, i32 %61, i32 %62, i32 %63)
%65 = load i32, i32* @EXIT_SUCCESS
store i32 %65, i32* %3
br label %66
66: ; preds = %56, %48, %29, %13
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [5 x i8] c"%ld\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"Invalid leaf: %s\0A\00"
@EXIT_FAILURE = global i32 0
@ERANGE = global i64 0
@INT32_MAX = global i64 0
@INT32_MIN = global i64 0
@.str.2 = [38 x i8] c"Leaf can't fit in 32 bit integer: %s\0A\00"
@.str.3 = [41 x i8] c"EAX: %08x\0AEBX: %08x\0AECX: %08x\0AEDX: %08x\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = icmp eq i32 %0, 2
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 @usage(i8* %13)
br label %51
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i64 @strtol(i8* nocapture %17, i32* null, i32 0)
%19 = trunc i64 %18 to i32
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINVAL
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %16
%27 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
br label %51
29: ; preds = %15
%30 = load i64, i64* @ERANGE
%31 = icmp eq i64 %21, %30
%32 = load i64, i64* @INT32_MAX
%33 = icmp sgt i64 %18, %32
%34 = select i1 %31, i1 true, i1 %33
%35 = load i64, i64* @INT32_MIN
%36 = icmp slt i64 %18, %35
%37 = select i1 %34, i1 true, i1 %36
br i1 %37, label %38, label %43
38: ; preds = %29
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %16
%41 = tail call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = load i32, i32* @EXIT_FAILURE
br label %51
43: ; preds = %29
store i32 %19, i32* %3
%44 = call i32 @cpuid(i32* %3, i32* %4, i32* %5, i32* %6)
%45 = load i32, i32* %3
%46 = load i32, i32* %4
%47 = load i32, i32* %5
%48 = load i32, i32* %6
%49 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 %46, i32 %47, i32 %48)
%50 = load i32, i32* @EXIT_SUCCESS
br label %51
51: ; preds = %43, %38, %24, %12
%52 = phi i32 [ %14, %12 ], [ %28, %24 ], [ %42, %38 ], [ %50, %43 ]
ret i32 %52
}