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

/* Variables and functions */
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int console_open(int);
scalar_t__ islower(int);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ set_term(int /*<<< orphan*/);
int /*<<< orphan*/ strchr(char *, int);
int toupper(int);
int /*<<< orphan*/ write(int, char const *, int);
int ask_runlevel(void) {
  const char prompt[] = "\nEnter runlevel: ";
  char buf[8];
  int lvl = -1;
  int fd;
  set_term(0);
  fd = console_open(O_RDWR | O_NOCTTY);
  if (fd < 0)
    return ('S');
  while (!strchr("0123456789S", lvl)) {
    write(fd, prompt, sizeof(prompt) - 1);
    buf[0] = 0;
    read(fd, buf, sizeof(buf));
    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    0xe71(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe6e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe6b(%rip),%ax
mov    %ax,-0x10(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
xor    %edi,%edi
call   10a0 <set_term@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
or     (%rax),%edi
call   1060 <console_open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11e7 <ask_runlevel+0x67>
movl   $0x53,-0x4(%rbp)
jmp    1294 <ask_runlevel+0x114>
jmp    11ec <ask_runlevel+0x6c>
mov    -0x2c(%rbp),%esi
lea    0xe1c(%rip),%rdi
call   1070 <strchr@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1210 <ask_runlevel+0x90>
jmp    1286 <ask_runlevel+0x106>
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x11,%edx
call   1050 <write@plt>
movb   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1090 <read@plt>
movsbl -0x28(%rbp),%eax
cmp    $0x0,%eax
je     1264 <ask_runlevel+0xe4>
movsbl -0x27(%rbp),%eax
cmp    $0xd,%eax
je     125d <ask_runlevel+0xdd>
movsbl -0x27(%rbp),%eax
cmp    $0xa,%eax
jne    1264 <ask_runlevel+0xe4>
movsbl -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <islower@plt>
cmp    $0x0,%rax
je     1281 <ask_runlevel+0x101>
mov    -0x2c(%rbp),%edi
call   1040 <toupper@plt>
mov    %eax,-0x2c(%rbp)
jmp    11ec <ask_runlevel+0x6c>
mov    -0x30(%rbp),%edi
call   1080 <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   %r12
push   %rbx
sub    $0x30,%rsp
movaps 0xe6d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movw   $0x20,0x20(%rsp)
xor    %edi,%edi
call   10a0 <set_term@plt>
mov    0x2e3b(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1060 <console_open@plt>
test   %eax,%eax
js     11e6 <ask_runlevel+0x66>
mov    %eax,%ebx
lea    0xe48(%rip),%rdi
mov    $0xffffffff,%ebp
mov    $0xffffffff,%esi
call   1070 <strchr@plt>
test   %eax,%eax
je     11fa <ask_runlevel+0x7a>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    11eb <ask_runlevel+0x6b>
mov    $0x53,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0xe02(%rip),%r12
jmp    122e <ask_runlevel+0xae>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %ebp,%esi
call   1070 <strchr@plt>
test   %eax,%eax
jne    11dd <ask_runlevel+0x5d>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x11,%edx
call   1050 <write@plt>
movb   $0x0,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1090 <read@plt>
movsbl 0x8(%rsp),%eax
test   %eax,%eax
je     126b <ask_runlevel+0xeb>
movzbl 0x9(%rsp),%ecx
cmp    $0xd,%cl
je     1269 <ask_runlevel+0xe9>
cmp    $0xa,%cl
jne    126b <ask_runlevel+0xeb>
mov    %eax,%ebp
mov    %ebp,%edi
call   1030 <islower@plt>
test   %rax,%rax
je     1220 <ask_runlevel+0xa0>
mov    %ebp,%edi
call   1040 <toupper@plt>
mov    %eax,%ebp
jmp    1220 <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"
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 @set_term(i32 0)
%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 %59
15: ; preds = %0
br label %16
16: ; preds = %54, %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 %55
21: ; preds = %16
%22 = load i32, i32* %5
%23 = getelementptr inbounds [18 x i8], [18 x i8]* %2, i64 0, i64 0
%24 = call i32 @write(i32 %22, i8* %23, i32 17)
%25 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
store i8 0, i8* %25
%26 = load i32, i32* %5
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 8)
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %21
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 13
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %47
43: ; preds = %38, %33
%44 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
store i32 %46, i32* %4
br label %47
47: ; preds = %43, %38, %21
%48 = load i32, i32* %4
%49 = call i64 @islower(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = load i32, i32* %4
%53 = call i32 @toupper(i32 %52)
store i32 %53, i32* %4
br label %54
54: ; preds = %51, %47
br label %16
55: ; preds = %16
%56 = load i32, i32* %5
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %4
store i32 %58, i32* %1
br label %59
59: ; preds = %55, %14
%60 = load i32, i32* %1
ret i32 %60
}

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"
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 @set_term(i32 0)
%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 %39, 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 %36
14: ; preds = %11
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %2, i64 0, i64 1
br label %16
16: ; preds = %14, %32
%17 = phi i32 [ -1, %14 ], [ %33, %32 ]
%18 = call i32 @write(i32 %9, i8* %3, i32 17)
store i8 0, i8* %4
%19 = call i32 @read(i32 %9, i8* %4, i32 8)
%20 = load i8, i8* %4
%21 = sext i8 %20 to i32
%22 = icmp eq i8 %20, 0
br i1 %22, label %26, label %23
23: ; preds = %16
%24 = load i8, i8* %15
switch i8 %24, label %26 [
i8 13, label %25
i8 10, label %25
]
25: ; preds = %23, %23
br label %26
26: ; preds = %23, %25, %16
%27 = phi i32 [ %21, %25 ], [ %17, %23 ], [ %17, %16 ]
%28 = tail call i64 @islower(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = tail call i32 @toupper(i32 %27)
br label %32
32: ; preds = %30, %26
%33 = phi i32 [ %31, %30 ], [ %27, %26 ]
%34 = tail call i32 @strchr(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %33)
%35 = icmp eq i32 %34, 0
br i1 %35, label %16, label %36
36: ; preds = %32, %11
%37 = phi i32 [ -1, %11 ], [ %33, %32 ]
%38 = tail call i32 @close(i32 %9)
br label %39
39: ; preds = %0, %36
%40 = phi i32 [ %37, %36 ], [ 83, %0 ]
ret i32 %40
}