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

/* Variables and functions */
int /*<<< orphan*/ alarm(int);
int atoi(char *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int strlen(char *);
int strspn(char *, char *);
int read_response(FILE *fp, int timeout) {
  char buf[1024];
  char *ptr;
  for (;;) {
    alarm(timeout);
    ptr = fgets(buf, 1023, fp);
    alarm(0);
    if (!ptr) {
      puts("<EOF>");
      return 0;
    }
    ptr += strlen(ptr);
    if (*--ptr == '\n')
      if (*--ptr == '\r')
        *ptr = '\0';
    printf("%s\n", buf);
    if (strlen(buf) < 3 || strspn(buf, "0123456789") != 3) {
      puts("*** malformed response");
    } else if (buf[3] == ' ' || buf[3] == '\0') {
      return atoi(buf);
    }
  }
}


x86 assembly (clang, -O0):
<read_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <alarm@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x3ff,%esi
call   1080 <fgets@plt>
mov    %rax,-0x428(%rbp)
xor    %edi,%edi
call   1060 <alarm@plt>
cmpq   $0x0,-0x428(%rbp)
jne    11d3 <read_response+0x63>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    12db <read_response+0x16b>
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x428(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x428(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0xa,%eax
jne    1248 <read_response+0xd8>
mov    -0x428(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x428(%rbp)
movsbl -0x1(%rax),%eax
cmp    $0xd,%eax
jne    1243 <read_response+0xd3>
mov    -0x428(%rbp),%rax
movb   $0x0,(%rax)
jmp    1248 <read_response+0xd8>
lea    -0x420(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   1050 <printf@plt>
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x3,%eax
jl     128c <read_response+0x11c>
lea    -0x420(%rbp),%rdi
lea    0xd8c(%rip),%rsi
call   1070 <strspn@plt>
cmp    $0x3,%eax
je     129d <read_response+0x12d>
lea    0xd82(%rip),%rdi
call   1030 <puts@plt>
jmp    12d6 <read_response+0x166>
movsbl -0x41d(%rbp),%eax
cmp    $0x20,%eax
je     12bd <read_response+0x14d>
movsbl -0x41d(%rbp),%eax
cmp    $0x0,%eax
jne    12d1 <read_response+0x161>
lea    -0x420(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x4(%rbp)
jmp    12db <read_response+0x16b>
jmp    12d6 <read_response+0x166>
jmp    1182 <read_response+0x12>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_response>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r12d
mov    %rdi,%rbx
mov    %esi,%edi
call   1060 <alarm@plt>
mov    %rsp,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rax,%r14
xor    %edi,%edi
call   1060 <alarm@plt>
test   %r14,%r14
je     125c <read_response+0xec>
lea    0xe4e(%rip),%r15
mov    %rsp,%rbp
lea    0xe53(%rip),%r13
jmp    11ff <read_response+0x8f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
call   1060 <alarm@plt>
mov    %rbp,%rdi
mov    $0x3ff,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rax,%r14
xor    %edi,%edi
call   1060 <alarm@plt>
test   %r14,%r14
je     125c <read_response+0xec>
mov    %r14,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%r14,%rax,1)
jne    121f <read_response+0xaf>
cmpb   $0xd,-0x2(%r14,%rax,1)
jne    121f <read_response+0xaf>
movb   $0x0,-0x2(%r14,%rax,1)
mov    %r15,%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0x3,%eax
jl     11d0 <read_response+0x60>
mov    %rbp,%rdi
lea    0xdc9(%rip),%rsi
call   1070 <strspn@plt>
cmp    $0x3,%eax
jne    11d0 <read_response+0x60>
testb  $0xdf,0x3(%rsp)
jne    11d8 <read_response+0x68>
mov    %rsp,%rdi
call   1090 <atoi@plt>
jmp    126a <read_response+0xfa>
lea    0xd9d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"<EOF>\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [11 x i8] c"0123456789\00"
@.str.3 = [23 x i8] c"*** malformed response\00"
define i32 @read_response(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
br label %8
8: ; preds = %65, %2
%9 = load i32, i32* %5
%10 = call i32 @alarm(i32 %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = load i32*, i32** %4
%13 = call i8* @fgets(i8* %11, i32 1023, i32* %12)
store i8* %13, i8** %7
%14 = call i32 @alarm(i32 0)
%15 = load i8*, i8** %7
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %8
%18 = call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %66
19: ; preds = %8
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = load i8*, i8** %7
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = getelementptr inbounds i8, i8* %25, i32 -1
store i8* %26, i8** %7
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %39
30: ; preds = %19
%31 = load i8*, i8** %7
%32 = getelementptr inbounds i8, i8* %31, i32 -1
store i8* %32, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 13
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = load i8*, i8** %7
store i8 0, i8* %37
br label %38
38: ; preds = %36, %30
br label %39
39: ; preds = %38, %19
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = icmp slt i32 %43, 3
br i1 %44, label %49, label %45
45: ; preds = %39
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = call i32 @strspn(i8* %46, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp ne i32 %47, 3
br i1 %48, label %49, label %51
49: ; preds = %45, %39
%50 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %65
51: ; preds = %45
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 3
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 32
br i1 %55, label %61, label %56
56: ; preds = %51
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 3
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %56, %51
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%63 = call i32 @atoi(i8* %62)
store i32 %63, i32* %3
br label %66
64: ; preds = %56
br label %65
65: ; preds = %64, %49
br label %8
66: ; preds = %61, %17
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [6 x i8] c"<EOF>\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [11 x i8] c"0123456789\00"
@.str.3 = [23 x i8] c"*** malformed response\00"
define i32 @read_response(i32* nocapture %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @alarm(i32 %1)
%6 = call i8* @fgets(i8* %4, i32 1023, i32* %0)
%7 = call i32 @alarm(i32 0)
%8 = icmp eq i8* %6, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 3
br label %13
11: ; preds = %41, %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %46
13: ; preds = %9, %41
%14 = phi i8* [ %6, %9 ], [ %43, %41 ]
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 -1
%18 = getelementptr inbounds i8, i8* %17, i64 %16
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = getelementptr inbounds i8, i8* %18, i64 -1
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 13
br i1 %24, label %25, label %26
25: ; preds = %21
store i8 0, i8* %22
br label %26
26: ; preds = %21, %25, %13
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%28 = call i32 @strlen(i8* %4)
%29 = icmp slt i32 %28, 3
br i1 %29, label %33, label %30
30: ; preds = %26
%31 = call i32 @strspn(i8* %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%32 = icmp eq i32 %31, 3
br i1 %32, label %35, label %33
33: ; preds = %30, %26
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %41
35: ; preds = %30
%36 = load i8, i8* %10
%37 = and i8 %36, -33
%38 = icmp eq i8 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @atoi(i8* %4)
br label %46
41: ; preds = %35, %33
%42 = call i32 @alarm(i32 %1)
%43 = call i8* @fgets(i8* %4, i32 1023, i32* %0)
%44 = call i32 @alarm(i32 0)
%45 = icmp eq i8* %43, null
br i1 %45, label %11, label %13
46: ; preds = %39, %11
%47 = phi i32 [ %40, %39 ], [ 0, %11 ]
ret i32 %47
}