#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);
int /*<<< orphan*/ console_stty();
scalar_t__ islower(int);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ safe_write(int, char const *, int);
int /*<<< orphan*/ strchr(char *, int);
int toupper(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)) {
    safe_write(fd, prompt, sizeof(prompt) - 1);
    if (read(fd, buf, sizeof(buf)) <= 0)
      buf[0] = 0;
    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)
mov    $0x0,%al
call   1030 <console_stty@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    129e <ask_runlevel+0x11e>
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    1290 <ask_runlevel+0x110>
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x11,%edx
call   10a0 <safe_write@plt>
mov    -0x30(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jg     1240 <ask_runlevel+0xc0>
movb   $0x0,-0x28(%rbp)
movsbl -0x28(%rbp),%eax
cmp    $0x0,%eax
je     126e <ask_runlevel+0xee>
movsbl -0x27(%rbp),%eax
cmp    $0xd,%eax
je     1267 <ask_runlevel+0xe7>
movsbl -0x27(%rbp),%eax
cmp    $0xa,%eax
jne    126e <ask_runlevel+0xee>
movsbl -0x28(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <islower@plt>
cmp    $0x0,%rax
je     128b <ask_runlevel+0x10b>
mov    -0x2c(%rbp),%edi
call   1050 <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    %eax,%eax
call   1030 <console_stty@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   10a0 <safe_write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    1270 <ask_runlevel+0xf0>
movsbl 0x8(%rsp),%eax
test   %eax,%eax
je     1275 <ask_runlevel+0xf5>
movzbl 0x9(%rsp),%ecx
cmp    $0xd,%cl
je     1269 <ask_runlevel+0xe9>
cmp    $0xa,%cl
jne    1275 <ask_runlevel+0xf5>
mov    %eax,%ebp
jmp    1275 <ask_runlevel+0xf5>
nopl   (%rax)
movb   $0x0,0x8(%rsp)
mov    %ebp,%edi
call   1040 <islower@plt>
test   %rax,%rax
je     1220 <ask_runlevel+0xa0>
mov    %ebp,%edi
call   1050 <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 (...) @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 %62
15: ; preds = %0
br label %16
16: ; preds = %57, %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 %58
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 @safe_write(i32 %22, i8* %23, i32 17)
%25 = load i32, i32* %5
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%27 = call i64 @read(i32 %25, i8* %26, i32 8)
%28 = icmp sle i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
store i8 0, i8* %30
br label %31
31: ; preds = %29, %21
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %50
36: ; preds = %31
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 13
br i1 %40, label %46, label %41
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, 10
br i1 %45, label %46, label %50
46: ; preds = %41, %36
%47 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %41, %31
%51 = load i32, i32* %4
%52 = call i64 @islower(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i32, i32* %4
%56 = call i32 @toupper(i32 %55)
store i32 %56, i32* %4
br label %57
57: ; preds = %54, %50
br label %16
58: ; preds = %16
%59 = load i32, i32* %5
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %4
store i32 %61, i32* %1
br label %62
62: ; preds = %58, %14
%63 = load i32, i32* %1
ret i32 %63
}

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