#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 */
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int wait_for_ack(unsigned char *data, int serial_fd) {
  unsigned char ack_ack[0x0A] = {0xB5, 0x62, 0x05, 0x01, 0x02, 0x00};
  struct timeval select_tv;
  fd_set read_fd_set;
  int max_fd;
  int i;
  int ack_index;
  unsigned char data_temp;
  ack_index = 0;
  ack_ack[6] = *(data + 2);
  ack_ack[7] = *(data + 3);
  ack_ack[8] = 0;
  ack_ack[9] = 0;
  for (i = 2; i < 0x0A - 2; i++) {
    ack_ack[8] = ack_ack[8] + ack_ack[i];
    ack_ack[9] = ack_ack[9] + ack_ack[8];
  }
  select_tv.tv_sec = 0;
  select_tv.tv_usec = 900000;
  while (1) {
    FD_ZERO(&read_fd_set);
    FD_SET(0, &read_fd_set);
    FD_SET(serial_fd, &read_fd_set);
    max_fd = serial_fd;
    i = select(max_fd + 1, &read_fd_set, NULL, NULL, &select_tv);
    if (FD_ISSET(serial_fd, &read_fd_set)) {
      read(serial_fd, &data_temp, 1);
      if (data_temp != ack_ack[ack_index++]) {
        ack_index = 0;
      }
      if (ack_index == 0x0A) {
        printf("OK\n");
        return (0);
      }
    } else {
      printf("*** no ACK from gps rx ***\n");
      return (-1);
    }
  }
}


