#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main(int argc, char *argv[]) {
  int grayNum;
  do {
    if (argc == 2) {
      grayNum = atoi(argv[1]);
    } else {
      printf("Please enter a positive number to convert to grey code.\n");
      scanf("%d", &grayNum);
    }
  } while (grayNum < 1);
  printf("The gray number of %d is ", grayNum);
  grayNum = ((grayNum >> 1) ^ grayNum);
  int loop = 31;
  int oneEncountered = 0;
  while (loop >= 0) {
    if (grayNum & (1 << loop)) {
      oneEncountered = 1;
      printf("1");
    } else {
      if (oneEncountered) {
        printf("0");
      }
    }
    loop--;
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1165 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x14(%rbp)
jmp    1183 <main+0x53>
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xebf(%rip),%rdi
lea    -0x14(%rbp),%rsi
call   1040 <scanf@plt>
jmp    1188 <main+0x58>
cmpl   $0x1,-0x14(%rbp)
jl     1146 <main+0x16>
mov    -0x14(%rbp),%esi
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
sar    $0x1,%eax
xor    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x1f,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1226 <main+0xf6>
mov    -0x14(%rbp),%eax
mov    -0x18(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     11fb <main+0xcb>
movl   $0x1,-0x1c(%rbp)
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1218 <main+0xe8>
cmpl   $0x0,-0x1c(%rbp)
je     1213 <main+0xe3>
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1218 <main+0xe8>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    11bd <main+0x8d>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xeb5(%rip),%r14
lea    0xe91(%rip),%r15
lea    0xc(%rsp),%r12
jmp    119b <main+0x4b>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1060 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
jg     11b1 <main+0x61>
cmp    $0x2,%ebp
jne    1180 <main+0x30>
mov    0x8(%rbx),%rdi
call   1070 <atoi@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jle    119b <main+0x4b>
lea    0xe4b(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%ecx
mov    %ecx,%eax
sar    %eax
xor    %ecx,%eax
mov    %eax,0xc(%rsp)
mov    $0x1f,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    $0x31,%edi
bt     %ebp,%eax
jb     11f3 <main+0xa3>
mov    $0x30,%edi
test   %ebx,%ebx
je     1208 <main+0xb8>
call   1030 <putchar@plt>
mov    $0x1,%ebx
sub    $0x1,%ebp
jb     120f <main+0xbf>
mov    0xc(%rsp),%eax
jmp    11e0 <main+0x90>
xor    %ebx,%ebx
sub    $0x1,%ebp
jae    1202 <main+0xb2>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [57 x i8] c"Please enter a positive number to convert to grey code.\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [26 x i8] c"The gray number of %d is \00"
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [2 x i8] c"0\00"
@.str.5 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %9
9: ; preds = %21, %2
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %6
br label %20
17: ; preds = %9
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
br label %20
20: ; preds = %17, %12
br label %21
21: ; preds = %20
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 1
br i1 %23, label %9, label %24
24: ; preds = %21
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %6
%28 = ashr i32 %27, 1
%29 = load i32, i32* %6
%30 = xor i32 %28, %29
store i32 %30, i32* %6
store i32 31, i32* %7
store i32 0, i32* %8
br label %31
31: ; preds = %48, %24
%32 = load i32, i32* %7
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %51
34: ; preds = %31
%35 = load i32, i32* %6
%36 = load i32, i32* %7
%37 = shl i32 1, %36
%38 = and i32 %35, %37
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
store i32 1, i32* %8
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %48
42: ; preds = %34
%43 = load i32, i32* %8
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
br label %48
48: ; preds = %47, %40
%49 = load i32, i32* %7
%50 = add nsw i32 %49, -1
store i32 %50, i32* %7
br label %31
51: ; preds = %31
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [26 x i8] c"The gray number of %d is \00"
@str = [56 x i8] c"Please enter a positive number to convert to grey code.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %0, 2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
br label %7
7: ; preds = %15, %2
br i1 %5, label %8, label %11
8: ; preds = %7
%9 = load i8*, i8** %6
%10 = call i32 @atoi(i8* %9)
store i32 %10, i32* %3
br label %15
11: ; preds = %7
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @str, i64 0, i64 0))
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%14 = load i32, i32* %3
br label %15
15: ; preds = %8, %11
%16 = phi i32 [ %10, %8 ], [ %14, %11 ]
%17 = icmp slt i32 %16, 1
br i1 %17, label %7, label %18
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%20 = load i32, i32* %3
%21 = ashr i32 %20, 1
%22 = xor i32 %21, %20
store i32 %22, i32* %3
br label %23
23: ; preds = %38, %18
%24 = phi i32 [ %22, %18 ], [ %40, %38 ]
%25 = phi i32 [ 0, %18 ], [ %36, %38 ]
%26 = phi i32 [ 31, %18 ], [ %39, %38 ]
%27 = shl nuw i32 1, %26
%28 = and i32 %24, %27
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = icmp eq i32 %25, 0
br i1 %31, label %35, label %32
32: ; preds = %30, %23
%33 = phi i32 [ 49, %23 ], [ 48, %30 ]
%34 = call i32 @putchar(i32 %33)
br label %35
35: ; preds = %32, %30
%36 = phi i32 [ 0, %30 ], [ 1, %32 ]
%37 = icmp eq i32 %26, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = add nsw i32 %26, -1
%40 = load i32, i32* %3
br label %23
41: ; preds = %35
%42 = call i32 @putchar(i32 10)
ret i32 0
}