#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 */

/* Variables and functions */
char read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int dev_sync(int serport) {
  int ntimer;
  char nread;
  char csync = 'U';
  ntimer = 0;
  nread = 0;
  do {
    if (ntimer % 10 == 0)
      write(serport, &csync, 1);
    usleep(100000);
    nread = read(serport, &csync, 1);
    ntimer++;
  } while (nread < 1 && ntimer < 50);
  if (nread < 1)
    return -1;
  else if (csync != 'U')
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<dev_sync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x55,-0xe(%rbp)
movl   $0x0,-0xc(%rbp)
movb   $0x0,-0xd(%rbp)
mov    -0xc(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    116f <dev_sync+0x3f>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %al,-0xd(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movsbl -0xd(%rbp),%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0xf(%rbp)
jge    11b2 <dev_sync+0x82>
cmpl   $0x32,-0xc(%rbp)
setl   %al
mov    %al,-0xf(%rbp)
mov    -0xf(%rbp),%al
test   $0x1,%al
jne    114a <dev_sync+0x1a>
movsbl -0xd(%rbp),%eax
cmp    $0x1,%eax
jge    11d6 <dev_sync+0xa6>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f6 <dev_sync+0xc6>
movsbl -0xe(%rbp),%eax
cmp    $0x55,%eax
je     11ef <dev_sync+0xbf>
movl   $0x1,-0x4(%rbp)
jmp    11f6 <dev_sync+0xc6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dev_sync>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
movb   $0x55,0x7(%rsp)
xor    %ebp,%ebp
lea    0x7(%rsp),%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movzbl %bl,%eax
imul   $0xcd,%eax,%eax
shr    $0xa,%eax
and    $0xfffffffe,%eax
lea    (%rax,%rax,4),%eax
add    %bpl,%al
jne    1177 <dev_sync+0x47>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %al,%al
jg     11a3 <dev_sync+0x73>
lea    0x1(%rbx),%ecx
add    $0xff,%bpl
cmp    $0x31,%ebx
mov    %ecx,%ebx
jb     1150 <dev_sync+0x20>
xor    %ecx,%ecx
cmpb   $0x55,0x7(%rsp)
setne  %cl
test   %al,%al
mov    $0xffffffff,%eax
cmovg  %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @dev_sync(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8
store i32 %0, i32* %3
store i8 85, i8* %6
store i32 0, i32* %4
store i8 0, i8* %5
br label %7
7: ; preds = %27, %1
%8 = load i32, i32* %4
%9 = srem i32 %8, 10
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i32, i32* %3
%13 = call i32 @write(i32 %12, i8* %6, i32 1)
br label %14
14: ; preds = %11, %7
%15 = call i32 @usleep(i32 100000)
%16 = load i32, i32* %3
%17 = call signext i8 @read(i32 %16, i8* %6, i32 1)
store i8 %17, i8* %5
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
br label %20
20: ; preds = %14
%21 = load i8, i8* %5
%22 = sext i8 %21 to i32
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 50
br label %27
27: ; preds = %24, %20
%28 = phi i1 [ false, %20 ], [ %26, %24 ]
br i1 %28, label %7, label %29
29: ; preds = %27
%30 = load i8, i8* %5
%31 = sext i8 %30 to i32
%32 = icmp slt i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 -1, i32* %2
br label %40
34: ; preds = %29
%35 = load i8, i8* %6
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 85
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 1, i32* %2
br label %40
39: ; preds = %34
store i32 0, i32* %2
br label %40
40: ; preds = %39, %38, %33
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
define i32 @dev_sync(i32 %0) {
%2 = alloca i8
store i8 85, i8* %2
br label %3
3: ; preds = %10, %1
%4 = phi i32 [ 0, %1 ], [ %13, %10 ]
%5 = trunc i32 %4 to i8
%6 = urem i8 %5, 10
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = call i32 @write(i32 %0, i8* %2, i32 1)
br label %10
10: ; preds = %8, %3
%11 = tail call i32 @usleep(i32 100000)
%12 = call signext i8 @read(i32 %0, i8* %2, i32 1)
%13 = add nuw nsw i32 %4, 1
%14 = icmp slt i8 %12, 1
%15 = icmp ult i32 %4, 49
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %3, label %17
17: ; preds = %10
%18 = load i8, i8* %2
%19 = icmp ne i8 %18, 85
%20 = zext i1 %19 to i32
%21 = select i1 %14, i32 -1, i32 %20
ret i32 %21
}