#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 /*<<< orphan*/ log_warn(char *, int);
int read(int, char *, int);
int read_slot(int client) {
  int r;
  char c;
  char last_char = 0;
  int count = 0;
  while (count < 2) {
    r = read(client, &c, 1);
    if (r == 0) {
      log_warn("read_slot: host %d: disconnected.", client);
      break;
    }
    if (r == -1) {
      log_warn("read_slot: host %d: parsing failed. ending client connection.",
               client);
      break;
    }
    if (c == '\n' && last_char >= '0' && last_char <= '9') {
      return (int)(last_char - '0');
    } else if (c >= '0' && c <= '9') {
      last_char = c;
    } else {
      break;
    }
    count++;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_slot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x0,-0xe(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x14(%rbp)
jge    1203 <read_slot+0xe3>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1172 <read_slot+0x52>
mov    -0x8(%rbp),%esi
lea    0xe98(%rip),%rdi
call   1040 <log_warn@plt>
jmp    1203 <read_slot+0xe3>
cmpl   $0xffffffff,-0xc(%rbp)
jne    1190 <read_slot+0x70>
mov    -0x8(%rbp),%esi
lea    0xe9c(%rip),%rdi
call   1040 <log_warn@plt>
jmp    1203 <read_slot+0xe3>
movsbl -0xd(%rbp),%eax
cmp    $0xa,%eax
jne    11c6 <read_slot+0xa6>
movsbl -0xe(%rbp),%eax
cmp    $0x30,%eax
jl     11c6 <read_slot+0xa6>
movsbl -0xe(%rbp),%eax
cmp    $0x39,%eax
jg     11c6 <read_slot+0xa6>
movsbl -0xe(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x4(%rbp)
jmp    120a <read_slot+0xea>
movsbl -0xd(%rbp),%eax
cmp    $0x30,%eax
jl     11eb <read_slot+0xcb>
movsbl -0xd(%rbp),%eax
cmp    $0x39,%eax
jg     11eb <read_slot+0xcb>
mov    -0xd(%rbp),%al
mov    %al,-0xe(%rbp)
jmp    11f0 <read_slot+0xd0>
jmp    1203 <read_slot+0xe3>
jmp    11f5 <read_slot+0xd5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1136 <read_slot+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_slot>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1188 <read_slot+0x68>
test   %eax,%eax
jne    114b <read_slot+0x2b>
lea    0xeb7(%rip),%rdi
jmp    118f <read_slot+0x6f>
movzbl 0xf(%rsp),%r14d
lea    -0x30(%r14),%eax
mov    $0xffffffff,%ebp
cmp    $0xa,%al
jae    119b <read_slot+0x7b>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1188 <read_slot+0x68>
test   %eax,%eax
je     1142 <read_slot+0x22>
cmpb   $0xa,0xf(%rsp)
jne    119b <read_slot+0x7b>
add    $0xffffffd0,%r14d
mov    %r14d,%ebp
jmp    119b <read_slot+0x7b>
lea    0xe93(%rip),%rdi
mov    %ebx,%esi
call   1040 <log_warn@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [34 x i8] c"read_slot: host %d: disconnected.\00"
@.str.1 = [62 x i8] c"read_slot: host %d: parsing failed. ending client connection.\00"
define i32 @read_slot(i32 %0) {
%2 = alloca i8
%3 = call i32 @read(i32 %0, i8* %2, i32 1)
switch i32 %3, label %16 [
i32 0, label %9
i32 -1, label %11
]
4: ; preds = %16
%5 = call i32 @read(i32 %0, i8* %2, i32 1)
switch i32 %5, label %6 [
i32 0, label %9
i32 -1, label %11
]
6: ; preds = %4
%7 = load i8, i8* %2
%8 = icmp eq i8 %7, 10
br i1 %8, label %13, label %20
9: ; preds = %4, %1
%10 = tail call i32 @log_warn(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %20
11: ; preds = %4, %1
%12 = tail call i32 @log_warn(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %20
13: ; preds = %6
%14 = zext i8 %17 to i32
%15 = add nsw i32 %14, -48
br label %20
16: ; preds = %1
%17 = load i8, i8* %2
%18 = add i8 %17, -48
%19 = icmp ult i8 %18, 10
br i1 %19, label %4, label %20
20: ; preds = %6, %16, %9, %11, %13
%21 = phi i32 [ %15, %13 ], [ -1, %11 ], [ -1, %9 ], [ -1, %16 ], [ -1, %6 ]
ret i32 %21
}