#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 */

/* Variables and functions */
int read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usleep(int);
int get_cmd(int s, char *str, int len) {
  char c;
  int i = 0;
  int n = 0;
  int tmo = 50;
  while (1) {
    n = read(s, &c, 1);
    if (n == 0) {
      if (--tmo <= 0) {
        return -2;
      }
      usleep(100000);
      continue;
    }
    if (c == '\n') {
      break;
    }
    if (n < 0) {
      return -1;
    }
    str[i] = c;
    i++;
  }
  str[i] = 0;
  if (strcmp(str, "read") == 0) {
    return 0;
  }
  if (strcmp(str, "on") == 0) {
    return 1;
  }
  if (strcmp(str, "off") == 0) {
    return 2;
  }
  return -1;
}


x86 assembly (clang, -O0):
<get_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x32,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11a2 <get_cmd+0x72>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     1193 <get_cmd+0x63>
movl   $0xfffffffe,-0x4(%rbp)
jmp    126b <get_cmd+0x13b>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
jmp    1157 <get_cmd+0x27>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    11b4 <get_cmd+0x84>
jmp    11e6 <get_cmd+0xb6>
cmpl   $0x0,-0x20(%rbp)
jge    11ca <get_cmd+0x9a>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <get_cmd+0x13b>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1157 <get_cmd+0x27>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    0xe03(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1218 <get_cmd+0xe8>
movl   $0x0,-0x4(%rbp)
jmp    126b <get_cmd+0x13b>
mov    -0x10(%rbp),%rdi
lea    0xde2(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    123e <get_cmd+0x10e>
movl   $0x1,-0x4(%rbp)
jmp    126b <get_cmd+0x13b>
mov    -0x10(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1264 <get_cmd+0x134>
movl   $0x2,-0x4(%rbp)
jmp    126b <get_cmd+0x13b>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x32,%ebx
xor    %r15d,%r15d
lea    0xf(%rsp),%r12
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jne    1177 <get_cmd+0x47>
cmp    $0x2,%ebx
jl     118f <get_cmd+0x5f>
add    $0xffffffff,%ebx
mov    $0x186a0,%edi
call   1050 <usleep@plt>
jmp    1150 <get_cmd+0x20>
movzbl 0xf(%rsp),%ecx
cmp    $0xa,%cl
je     11a1 <get_cmd+0x71>
test   %eax,%eax
js     11ea <get_cmd+0xba>
mov    %cl,(%r14,%r15,1)
add    $0x1,%r15
jmp    1150 <get_cmd+0x20>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15d,%eax
movb   $0x0,(%r14,%rax,1)
lea    0xe50(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11f1 <get_cmd+0xc1>
lea    0xe41(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11f5 <get_cmd+0xc5>
lea    0xe30(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcmp@plt>
neg    %rax
sbb    %eax,%eax
or     $0x2,%eax
jmp    1194 <get_cmd+0x64>
mov    $0xffffffff,%eax
jmp    1194 <get_cmd+0x64>
xor    %eax,%eax
jmp    1194 <get_cmd+0x64>
mov    $0x1,%eax
jmp    1194 <get_cmd+0x64>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [3 x i8] c"on\00"
@.str.2 = [4 x i8] c"off\00"
define i32 @get_cmd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
store i32 0, i32* %10
store i32 50, i32* %11
br label %12
12: ; preds = %3, %22, %33
%13 = load i32, i32* %5
%14 = call i32 @read(i32 %13, i8* %8, i32 1)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* %11
%19 = add nsw i32 %18, -1
store i32 %19, i32* %11
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -2, i32* %4
br label %61
22: ; preds = %17
%23 = call i32 @usleep(i32 100000)
br label %12
24: ; preds = %12
%25 = load i8, i8* %8
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %29
28: ; preds = %24
br label %41
29: ; preds = %24
%30 = load i32, i32* %10
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 -1, i32* %4
br label %61
33: ; preds = %29
%34 = load i8, i8* %8
%35 = load i8*, i8** %6
%36 = load i32, i32* %9
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
%39 = load i32, i32* %9
%40 = add nsw i32 %39, 1
store i32 %40, i32* %9
br label %12
41: ; preds = %28
%42 = load i8*, i8** %6
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %6
%47 = call i64 @strcmp(i8* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %41
store i32 0, i32* %4
br label %61
50: ; preds = %41
%51 = load i8*, i8** %6
%52 = call i64 @strcmp(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 1, i32* %4
br label %61
55: ; preds = %50
%56 = load i8*, i8** %6
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %55
store i32 2, i32* %4
br label %61
60: ; preds = %55
store i32 -1, i32* %4
br label %61
61: ; preds = %60, %59, %54, %49, %32, %21
%62 = load i32, i32* %4
ret i32 %62
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [3 x i8] c"on\00"
@.str.2 = [4 x i8] c"off\00"
define i32 @get_cmd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
br label %5
5: ; preds = %25, %3
%6 = phi i64 [ %27, %25 ], [ 0, %3 ]
%7 = phi i32 [ %19, %25 ], [ 50, %3 ]
%8 = call i32 @read(i32 %0, i8* %4, i32 1)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %5, %13
%11 = phi i32 [ %14, %13 ], [ %7, %5 ]
%12 = icmp slt i32 %11, 2
br i1 %12, label %40, label %13
13: ; preds = %10
%14 = add nsw i32 %11, -1
%15 = tail call i32 @usleep(i32 100000)
%16 = call i32 @read(i32 %0, i8* %4, i32 1)
%17 = icmp eq i32 %16, 0
br i1 %17, label %10, label %18
18: ; preds = %13, %5
%19 = phi i32 [ %7, %5 ], [ %14, %13 ]
%20 = phi i32 [ %8, %5 ], [ %16, %13 ]
%21 = load i8, i8* %4
%22 = icmp eq i8 %21, 10
br i1 %22, label %28, label %23
23: ; preds = %18
%24 = icmp slt i32 %20, 0
br i1 %24, label %40, label %25
25: ; preds = %23
%26 = getelementptr inbounds i8, i8* %1, i64 %6
store i8 %21, i8* %26
%27 = add nuw i64 %6, 1
br label %5
28: ; preds = %18
%29 = and i64 %6, 4294967295
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 0, i8* %30
%31 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %28
%34 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
%39 = select i1 %38, i32 2, i32 -1
br label %40
40: ; preds = %23, %10, %36, %33, %28
%41 = phi i32 [ 0, %28 ], [ 1, %33 ], [ %39, %36 ], [ -2, %10 ], [ -1, %23 ]
ret i32 %41
}