#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 uint8_t;
typedef int int16_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ kbhit();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ setup();
int updateDial();
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  setup();
  usleep(100000);
  int16_t i, active;
  uint8_t dial;
  while (!kbhit()) {
    for (i = 0; i < 5000; i++) {
      if (dial = updateDial()) {
        active = 1000;
        if (dial == 1)
          printf("D=D\n");
        else
          printf("D=U\n");
      }
      if (active > 0) {
        active--;
        usleep(200);
      } else {
        usleep(5000);
      }
    }
  }
  printf("Powering down...");
  char buffer[32];
  read(STDIN_FILENO, buffer, 32);
  printf("done.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <setup@plt>
mov    $0x186a0,%edi
call   1080 <usleep@plt>
mov    $0x0,%al
call   1050 <kbhit@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a3 <main+0x43>
jmp    1241 <main+0xe1>
movl   $0x0,-0x14(%rbp)
cmpl   $0x1388,-0x14(%rbp)
jge    123c <main+0xdc>
mov    $0x0,%al
call   1030 <updateDial@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     11fd <main+0x9d>
movl   $0x3e8,-0x18(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    11ec <main+0x8c>
lea    0xe1e(%rip),%rdi
call   1040 <printf@plt>
jmp    11f8 <main+0x98>
lea    0xe12(%rip),%rdi
call   1040 <printf@plt>
jmp    11fd <main+0x9d>
cmpl   $0x0,-0x18(%rbp)
jle    121f <main+0xbf>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    $0xc8,%edi
call   1080 <usleep@plt>
jmp    1229 <main+0xc9>
mov    $0x1388,%edi
call   1080 <usleep@plt>
jmp    122e <main+0xce>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11aa <main+0x4a>
jmp    1187 <main+0x27>
lea    0xdc2(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
xor    %eax,%eax
call   1080 <setup@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
xor    %eax,%eax
call   1060 <kbhit@plt>
test   %eax,%eax
je     11d1 <main+0x61>
lea    0xe63(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    $0x20,%edx
call   1070 <read@plt>
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe43(%rip),%r15
lea    0xe38(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <updateDial@plt>
test   %eax,%eax
je     1220 <main+0xb0>
mov    %r15,%rdi
cmp    $0x1,%eax
je     1206 <main+0x96>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    $0x3e8,%ebx
jmp    1224 <main+0xb4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
jle    124e <main+0xde>
add    $0xffffffff,%ebx
mov    $0xc8,%edi
call   1090 <usleep@plt>
add    $0x1,%ebp
cmp    $0x1388,%ebp
jne    11f0 <main+0x80>
xor    %ebp,%ebp
xor    %eax,%eax
call   1060 <kbhit@plt>
test   %eax,%eax
je     11f0 <main+0x80>
jmp    1196 <main+0x26>
mov    $0x1388,%edi
jmp    122c <main+0xbc>

LLVM IR (-O0):
@.str = [5 x i8] c"D=D\0A\00"
@.str.1 = [5 x i8] c"D=U\0A\00"
@.str.2 = [17 x i8] c"Powering down...\00"
@STDIN_FILENO = global i32 0
@.str.3 = [7 x i8] c"done.\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
%9 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @setup()
%11 = call i32 @usleep(i32 100000)
br label %12
12: ; preds = %44, %2
%13 = call i32 (...) @kbhit()
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %45
16: ; preds = %12
store i32 0, i32* %6
br label %17
17: ; preds = %41, %16
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 5000
br i1 %19, label %20, label %44
20: ; preds = %17
%21 = call i32 (...) @updateDial()
store i32 %21, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
store i32 1000, i32* %7
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %30
28: ; preds = %23
%29 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
br label %31
31: ; preds = %30, %20
%32 = load i32, i32* %7
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = load i32, i32* %7
%36 = add nsw i32 %35, -1
store i32 %36, i32* %7
%37 = call i32 @usleep(i32 200)
br label %40
38: ; preds = %31
%39 = call i32 @usleep(i32 5000)
br label %40
40: ; preds = %38, %34
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %17
44: ; preds = %17
br label %12
45: ; preds = %12
%46 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* @STDIN_FILENO
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 32)
%50 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.2 = [17 x i8] c"Powering down...\00"
@STDIN_FILENO = global i32 0
@str = [6 x i8] c"done.\00"
@str.4 = [4 x i8] c"D=U\00"
@str.5 = [4 x i8] c"D=D\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = tail call i32 (...) @setup()
%5 = tail call i32 @usleep(i32 100000)
%6 = tail call i32 (...) @kbhit()
%7 = icmp eq i32 %6, 0
br i1 %7, label %11, label %32
8: ; preds = %24
%9 = tail call i32 (...) @kbhit()
%10 = icmp eq i32 %9, 0
br i1 %10, label %30, label %32
11: ; preds = %2, %30
%12 = phi i32 [ %31, %30 ], [ 0, %2 ]
%13 = phi i32 [ %26, %30 ], [ undef, %2 ]
%14 = tail call i32 (...) @updateDial()
switch i32 %14, label %15 [
i32 0, label %16
i32 1, label %18
]
15: ; preds = %11
br label %18
16: ; preds = %11
%17 = icmp sgt i32 %13, 0
br i1 %17, label %21, label %24
18: ; preds = %11, %15
%19 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @str.4, i64 0, i64 0), %15 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @str.5, i64 0, i64 0), %11 ]
%20 = tail call i32 @puts(i8* dereferenceable(1) %19)
br label %21
21: ; preds = %18, %16
%22 = phi i32 [ %13, %16 ], [ 1000, %18 ]
%23 = add nsw i32 %22, -1
br label %24
24: ; preds = %16, %21
%25 = phi i32 [ 200, %21 ], [ 5000, %16 ]
%26 = phi i32 [ %23, %21 ], [ %13, %16 ]
%27 = tail call i32 @usleep(i32 %25)
%28 = add nuw nsw i32 %12, 1
%29 = icmp eq i32 %28, 5000
br i1 %29, label %8, label %30
30: ; preds = %24, %8
%31 = phi i32 [ %28, %24 ], [ 0, %8 ]
br label %11
32: ; preds = %8, %2
%33 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%35 = load i32, i32* @STDIN_FILENO
%36 = call i32 @read(i32 %35, i8* %34, i32 32)
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
ret i32 0
}