#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 time_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_INFO;
int /*<<< orphan*/ LOG_WARNING;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ logIT(int /*<<< orphan*/, char *, char *);
scalar_t__ receive(int, char *, int, unsigned long *);
int /*<<< orphan*/ sprintf(char *, char *, char);
int /*<<< orphan*/ strncat(char *, char *, int);
int time(int /*<<< orphan*/ *);
int waitfor(int fd, char *w_buf, int w_len) {
  int i;
  time_t start;
  char r_buf[4096];
  char hexString[128] = "\0";
  char dummy[3];
  unsigned long etime;
  for (i = 0; i < w_len; i++) {
    sprintf(dummy, "%02X", w_buf[i]);
    strncat(hexString, dummy, 999);
  }
  logIT(LOG_INFO, "Waiting for %s", hexString);
  start = time(NULL);
  do {
    etime = 0;
    if (receive(fd, r_buf, 1, &etime) < 0)
      return (0);
    if (time(NULL) - start > 5) {
      logIT(LOG_WARNING, "%s", "Timeout wait");
      return (0);
    }
  } while (r_buf[0] != w_buf[0]);
  for (i = 1; i < w_len; i++) {
    etime = 0;
    if (receive(fd, r_buf, 1, &etime) < 0)
      return (0);
    if (time(NULL) - start > 5) {
      logIT(LOG_WARNING, "%s", "Timeout wait");
      return (0);
    }
    if (r_buf[0] != w_buf[i]) {
      logIT(LOG_ERR, "%s", "Lost synchronization");
      exit(1);
    }
  }
  return (1);
}


