#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 unsigned int uint8_t;
typedef int /*<<< orphan*/ uint64_t;

/* Variables and functions */
unsigned int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ readVal(unsigned int, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writeValReverse(int /*<<< orphan*/, unsigned int);
int main(int argc, char **argv) {
  uint8_t width = 8u;
  if (argc == 2) {
    width = atoi(argv[1]);
  }
  if (width == 0u || width > 64u || argc > 2) {
    fprintf(stderr, "Usage: flipbits [width]\n");
    return 1;
  }
  bool endOfInput;
  uint64_t value = readVal(width, &endOfInput);
  while (!endOfInput) {
    writeValReverse(value, width);
    value = readVal(width, &endOfInput);
  }
  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)
movl   $0x8,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    1177 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1195 <main+0x55>
cmpl   $0x40,-0x14(%rbp)
ja     1195 <main+0x55>
cmpl   $0x2,-0x8(%rbp)
jle    11b6 <main+0x76>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1201 <main+0xc1>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <readVal@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11db <main+0x9b>
jmp    11fa <main+0xba>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <writeValReverse@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <readVal@plt>
mov    %eax,-0x1c(%rbp)
jmp    11c5 <main+0x85>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    $0x8,%ebx
cmp    $0x2,%edi
jne    115f <main+0x1f>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
cmp    $0x2,%ebp
jg     11ae <main+0x6e>
lea    -0x41(%rbx),%eax
cmp    $0xffffffbf,%eax
jbe    11ae <main+0x6e>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <readVal@plt>
cmpl   $0x0,0xc(%rsp)
jne    11aa <main+0x6a>
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edi
mov    %ebx,%esi
call   1040 <writeValReverse@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <readVal@plt>
cmpl   $0x0,0xc(%rsp)
je     1190 <main+0x50>
xor    %eax,%eax
jmp    11c8 <main+0x88>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: flipbits [width]\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
store i32 8, i32* %6
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %6
br label %16
16: ; preds = %11, %2
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = load i32, i32* %6
%21 = icmp ugt i32 %20, 64
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 2
br i1 %24, label %25, label %28
25: ; preds = %22, %19, %16
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %42
28: ; preds = %22
%29 = load i32, i32* %6
%30 = call i32 @readVal(i32 %29, i32* %7)
store i32 %30, i32* %8
br label %31
31: ; preds = %35, %28
%32 = load i32, i32* %7
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32, i32* %8
%37 = load i32, i32* %6
%38 = call i32 @writeValReverse(i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = call i32 @readVal(i32 %39, i32* %7)
store i32 %40, i32* %8
br label %31
41: ; preds = %31
store i32 0, i32* %3
br label %42
42: ; preds = %41, %25
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Usage: flipbits [width]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
br label %9
9: ; preds = %5, %2
%10 = phi i32 [ %8, %5 ], [ 8, %2 ]
%11 = add i32 %10, -65
%12 = icmp ult i32 %11, -64
%13 = icmp sgt i32 %0, 2
%14 = or i1 %13, %12
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %30
18: ; preds = %9
%19 = bitcast i32* %3 to i8*
%20 = call i32 @readVal(i32 %10, i32* %3)
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %18, %23
%24 = phi i32 [ %26, %23 ], [ %20, %18 ]
%25 = call i32 @writeValReverse(i32 %24, i32 %10)
%26 = call i32 @readVal(i32 %10, i32* %3)
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
br i1 %28, label %23, label %29
29: ; preds = %23, %18
br label %30
30: ; preds = %29, %15
%31 = phi i32 [ 1, %15 ], [ 0, %29 ]
ret i32 %31
}