#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 uint8_t;
typedef scalar_t__ time_t;

/* Variables and functions */
scalar_t__ EAGAIN;
int EINVAL;
int EIO;
int ETIMEDOUT;
int /*<<< orphan*/ discard_input(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int read(int, int *, int);
int /*<<< orphan*/ stderr;
scalar_t__ time(int /*<<< orphan*/ *);
int wait_for_ack(int fd) {
  uint8_t resp;
  int res;
  time_t deadline = time(NULL) + 4;
  while (time(NULL) < deadline) {
    res = read(fd, &resp, 1);
    if ((res < 0) && (errno != EAGAIN)) {
      perror("Failed to read answer");
      return -EIO;
    }
    if (res == 1) {
      if (resp == 0x79)
        return 0;
      else if (resp == 0x1f) {
        fprintf(stderr, "NACK\n");
        discard_input(fd);
        return -EINVAL;
      } else {
        fprintf(stderr, "Receive junk: %02x\n", resp);
      }
    }
  }
  fprintf(stderr, "Timeout\n");
  return -ETIMEDOUT;
}


x86 assembly (clang, -O0):
<wait_for_ack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
cmp    -0x18(%rbp),%rax
jge    125b <wait_for_ack+0x10b>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d6 <wait_for_ack+0x86>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
je     11d6 <wait_for_ack+0x86>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
mov    0x2e14(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <wait_for_ack+0x130>
cmpl   $0x1,-0x10(%rbp)
jne    1256 <wait_for_ack+0x106>
cmpl   $0x79,-0xc(%rbp)
jne    11f6 <wait_for_ack+0xa6>
movl   $0x0,-0x4(%rbp)
jmp    1280 <wait_for_ack+0x130>
cmpl   $0x1f,-0xc(%rbp)
jne    1232 <wait_for_ack+0xe2>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1070 <discard_input@plt>
xor    %eax,%eax
mov    0x2d98(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1280 <wait_for_ack+0x130>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1251 <wait_for_ack+0x101>
jmp    1256 <wait_for_ack+0x106>
jmp    116c <wait_for_ack+0x1c>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2d4d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_ack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %r14d,%r14d
xor    %edi,%edi
call   1050 <time@plt>
mov    %rax,%rbx
add    $0x4,%rbx
xor    %edi,%edi
call   1050 <time@plt>
cmp    %rbx,%rax
jge    11e9 <wait_for_ack+0x99>
lea    0x4(%rsp),%r15
mov    0x2e4a(%rip),%r13
mov    0x2e23(%rip),%r12
jmp    11aa <wait_for_ack+0x5a>
nop
mov    0x0(%r13),%rax
cmp    (%r12),%rax
jne    1244 <wait_for_ack+0xf4>
xor    %edi,%edi
call   1050 <time@plt>
cmp    %rbx,%rax
jge    11e9 <wait_for_ack+0x99>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1190 <wait_for_ack+0x40>
cmp    $0x1,%eax
jne    119e <wait_for_ack+0x4e>
mov    0x4(%rsp),%edx
cmp    $0x79,%edx
je     121c <wait_for_ack+0xcc>
mov    0x2e06(%rip),%rax
cmp    $0x1f,%edx
je     1221 <wait_for_ack+0xd1>
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    119e <wait_for_ack+0x4e>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2dc1(%rip),%rax
sub    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
jmp    120a <wait_for_ack+0xba>
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%edi
call   1070 <discard_input@plt>
mov    0x2d7e(%rip),%rax
jmp    1207 <wait_for_ack+0xb7>
lea    0xdb5(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d89(%rip),%rax
xor    %r14d,%r14d
jmp    1207 <wait_for_ack+0xb7>

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [22 x i8] c"Failed to read answer\00"
@EIO = global i32 0
@stderr = global i32 0
@.str.1 = [6 x i8] c"NACK\0A\00"
@EINVAL = global i32 0
@.str.2 = [20 x i8] c"Receive junk: %02x\0A\00"
@.str.3 = [9 x i8] c"Timeout\0A\00"
@ETIMEDOUT = global i32 0
define i32 @wait_for_ack(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
store i32 %0, i32* %3
%7 = call i64 @time(i32* null)
%8 = add nsw i64 %7, 4
store i64 %8, i64* %6
br label %9
9: ; preds = %49, %1
%10 = call i64 @time(i32* null)
%11 = load i64, i64* %6
%12 = icmp slt i64 %10, %11
br i1 %12, label %13, label %50
13: ; preds = %9
%14 = load i32, i32* %3
%15 = call i32 @read(i32 %14, i32* %4, i32 1)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @EAGAIN
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EIO
%25 = sub nsw i32 0, %24
store i32 %25, i32* %2
br label %55
26: ; preds = %18, %13
%27 = load i32, i32* %5
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %49
29: ; preds = %26
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 121
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 0, i32* %2
br label %55
33: ; preds = %29
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, 31
br i1 %35, label %36, label %43
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%39 = load i32, i32* %3
%40 = call i32 @discard_input(i32 %39)
%41 = load i32, i32* @EINVAL
%42 = sub nsw i32 0, %41
store i32 %42, i32* %2
br label %55
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = load i32, i32* %4
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %43
br label %48
48: ; preds = %47
br label %49
49: ; preds = %48, %26
br label %9
50: ; preds = %9
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @ETIMEDOUT
%54 = sub nsw i32 0, %53
store i32 %54, i32* %2
br label %55
55: ; preds = %50, %36, %32, %22
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [22 x i8] c"Failed to read answer\00"
@EIO = global i32 0
@stderr = global i32 0
@.str.1 = [6 x i8] c"NACK\0A\00"
@EINVAL = global i32 0
@.str.2 = [20 x i8] c"Receive junk: %02x\0A\00"
@.str.3 = [9 x i8] c"Timeout\0A\00"
@ETIMEDOUT = global i32 0
define i32 @wait_for_ack(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i64 @time(i32* null)
%5 = add nsw i64 %4, 4
%6 = tail call i64 @time(i32* null)
%7 = icmp slt i64 %6, %5
br i1 %7, label %8, label %35
8: ; preds = %1, %32
%9 = call i32 @read(i32 %0, i32* %2, i32 1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %8
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
br i1 %14, label %32, label %15
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EIO
%18 = sub nsw i32 0, %17
br label %40
19: ; preds = %8
%20 = icmp eq i32 %9, 1
br i1 %20, label %21, label %32
21: ; preds = %19
%22 = load i32, i32* %2
switch i32 %22, label %29 [
i32 121, label %40
i32 31, label %23
]
23: ; preds = %21
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = tail call i32 @discard_input(i32 %0)
%27 = load i32, i32* @EINVAL
%28 = sub nsw i32 0, %27
br label %40
29: ; preds = %21
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %32
32: ; preds = %11, %29, %19
%33 = tail call i64 @time(i32* null)
%34 = icmp slt i64 %33, %5
br i1 %34, label %8, label %35
35: ; preds = %32, %1
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @ETIMEDOUT
%39 = sub nsw i32 0, %38
br label %40
40: ; preds = %21, %35, %23, %15
%41 = phi i32 [ %18, %15 ], [ %28, %23 ], [ %39, %35 ], [ 0, %21 ]
ret i32 %41
}