x86 assembly (clang, -O0):
<waitfor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10b0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x10a0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1030 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f0 <waitfor+0x80>
lea    -0x10a3(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
lea    0xe3f(%rip),%rsi
movsbl (%rax,%rcx,1),%edx
call   1070 <sprintf@plt>
lea    -0x10a0(%rbp),%rdi
lea    -0x10a3(%rbp),%rsi
mov    $0x3e7,%edx
call   1050 <strncat@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119f <waitfor+0x2f>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    -0x10a0(%rbp),%rdx
lea    0xdfe(%rip),%rsi
call   1090 <logIT@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x10b0(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1,%edx
lea    -0x10b0(%rbp),%rcx
call   1040 <receive@plt>
cmp    $0x0,%rax
jge    1254 <waitfor+0xe4>
movl   $0x0,-0x4(%rbp)
jmp    1393 <waitfor+0x223>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
sub    -0x1c(%rbp),%eax
cmp    $0x5,%eax
jle    1291 <waitfor+0x121>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
lea    0xd94(%rip),%rdx
call   1090 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1393 <waitfor+0x223>
jmp    1296 <waitfor+0x126>
movsbl -0x1020(%rbp),%eax
mov    -0x10(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1218 <waitfor+0xa8>
movl   $0x1,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    138c <waitfor+0x21c>
movq   $0x0,-0x10b0(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1,%edx
lea    -0x10b0(%rbp),%rcx
call   1040 <receive@plt>
cmp    $0x0,%rax
jge    12fb <waitfor+0x18b>
movl   $0x0,-0x4(%rbp)
jmp    1393 <waitfor+0x223>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
sub    -0x1c(%rbp),%eax
cmp    $0x5,%eax
jle    1338 <waitfor+0x1c8>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
lea    0xcf1(%rip),%rsi
lea    0xced(%rip),%rdx
call   1090 <logIT@plt>
movl   $0x0,-0x4(%rbp)
jmp    1393 <waitfor+0x223>
movsbl -0x1020(%rbp),%eax
mov    -0x10(%rbp),%rcx
movslq -0x18(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
je     1379 <waitfor+0x209>
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
lea    0xcae(%rip),%rsi
lea    0xcb7(%rip),%rdx
call   1090 <logIT@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    137e <waitfor+0x20e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12b3 <waitfor+0x143>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitfor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10a8,%rsp
mov    %rsi,%r15
mov    %edi,0x18(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
mov    %edx,0xc(%rsp)
test   %edx,%edx
jle    11f9 <waitfor+0x99>
mov    0xc(%rsp),%ebp
xor    %r12d,%r12d
lea    0xe41(%rip),%r14
lea    0x1d(%rsp),%rbx
lea    0x20(%rsp),%r13
nopl   0x0(%rax)
movsbl (%r15,%r12,1),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <sprintf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
mov    $0x3e7,%edx
call   1040 <strncat@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    11d0 <waitfor+0x70>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdfc(%rip),%rsi
lea    0x20(%rsp),%rdx
call   1080 <logIT@plt>
xor    %r12d,%r12d
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,%ebp
lea    0xa0(%rsp),%r13
lea    0x10(%rsp),%r14
mov    0x18(%rsp),%ebx
movq   $0x0,0x10(%rsp)
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <receive@plt>
test   %rax,%rax
js     1313 <waitfor+0x1b3>
xor    %edi,%edi
call   1050 <time@plt>
sub    %ebp,%eax
cmp    $0x5,%eax
jg     12f4 <waitfor+0x194>
movzbl 0xa0(%rsp),%eax
cmp    (%r15),%al
jne    1230 <waitfor+0xd0>
mov    $0x1,%r12d
mov    0xc(%rsp),%eax
cmp    $0x2,%eax
jl     1313 <waitfor+0x1b3>
mov    %eax,%r14d
mov    $0x1,%r13d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movq   $0x0,0x10(%rsp)
mov    %ebx,%edi
lea    0xa0(%rsp),%rsi
mov    $0x1,%edx
lea    0x10(%rsp),%rcx
call   1030 <receive@plt>
test   %rax,%rax
js     1313 <waitfor+0x1b3>
xor    %edi,%edi
call   1050 <time@plt>
sub    %ebp,%eax
cmp    $0x5,%eax
jg     12f4 <waitfor+0x194>
movzbl 0xa0(%rsp),%eax
cmp    (%r15,%r13,1),%al
jne    1328 <waitfor+0x1c8>
add    $0x1,%r13
cmp    %r13,%r14
jne    12a0 <waitfor+0x140>
mov    $0x1,%r12d
jmp    1313 <waitfor+0x1b3>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    0xd0d(%rip),%rsi
lea    0xd09(%rip),%rdx
call   1080 <logIT@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x10a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
lea    0xcd9(%rip),%rsi
lea    0xce2(%rip),%rdx
call   1080 <logIT@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%02X\00"
@LOG_INFO = global i32 0
@.str.1 = [15 x i8] c"Waiting for %s\00"
@LOG_WARNING = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [13 x i8] c"Timeout wait\00"
@LOG_ERR = global i32 0
@.str.4 = [21 x i8] c"Lost synchronization\00"
define i32 @waitfor(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca [128 x i8]
%12 = alloca [3 x i8]
%13 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%14 = bitcast [128 x i8]* %11 to i8*
store i32 0, i32* %8
br label %15
15: ; preds = %30, %3
%16 = load i32, i32* %8
%17 = load i32, i32* %7
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %33
19: ; preds = %15
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 0
%21 = load i8*, i8** %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %25)
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %12, i64 0, i64 0
%29 = call i32 @strncat(i8* %27, i8* %28, i32 999)
br label %30
30: ; preds = %19
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
br label %15
33: ; preds = %15
%34 = load i32, i32* @LOG_INFO
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%36 = call i32 @logIT(i32 %34, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = call i32 @time(i32* null)
store i32 %37, i32* %9
br label %38
38: ; preds = %53, %33
store i64 0, i64* %13
%39 = load i32, i32* %5
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%41 = call i64 @receive(i32 %39, i8* %40, i32 1, i64* %13)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 0, i32* %4
br label %101
44: ; preds = %38
%45 = call i32 @time(i32* null)
%46 = load i32, i32* %9
%47 = sub nsw i32 %45, %46
%48 = icmp sgt i32 %47, 5
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = load i32, i32* @LOG_WARNING
%51 = call i32 @logIT(i32 %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %101
52: ; preds = %44
br label %53
53: ; preds = %52
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = load i8*, i8** %6
%58 = getelementptr inbounds i8, i8* %57, i64 0
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = icmp ne i32 %56, %60
br i1 %61, label %38, label %62
62: ; preds = %53
store i32 1, i32* %8
br label %63
63: ; preds = %97, %62
%64 = load i32, i32* %8
%65 = load i32, i32* %7
%66 = icmp slt i32 %64, %65
br i1 %66, label %67, label %100
67: ; preds = %63
store i64 0, i64* %13
%68 = load i32, i32* %5
%69 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%70 = call i64 @receive(i32 %68, i8* %69, i32 1, i64* %13)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %73
72: ; preds = %67
store i32 0, i32* %4
br label %101
73: ; preds = %67
%74 = call i32 @time(i32* null)
%75 = load i32, i32* %9
%76 = sub nsw i32 %74, %75
%77 = icmp sgt i32 %76, 5
br i1 %77, label %78, label %81
78: ; preds = %73
%79 = load i32, i32* @LOG_WARNING
%80 = call i32 @logIT(i32 %79, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %4
br label %101
81: ; preds = %73
%82 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%83 = load i8, i8* %82
%84 = sext i8 %83 to i32
%85 = load i8*, i8** %6
%86 = load i32, i32* %8
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
%89 = load i8, i8* %88
%90 = sext i8 %89 to i32
%91 = icmp ne i32 %84, %90
br i1 %91, label %92, label %96
92: ; preds = %81
%93 = load i32, i32* @LOG_ERR
%94 = call i32 @logIT(i32 %93, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%95 = call i32 @exit(i32 1)
unreachable
96: ; preds = %81
br label %97
97: ; preds = %96
%98 = load i32, i32* %8
%99 = add nsw i32 %98, 1
store i32 %99, i32* %8
br label %63
100: ; preds = %63
store i32 1, i32* %4
br label %101
101: ; preds = %100, %78, %72, %49, %43
%102 = load i32, i32* %4
ret i32 %102
}

LLVM IR (-O2):
@.str = [5 x i8] c"%02X\00"
@LOG_INFO = global i32 0
@.str.1 = [15 x i8] c"Waiting for %s\00"
@LOG_WARNING = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [13 x i8] c"Timeout wait\00"
@LOG_ERR = global i32 0
@.str.4 = [21 x i8] c"Lost synchronization\00"
define i32 @waitfor(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = alloca [128 x i8]
%6 = alloca [3 x i8]
%7 = alloca i64
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
%11 = bitcast i64* %7 to i8*
%12 = icmp sgt i32 %2, 0
br i1 %12, label %13, label %23
13: ; preds = %3
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %15
%16 = phi i64 [ 0, %13 ], [ %21, %15 ]
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %18)
%20 = call i32 @strncat(i8* %9, i8* %10, i32 999)
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, %14
br i1 %22, label %23, label %15
23: ; preds = %15, %3
%24 = load i32, i32* @LOG_INFO
%25 = call i32 @logIT(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%26 = call i32 @time(i32* null)
br label %27
27: ; preds = %34, %23
store i64 0, i64* %7
%28 = call i64 @receive(i32 %0, i8* %8, i32 1, i64* %7)
%29 = icmp slt i64 %28, 0
br i1 %29, label %65, label %30
30: ; preds = %27
%31 = call i32 @time(i32* null)
%32 = sub nsw i32 %31, %26
%33 = icmp sgt i32 %32, 5
br i1 %33, label %62, label %34
34: ; preds = %30
%35 = load i8, i8* %8
%36 = load i8, i8* %1
%37 = icmp eq i8 %35, %36
br i1 %37, label %38, label %27
38: ; preds = %34
%39 = icmp sgt i32 %2, 1
br i1 %39, label %40, label %65
40: ; preds = %38
%41 = zext i32 %2 to i64
br label %44
42: ; preds = %52
%43 = icmp eq i64 %57, %41
br i1 %43, label %65, label %44
44: ; preds = %40, %42
%45 = phi i64 [ 1, %40 ], [ %57, %42 ]
store i64 0, i64* %7
%46 = call i64 @receive(i32 %0, i8* %8, i32 1, i64* %7)
%47 = icmp slt i64 %46, 0
br i1 %47, label %65, label %48
48: ; preds = %44
%49 = call i32 @time(i32* null)
%50 = sub nsw i32 %49, %26
%51 = icmp sgt i32 %50, 5
br i1 %51, label %62, label %52
52: ; preds = %48
%53 = load i8, i8* %8
%54 = getelementptr inbounds i8, i8* %1, i64 %45
%55 = load i8, i8* %54
%56 = icmp eq i8 %53, %55
%57 = add nuw nsw i64 %45, 1
br i1 %56, label %42, label %58
58: ; preds = %52
%59 = load i32, i32* @LOG_ERR
%60 = call i32 @logIT(i32 %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %30, %48
%63 = load i32, i32* @LOG_WARNING
%64 = call i32 @logIT(i32 %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %27, %44, %42, %62, %38
%66 = phi i32 [ 1, %38 ], [ 0, %62 ], [ 0, %44 ], [ 1, %42 ], [ 0, %27 ]
ret i32 %66
}