#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_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
void synchronise(int serialfd, unsigned char frame_header, int frame_size) {
  struct timeval timeout;
  unsigned char data = 0, attempt = 0;
  fd_set rfds;
  int read_count;
  int i;
  do {
    attempt++;
    printf("Connection attempt %d\n", attempt);
    timeout.tv_sec = 2;
    timeout.tv_usec = 0;
    FD_ZERO(&rfds);
    FD_SET(serialfd, &rfds);
    select(serialfd + 1, &rfds, NULL, NULL, &timeout);
    read_count = read(serialfd, &data, 1);
  } while (attempt < 10 && (read_count <= 0 || data != frame_header));
  if (attempt >= 10)
    printf("Not Responding\n");
  else if (data == frame_header)
    printf("Synchronised\n");
  else
    printf("Unexpected response\n");
  for (i = 0; i < (frame_size - 1); i++)
    while (read(serialfd, &data, 1) != 1)
      ;
}


x86 assembly (clang, -O0):
<synchronise>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %edx,-0xc(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x0,-0x22(%rbp)
mov    -0x22(%rbp),%al
add    $0x1,%al
mov    %al,-0x22(%rbp)
movzbl -0x22(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
movzbl -0x22(%rbp),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x31(%rbp)
jge    120f <synchronise+0xbf>
mov    $0x1,%al
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x32(%rbp)
jle    1209 <synchronise+0xb9>
movzbl -0x21(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
mov    %al,-0x32(%rbp)
mov    -0x32(%rbp),%al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    116c <synchronise+0x1c>
movzbl -0x22(%rbp),%eax
cmp    $0xa,%eax
jl     123a <synchronise+0xea>
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1270 <synchronise+0x120>
movzbl -0x21(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
jne    125d <synchronise+0x10d>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    126b <synchronise+0x11b>
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1270 <synchronise+0x120>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12bf <synchronise+0x16f>
jmp    128d <synchronise+0x13d>
mov    -0x4(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     12ac <synchronise+0x15c>
jmp    128d <synchronise+0x13d>
jmp    12b1 <synchronise+0x161>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1277 <synchronise+0x127>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<synchronise>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %esi,0x10(%rsp)
mov    %edi,%ebx
movb   $0x0,0xf(%rsp)
lea    0x1(%rbx),%r13d
mov    $0x1,%ebp
lea    0x14(%rsp),%r14
lea    0xf(%rsp),%r12
nop
lea    0xe69(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x2,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
mov    %r13d,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1070 <select@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0xa,%ebp
je     1205 <synchronise+0xa5>
add    $0x1,%ebp
test   %eax,%eax
jle    1190 <synchronise+0x30>
mov    0x10(%rsp),%eax
cmp    %al,0xf(%rsp)
jne    1190 <synchronise+0x30>
lea    0xe14(%rip),%rdi
jmp    120c <synchronise+0xac>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x2,%r15d
jl     124d <synchronise+0xed>
add    $0xffffffff,%r15d
xor    %r14d,%r14d
lea    0xf(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x1,%eax
jne    1230 <synchronise+0xd0>
add    $0x1,%r14d
cmp    %r15d,%r14d
jne    1230 <synchronise+0xd0>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [23 x i8] c"Connection attempt %d\0A\00"
@.str.1 = [16 x i8] c"Not Responding\0A\00"
@.str.2 = [14 x i8] c"Synchronised\0A\00"
@.str.3 = [21 x i8] c"Unexpected response\0A\00"
define void @synchronise(i32 %0, i8 zeroext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8 %1, i8* %5
store i32 %2, i32* %6
store i8 0, i8* %8
store i8 0, i8* %9
br label %13
13: ; preds = %44, %3
%14 = load i8, i8* %9
%15 = add i8 %14, 1
store i8 %15, i8* %9
%16 = load i8, i8* %9
%17 = zext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
store i32 2, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
store i64 0, i64* %20
%21 = call i32 @FD_ZERO(i32* %10)
%22 = load i32, i32* %4
%23 = call i32 @FD_SET(i32 %22, i32* %10)
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
%26 = call i32 @select(i32 %25, i32* %10, i32* null, i32* null, %struct.timeval* %7)
%27 = load i32, i32* %4
%28 = call i32 @read(i32 %27, i8* %8, i32 1)
store i32 %28, i32* %11
br label %29
29: ; preds = %13
%30 = load i8, i8* %9
%31 = zext i8 %30 to i32
%32 = icmp slt i32 %31, 10
br i1 %32, label %33, label %44
33: ; preds = %29
%34 = load i32, i32* %11
%35 = icmp sle i32 %34, 0
br i1 %35, label %42, label %36
36: ; preds = %33
%37 = load i8, i8* %8
%38 = zext i8 %37 to i32
%39 = load i8, i8* %5
%40 = zext i8 %39 to i32
%41 = icmp ne i32 %38, %40
br label %42
42: ; preds = %36, %33
%43 = phi i1 [ true, %33 ], [ %41, %36 ]
br label %44
44: ; preds = %42, %29
%45 = phi i1 [ false, %29 ], [ %43, %42 ]
br i1 %45, label %13, label %46
46: ; preds = %44
%47 = load i8, i8* %9
%48 = zext i8 %47 to i32
%49 = icmp sge i32 %48, 10
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %63
52: ; preds = %46
%53 = load i8, i8* %8
%54 = zext i8 %53 to i32
%55 = load i8, i8* %5
%56 = zext i8 %55 to i32
%57 = icmp eq i32 %54, %56
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %62
60: ; preds = %52
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %58
br label %63
63: ; preds = %62, %50
store i32 0, i32* %12
br label %64
64: ; preds = %76, %63
%65 = load i32, i32* %12
%66 = load i32, i32* %6
%67 = sub nsw i32 %66, 1
%68 = icmp slt i32 %65, %67
br i1 %68, label %69, label %79
69: ; preds = %64
br label %70
70: ; preds = %74, %69
%71 = load i32, i32* %4
%72 = call i32 @read(i32 %71, i8* %8, i32 1)
%73 = icmp ne i32 %72, 1
br i1 %73, label %74, label %75
74: ; preds = %70
br label %70
75: ; preds = %70
br label %76
76: ; preds = %75
%77 = load i32, i32* %12
%78 = add nsw i32 %77, 1
store i32 %78, i32* %12
br label %64
79: ; preds = %64
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [23 x i8] c"Connection attempt %d\0A\00"
@str.4 = [13 x i8] c"Synchronised\00"
@str.5 = [15 x i8] c"Not Responding\00"
define void @synchronise(i32 %0, i8 zeroext %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i8
%6 = alloca i32
%7 = bitcast %struct.timeval* %4 to i8*
store i8 0, i8* %5
%8 = bitcast i32* %6 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
%11 = add nsw i32 %0, 1
br label %12
12: ; preds = %21, %3
%13 = phi i32 [ %14, %21 ], [ 0, %3 ]
%14 = add nuw nsw i32 %13, 1
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 2, i32* %9
store i64 0, i64* %10
%16 = call i32 @FD_ZERO(i32* %6)
%17 = call i32 @FD_SET(i32 %0, i32* %6)
%18 = call i32 @select(i32 %11, i32* %6, i32* null, i32* null, %struct.timeval* %4)
%19 = call i32 @read(i32 %0, i8* %5, i32 1)
%20 = icmp eq i32 %13, 9
br i1 %20, label %26, label %21
21: ; preds = %12
%22 = icmp slt i32 %19, 1
%23 = load i8, i8* %5
%24 = icmp ne i8 %23, %1
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %12, label %26
26: ; preds = %21, %12
%27 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0), %12 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.4, i64 0, i64 0), %21 ]
%28 = call i32 @puts(i8* dereferenceable(1) %27)
%29 = add i32 %2, -1
%30 = icmp sgt i32 %2, 1
br i1 %30, label %31, label %39
31: ; preds = %26, %36
%32 = phi i32 [ %37, %36 ], [ 0, %26 ]
br label %33
33: ; preds = %31, %33
%34 = call i32 @read(i32 %0, i8* %5, i32 1)
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %33
36: ; preds = %33
%37 = add nuw nsw i32 %32, 1
%38 = icmp eq i32 %37, %29
br i1 %38, label %39, label %31
39: ; preds = %36, %26
ret void
}