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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
size_t read(int, int *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
u_int8_t cfg_ack_check(int fd) {
  int rev;
  fd_set rfds;
  struct timeval tv;
  tv.tv_sec = 3;
  tv.tv_usec = 0;
  u_int16_t len = 0, i;
  u_int8_t rval = 1, buf[1000];
  while (1) {
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    rev = select(fd + 1, &rfds, NULL, NULL, &tv);
    if (rev == -1) {
      perror("select");
      break;
    } else if (rev) {
      len = read(fd, buf, sizeof(buf));
      for (i = 0; i < len; i++) {
        if (buf[i] == 0xB5 && buf[i + 1] == 0x62) {
          printf("Found header B5 62\n");
          break;
        }
      }
      if (i == len) {
        rval = 3;
      } else {
        if (buf[i + 2] == 0x05 && buf[i + 3] == 0x00) {
          printf("Received NAK, failed.\n");
          rval = 2;
          break;
        } else {
          printf("Received ACK, succeeded.\n");
          rval = 1;
          break;
        }
      }
    } else {
      printf("No ACK/NAK received.\n");
      rval = 3;
      break;
    }
  }
  return rval;
}


x86 assembly (clang, -O0):
<cfg_ack_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0xfe0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x3,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x1,-0x34(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d9 <cfg_ack_check+0x79>
lea    0xe31(%rip),%rdi
call   1070 <perror@plt>
jmp    12f4 <cfg_ack_check+0x194>
cmpl   $0x0,-0x8(%rbp)
je     12d2 <cfg_ack_check+0x172>
mov    -0x4(%rbp),%edi
lea    -0xfe0(%rbp),%rsi
mov    $0xfa0,%edx
call   1040 <read@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    125f <cfg_ack_check+0xff>
mov    -0x30(%rbp),%rax
cmpl   $0xb5,-0xfe0(%rbp,%rax,4)
jne    1249 <cfg_ack_check+0xe9>
mov    -0x30(%rbp),%rax
cmpl   $0x62,-0xfdc(%rbp,%rax,4)
62
jne    1249 <cfg_ack_check+0xe9>
lea    0xdc8(%rip),%rdi
call   1030 <printf@plt>
jmp    125f <cfg_ack_check+0xff>
jmp    124e <cfg_ack_check+0xee>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1203 <cfg_ack_check+0xa3>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1279 <cfg_ack_check+0x119>
movl   $0x3,-0x34(%rbp)
jmp    12cd <cfg_ack_check+0x16d>
mov    -0x30(%rbp),%rax
cmpl   $0x5,-0xfd8(%rbp,%rax,4)
05
jne    12b5 <cfg_ack_check+0x155>
mov    -0x30(%rbp),%rax
cmpl   $0x0,-0xfd4(%rbp,%rax,4)
jne    12b5 <cfg_ack_check+0x155>
lea    0xd77(%rip),%rdi
call   1030 <printf@plt>
movl   $0x2,-0x34(%rbp)
jmp    12f4 <cfg_ack_check+0x194>
lea    0xd76(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x34(%rbp)
jmp    12f4 <cfg_ack_check+0x194>
jmp    12ea <cfg_ack_check+0x18a>
lea    0xd73(%rip),%rdi
call   1030 <printf@plt>
movl   $0x3,-0x34(%rbp)
jmp    12f4 <cfg_ack_check+0x194>
jmp    12ef <cfg_ack_check+0x18f>
jmp    118c <cfg_ack_check+0x2c>
mov    -0x34(%rbp),%eax
add    $0xfe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cfg_ack_check>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xfc8,%rsp
mov    %edi,%ebx
movl   $0x3,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x1(%rbx),%r14d
mov    $0x1,%r13d
lea    0xc(%rsp),%rbp
jmp    11b2 <cfg_ack_check+0x52>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%r12
mov    $0x3,%r13d
cmp    %r15,%r12
jne    1251 <cfg_ack_check+0xf1>
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1060 <select@plt>
test   %eax,%eax
je     123d <cfg_ack_check+0xdd>
cmp    $0xffffffff,%eax
je     129c <cfg_ack_check+0x13c>
mov    %ebx,%edi
lea    0x20(%rsp),%rsi
mov    $0xfa0,%edx
call   1040 <read@plt>
mov    %rax,%r15
xor    %r12d,%r12d
test   %rax,%rax
jne    1219 <cfg_ack_check+0xb9>
jmp    11a3 <cfg_ack_check+0x43>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r12
cmp    %r12,%r15
je     11a0 <cfg_ack_check+0x40>
cmpl   $0xb5,0x20(%rsp,%r12,4)
jne    1210 <cfg_ack_check+0xb0>
cmpl   $0x62,0x24(%rsp,%r12,4)
jne    1210 <cfg_ack_check+0xb0>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
jmp    11a3 <cfg_ack_check+0x43>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%r13d
jmp    1273 <cfg_ack_check+0x113>
cmpl   $0x5,0x28(%rsp,%r12,4)
jne    1261 <cfg_ack_check+0x101>
cmpl   $0x0,0x2c(%rsp,%r12,4)
je     1288 <cfg_ack_check+0x128>
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0xfc8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%r13d
jmp    1273 <cfg_ack_check+0x113>
lea    0xd5d(%rip),%rdi
call   1070 <perror@plt>
jmp    1273 <cfg_ack_check+0x113>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"select\00"
@.str.1 = [20 x i8] c"Found header B5 62\0A\00"
@.str.2 = [23 x i8] c"Received NAK, failed.\0A\00"
@.str.3 = [26 x i8] c"Received ACK, succeeded.\0A\00"
@.str.4 = [22 x i8] c"No ACK/NAK received.\0A\00"
define i32 @cfg_ack_check(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca [1000 x i32]
store i32 %0, i32* %2
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 3, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %11
store i64 0, i64* %6
store i32 1, i32* %8
br label %12
12: ; preds = %1, %76
%13 = call i32 @FD_ZERO(i32* %4)
%14 = load i32, i32* %2
%15 = call i32 @FD_SET(i32 %14, i32* %4)
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
%18 = call i32 @select(i32 %17, i32* %4, i32* null, i32* null, %struct.timeval* %5)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %77
23: ; preds = %12
%24 = load i32, i32* %3
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %73
26: ; preds = %23
%27 = load i32, i32* %2
%28 = getelementptr inbounds [1000 x i32], [1000 x i32]* %9, i64 0, i64 0
%29 = call i64 @read(i32 %27, i32* %28, i32 4000)
store i64 %29, i64* %6
store i64 0, i64* %7
br label %30
30: ; preds = %48, %26
%31 = load i64, i64* %7
%32 = load i64, i64* %6
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %51
34: ; preds = %30
%35 = load i64, i64* %7
%36 = getelementptr inbounds [1000 x i32], [1000 x i32]* %9, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, 181
br i1 %38, label %39, label %47
39: ; preds = %34
%40 = load i64, i64* %7
%41 = add i64 %40, 1
%42 = getelementptr inbounds [1000 x i32], [1000 x i32]* %9, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = icmp eq i32 %43, 98
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %51
47: ; preds = %39, %34
br label %48
48: ; preds = %47
%49 = load i64, i64* %7
%50 = add i64 %49, 1
store i64 %50, i64* %7
br label %30
51: ; preds = %45, %30
%52 = load i64, i64* %7
%53 = load i64, i64* %6
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %56
55: ; preds = %51
store i32 3, i32* %8
br label %72
56: ; preds = %51
%57 = load i64, i64* %7
%58 = add i64 %57, 2
%59 = getelementptr inbounds [1000 x i32], [1000 x i32]* %9, i64 0, i64 %58
%60 = load i32, i32* %59
%61 = icmp eq i32 %60, 5
br i1 %61, label %62, label %70
62: ; preds = %56
%63 = load i64, i64* %7
%64 = add i64 %63, 3
%65 = getelementptr inbounds [1000 x i32], [1000 x i32]* %9, i64 0, i64 %64
%66 = load i32, i32* %65
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %8
br label %77
70: ; preds = %62, %56
%71 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %8
br label %77
72: ; preds = %55
br label %75
73: ; preds = %23
%74 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i32 3, i32* %8
br label %77
75: ; preds = %72
br label %76
76: ; preds = %75
br label %12
77: ; preds = %73, %70, %68, %21
%78 = load i32, i32* %8
ret i32 %78
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"select\00"
@str = [21 x i8] c"No ACK/NAK received.\00"
@str.5 = [25 x i8] c"Received ACK, succeeded.\00"
@str.6 = [22 x i8] c"Received NAK, failed.\00"
@str.7 = [19 x i8] c"Found header B5 62\00"
define i32 @cfg_ack_check(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca [1000 x i32]
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 3, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %8
%9 = bitcast [1000 x i32]* %4 to i8*
%10 = add nsw i32 %0, 1
%11 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 0
br label %12
12: ; preds = %36, %1
%13 = phi i32 [ 1, %1 ], [ 3, %36 ]
%14 = call i32 @FD_ZERO(i32* %2)
%15 = call i32 @FD_SET(i32 %0, i32* %2)
%16 = call i32 @select(i32 %10, i32* %2, i32* null, i32* null, %struct.timeval* %3)
switch i32 %16, label %19 [
i32 -1, label %17
i32 0, label %53
]
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %55
19: ; preds = %12
%20 = call i64 @read(i32 %0, i32* %11, i32 4000)
%21 = icmp eq i64 %20, 0
br i1 %21, label %36, label %22
22: ; preds = %19, %34
%23 = phi i64 [ %27, %34 ], [ 0, %19 ]
%24 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = icmp eq i32 %25, 181
%27 = add nuw i64 %23, 1
br i1 %26, label %28, label %34
28: ; preds = %22
%29 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 %27
%30 = load i32, i32* %29
%31 = icmp eq i32 %30, 98
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
br label %36
34: ; preds = %22, %28
%35 = icmp eq i64 %27, %20
br i1 %35, label %36, label %22
36: ; preds = %34, %19, %32
%37 = phi i64 [ %23, %32 ], [ 0, %19 ], [ %20, %34 ]
%38 = icmp eq i64 %37, %20
br i1 %38, label %12, label %39
39: ; preds = %36
%40 = add i64 %37, 2
%41 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 %40
%42 = load i32, i32* %41
%43 = icmp eq i32 %42, 5
br i1 %43, label %44, label %51
44: ; preds = %39
%45 = add i64 %37, 3
%46 = getelementptr inbounds [1000 x i32], [1000 x i32]* %4, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
br label %55
51: ; preds = %44, %39
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.5, i64 0, i64 0))
br label %55
53: ; preds = %12
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %55
55: ; preds = %53, %51, %49, %17
%56 = phi i32 [ %13, %17 ], [ 2, %49 ], [ 1, %51 ], [ 3, %53 ]
ret i32 %56
}