x86 assembly (clang, -O0):
<wait_for_ack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x1e(%rbp)
mov    0xe87(%rip),%ax
mov    %ax,-0x16(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2(%rax),%al
mov    %al,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x3(%rax),%al
mov    %al,-0x17(%rbp)
movb   $0x0,-0x16(%rbp)
movb   $0x0,-0x15(%rbp)
movl   $0x2,-0x3c(%rbp)
cmpl   $0x8,-0x3c(%rbp)
jge    11e6 <wait_for_ack+0x86>
movzbl -0x16(%rbp),%eax
movslq -0x3c(%rbp),%rcx
movzbl -0x1e(%rbp,%rcx,1),%ecx
add    %ecx,%eax
mov    %al,-0x16(%rbp)
movzbl -0x15(%rbp),%eax
movzbl -0x16(%rbp),%ecx
add    %ecx,%eax
mov    %al,-0x15(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11af <wait_for_ack+0x4f>
movq   $0x0,-0x28(%rbp)
movl   $0xdbba0,-0x30(%rbp)
lea    -0x34(%rbp),%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x34(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12ac <wait_for_ack+0x14c>
mov    -0x14(%rbp),%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl -0x41(%rbp),%eax
mov    -0x40(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x40(%rbp)
movslq %ecx,%rcx
movzbl -0x1e(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
je     1285 <wait_for_ack+0x125>
movl   $0x0,-0x40(%rbp)
cmpl   $0xa,-0x40(%rbp)
jne    12a7 <wait_for_ack+0x147>
lea    0xd74(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c9 <wait_for_ack+0x169>
jmp    12c4 <wait_for_ack+0x164>
lea    0xd5b(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <wait_for_ack+0x169>
jmp    11f5 <wait_for_ack+0x95>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_ack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
movw   $0x2,0xc(%rsp)
movl   $0x10562b5,0x8(%rsp)
01
mov    0x2(%rdi),%al
mov    %al,0xe(%rsp)
mov    0x3(%rdi),%cl
mov    %cl,0xf(%rsp)
add    %al,%cl
add    $0x8,%cl
add    %cl,%al
mov    %cl,0x10(%rsp)
add    $0x23,%al
mov    %al,0x11(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0xdbba0,0x18(%rsp)
lea    0x1(%rbx),%r15d
xor    %r13d,%r13d
lea    0x14(%rsp),%rbp
lea    0x18(%rsp),%r12
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1070 <select@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1241 <wait_for_ack+0xe1>
mov    %ebx,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
movzbl 0x7(%rsp),%eax
movslq %r13d,%rcx
add    $0x1,%r13d
cmp    0x8(%rsp,%rcx,1),%al
cmovne %r14d,%r13d
cmp    $0xa,%r13d
jne    11d0 <wait_for_ack+0x70>
lea    0xde6(%rip),%rdi
jmp    124e <wait_for_ack+0xee>
mov    $0xffffffff,%r14d
lea    0xdbc(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@__const.wait_for_ack.ack_ack = [10 x i8] c"\B5b\05\01\02\00\00\00\00\00"
@.str = [4 x i8] c"OK\0A\00"
@.str.1 = [28 x i8] c"*** no ACK from gps rx ***\0A\00"
define i32 @wait_for_ack(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [10 x i8]
%7 = alloca %struct.timeval
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = bitcast [10 x i8]* %6 to i8*
store i32 0, i32* %11
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 2
%16 = load i8, i8* %15
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 6
store i8 %16, i8* %17
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 3
%20 = load i8, i8* %19
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 7
store i8 %20, i8* %21
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 8
store i8 0, i8* %22
%23 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 9
store i8 0, i8* %23
store i32 2, i32* %10
br label %24
24: ; preds = %48, %2
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 8
br i1 %26, label %27, label %51
27: ; preds = %24
%28 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 8
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = add nsw i32 %30, %35
%37 = trunc i32 %36 to i8
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 8
store i8 %37, i8* %38
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 9
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 8
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = add nsw i32 %41, %44
%46 = trunc i32 %45 to i8
%47 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 9
store i8 %46, i8* %47
br label %48
48: ; preds = %27
%49 = load i32, i32* %10
%50 = add nsw i32 %49, 1
store i32 %50, i32* %10
br label %24
51: ; preds = %24
%52 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %52
%53 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 900000, i32* %53
br label %54
54: ; preds = %51, %87
%55 = call i32 @FD_ZERO(i32* %8)
%56 = call i32 @FD_SET(i32 0, i32* %8)
%57 = load i32, i32* %5
%58 = call i32 @FD_SET(i32 %57, i32* %8)
%59 = load i32, i32* %5
store i32 %59, i32* %9
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1
%62 = call i32 @select(i32 %61, i32* %8, i32* null, i32* null, %struct.timeval* %7)
store i32 %62, i32* %10
%63 = load i32, i32* %5
%64 = call i64 @FD_ISSET(i32 %63, i32* %8)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %85
66: ; preds = %54
%67 = load i32, i32* %5
%68 = call i32 @read(i32 %67, i8* %12, i32 1)
%69 = load i8, i8* %12
%70 = zext i8 %69 to i32
%71 = load i32, i32* %11
%72 = add nsw i32 %71, 1
store i32 %72, i32* %11
%73 = sext i32 %71 to i64
%74 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 %73
%75 = load i8, i8* %74
%76 = zext i8 %75 to i32
%77 = icmp ne i32 %70, %76
br i1 %77, label %78, label %79
78: ; preds = %66
store i32 0, i32* %11
br label %79
79: ; preds = %78, %66
%80 = load i32, i32* %11
%81 = icmp eq i32 %80, 10
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %88
84: ; preds = %79
br label %87
85: ; preds = %54
%86 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
87: ; preds = %84
br label %54
88: ; preds = %85, %82
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@__const.wait_for_ack.ack_ack = [10 x i8] c"\B5b\05\01\02\00\00\00\00\00"
@str = [27 x i8] c"*** no ACK from gps rx ***\00"
@str.2 = [3 x i8] c"OK\00"
define i32 @wait_for_ack(i8* nocapture %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i8
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.timeval* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds i8, i8* %0, i64 2
%11 = load i8, i8* %10
%12 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 6
store i8 %11, i8* %12
%13 = getelementptr inbounds i8, i8* %0, i64 3
%14 = load i8, i8* %13
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 7
store i8 %14, i8* %15
%16 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 8
%17 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 9
%18 = add i8 %11, 8
%19 = add i8 %11, 35
%20 = add i8 %14, %18
store i8 %20, i8* %16
%21 = add i8 %19, %20
store i8 %21, i8* %17
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 900000, i32* %23
%24 = add nsw i32 %1, 1
br label %25
25: ; preds = %33, %2
%26 = phi i32 [ 0, %2 ], [ %41, %33 ]
%27 = call i32 @FD_ZERO(i32* %5)
%28 = call i32 @FD_SET(i32 0, i32* %5)
%29 = call i32 @FD_SET(i32 %1, i32* %5)
%30 = call i32 @select(i32 %24, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%31 = call i64 @FD_ISSET(i32 %1, i32* %5)
%32 = icmp eq i64 %31, 0
br i1 %32, label %43, label %33
33: ; preds = %25
%34 = call i32 @read(i32 %1, i8* %6, i32 1)
%35 = load i8, i8* %6
%36 = add nsw i32 %26, 1
%37 = sext i32 %26 to i64
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %35, %39
%41 = select i1 %40, i32 %36, i32 0
%42 = icmp eq i32 %41, 10
br i1 %42, label %43, label %25
43: ; preds = %25, %33
%44 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @str.2, i64 0, i64 0), %33 ], [ getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0), %25 ]
%45 = phi i32 [ 0, %33 ], [ -1, %25 ]
%46 = call i32 @puts(i8* dereferenceable(1) %44)
ret i32 %45
}