#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ prompt;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int console_open(int);
int /*<<< orphan*/ console_stty();
scalar_t__ islower(int);
int read(int, char *, int);
int /*<<< orphan*/ strchr(char *, int);
int toupper(int);
int /*<<< orphan*/ warn(char *);
scalar_t__ write(int, char const *, int);
int ask_runlevel(void) {
  const char prompt[] = "\nEnter runlevel: ";
  char buf[8];
  int lvl = -1;
  int fd;
  console_stty();
  fd = console_open(O_RDWR | O_NOCTTY);
  if (fd < 0)
    return ('S');
  while (!strchr("0123456789S", lvl)) {
    if (write(fd, prompt, sizeof(prompt) - 1) < (ssize_t)(sizeof(prompt) - 1))
      warn("console write error");
    buf[0] = 0;
    if (read(fd, buf, sizeof(buf)) < 1)
      warn("console read error");
    if (buf[0] != 0 && (buf[1] == '\r' || buf[1] == '\n'))
      lvl = buf[0];
    if (islower(lvl))
      lvl = toupper(lvl);
  }
  close(fd);
  return lvl;
}


x86 assembly (clang, -O0):
<ask_runlevel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xe61(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe5e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe5b(%rip),%ax
mov    %ax,-0x10(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <console_stty@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
or     (%rax),%edi
call   1080 <console_open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11f7 <ask_runlevel+0x67>
movl   $0x53,-0x4(%rbp)
jmp    12cf <ask_runlevel+0x13f>
jmp    11fc <ask_runlevel+0x6c>
mov    -0x2c(%rbp),%esi
lea    0xe0c(%rip),%rdi
call   1090 <strchr@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1220 <ask_runlevel+0x90>
jmp    12c1 <ask_runlevel+0x131>
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x11,%edx
call   1070 <write@plt>
cmp    $0x11,%rax
jge    1247 <ask_runlevel+0xb7>
lea    0xddc(%rip),%rdi
call   1040 <warn@plt>
movb   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10b0 <read@plt>
cmp    $0x1,%eax
jge    1271 <ask_runlevel+0xe1>
lea    0xdc6(%rip),%rdi
call   1040 <warn@plt>
movsbl -0x28(%rbp),%eax
cmp    $0x0,%eax
je     129f <ask_runlevel+0x10f>
movsbl -0x27(%rbp),%eax
cmp    $0xd,%eax
je     1298 <ask_runlevel+0x108>
movsbl -0x27(%rbp),%eax
cmp    $0xa,%eax
jne    129f <ask_runlevel+0x10f>
movsbl -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <islower@plt>
cmp    $0x0,%rax
je     12bc <ask_runlevel+0x12c>
mov    -0x2c(%rbp),%edi
call   1060 <toupper@plt>
mov    %eax,-0x2c(%rbp)
jmp    11fc <ask_runlevel+0x6c>
mov    -0x30(%rbp),%edi
call   10a0 <close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ask_runlevel>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movaps 0xe5b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x20,0x20(%rsp)
xor    %eax,%eax
call   1030 <console_stty@plt>
mov    0x2e29(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1080 <console_open@plt>
test   %eax,%eax
js     11f8 <ask_runlevel+0x68>
mov    %eax,%ebx
lea    0xe36(%rip),%rdi
mov    $0xffffffff,%ebp
mov    $0xffffffff,%esi
call   1090 <strchr@plt>
test   %eax,%eax
je     120e <ask_runlevel+0x7e>
mov    %ebx,%edi
call   10a0 <close@plt>
jmp    11fd <ask_runlevel+0x6d>
mov    $0x53,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
lea    0x10(%rsp),%r14
lea    0xdff(%rip),%r15
lea    0x8(%rsp),%r12
lea    0xde7(%rip),%r13
jmp    123e <ask_runlevel+0xae>
nopl   (%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1090 <strchr@plt>
test   %eax,%eax
jne    11ef <ask_runlevel+0x5f>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x11,%edx
call   1070 <write@plt>
cmp    $0x10,%rax
jg     125b <ask_runlevel+0xcb>
mov    %r15,%rdi
call   1040 <warn@plt>
movb   $0x0,0x8(%rsp)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   10b0 <read@plt>
test   %eax,%eax
jg     127f <ask_runlevel+0xef>
lea    0xdb8(%rip),%rdi
call   1040 <warn@plt>
movsbl 0x8(%rsp),%eax
test   %eax,%eax
je     1299 <ask_runlevel+0x109>
movzbl 0x9(%rsp),%ecx
cmp    $0xd,%cl
je     1297 <ask_runlevel+0x107>
cmp    $0xa,%cl
jne    1299 <ask_runlevel+0x109>
mov    %eax,%ebp
mov    %ebp,%edi
call   1050 <islower@plt>
test   %rax,%rax
je     1230 <ask_runlevel+0xa0>
mov    %ebp,%edi
call   1060 <toupper@plt>
mov    %eax,%ebp
jmp    1230 <ask_runlevel+0xa0>

LLVM IR (-O0):
@__const.ask_runlevel.prompt = [18 x i8] c"\0AEnter runlevel: \00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [12 x i8] c"0123456789S\00"
@.str.1 = [20 x i8] c"console write error\00"
@.str.2 = [19 x i8] c"console read error\00"
define i32 @ask_runlevel() {
%1 = alloca i32
%2 = alloca [18 x i8]
%3 = alloca [8 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [18 x i8]* %2 to i8*
store i32 -1, i32* %4
%7 = call i32 (...) @console_stty()
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @console_open(i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %0
store i32 83, i32* %1
br label %67
15: ; preds = %0
br label %16
16: ; preds = %62, %15
%17 = load i32, i32* %4
%18 = call i32 @strchr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp ne i32 %18, 0
%20 = xor i1 %19, true
br i1 %20, label %21, label %63
21: ; preds = %16
%22 = load i32, i32* %5
%23 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%24 = call i64 @write(i32 %22, i8* %23, i32 17)
%25 = icmp slt i64 %24, 17
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @warn(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %21
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
store i8 0, i8* %29
%30 = load i32, i32* %5
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 8)
%33 = icmp slt i32 %32, 1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %36
%42 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 13
br i1 %45, label %51, label %46
46: ; preds = %41
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 10
br i1 %50, label %51, label %55
51: ; preds = %46, %41
%52 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
store i32 %54, i32* %4
br label %55
55: ; preds = %51, %46, %36
%56 = load i32, i32* %4
%57 = call i64 @islower(i32 %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %4
%61 = call i32 @toupper(i32 %60)
store i32 %61, i32* %4
br label %62
62: ; preds = %59, %55
br label %16
63: ; preds = %16
%64 = load i32, i32* %5
%65 = call i32 @close(i32 %64)
%66 = load i32, i32* %4
store i32 %66, i32* %1
br label %67
67: ; preds = %63, %14
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
@__const.ask_runlevel.prompt = [18 x i8] c"\0AEnter runlevel: \00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str = [12 x i8] c"0123456789S\00"
@.str.1 = [20 x i8] c"console write error\00"
@.str.2 = [19 x i8] c"console read error\00"
define i32 @ask_runlevel() {
%1 = alloca [18 x i8]
%2 = alloca [8 x i8]
%3 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 0
%5 = tail call i32 (...) @console_stty()
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_NOCTTY
%8 = or i32 %7, %6
%9 = tail call i32 @console_open(i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %47, label %11
11: ; preds = %0
%12 = tail call i32 @strchr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 -1)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %44
14: ; preds = %11
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 1
br label %16
16: ; preds = %14, %40
%17 = phi i32 [ -1, %14 ], [ %41, %40 ]
%18 = call i64 @write(i32 %9, i8* %3, i32 17)
%19 = icmp slt i64 %18, 17
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @warn(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
store i8 0, i8* %4
%23 = call i32 @read(i32 %9, i8* %4, i32 8)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @warn(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %22
%28 = load i8, i8* %4
%29 = sext i8 %28 to i32
%30 = icmp eq i8 %28, 0
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = load i8, i8* %15
switch i8 %32, label %34 [
i8 13, label %33
i8 10, label %33
]
33: ; preds = %31, %31
br label %34
34: ; preds = %31, %33, %27
%35 = phi i32 [ %29, %33 ], [ %17, %31 ], [ %17, %27 ]
%36 = tail call i64 @islower(i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %34
%39 = tail call i32 @toupper(i32 %35)
br label %40
40: ; preds = %38, %34
%41 = phi i32 [ %39, %38 ], [ %35, %34 ]
%42 = tail call i32 @strchr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %41)
%43 = icmp eq i32 %42, 0
br i1 %43, label %16, label %44
44: ; preds = %40, %11
%45 = phi i32 [ -1, %11 ], [ %41, %40 ]
%46 = tail call i32 @close(i32 %9)
br label %47
47: ; preds = %0, %44
%48 = phi i32 [ %45, %44 ], [ 83, %0 ]
ret i32 %48
}