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

/* Variables and functions */
int bcm4751_serial_read(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                        struct timeval *);
int /*<<< orphan*/ bcm4751_serial_write(int, int /*<<< orphan*/ *, int,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, int, int);
int bcm4751_autobaud(int fd) {
  struct timeval timeout;
  uint8_t autobaud[20];
  int ready = 0;
  int rc = -1;
  memset(autobaud, 0x80, sizeof(autobaud));
  timeout.tv_sec = 1;
  timeout.tv_usec = 0;
  while (!ready) {
    bcm4751_serial_write(fd, autobaud, sizeof(autobaud), NULL);
    rc = bcm4751_serial_read(fd, NULL, 0, &timeout);
    if (rc > 0)
      ready = 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<bcm4751_autobaud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x74(%rbp)
movl   $0xffffffff,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
mov    $0x80,%esi
mov    $0x50,%edx
call   1030 <memset@plt>
movl   $0x1,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
cmpl   $0x0,-0x74(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1184 <bcm4751_autobaud+0x54>
jmp    11c4 <bcm4751_autobaud+0x94>
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <bcm4751_serial_write@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
lea    -0x18(%rbp),%rcx
call   1050 <bcm4751_serial_read@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jle    11bf <bcm4751_autobaud+0x8f>
movl   $0x1,-0x74(%rbp)
jmp    116e <bcm4751_autobaud+0x3e>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcm4751_autobaud>:
push   %r15
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x80,%esi
mov    $0x50,%edx
call   1030 <memset@plt>
movl   $0x1,(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x50,%edx
xor    %ecx,%ecx
call   1040 <bcm4751_serial_write@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
mov    %r15,%rcx
call   1050 <bcm4751_serial_read@plt>
test   %eax,%eax
jle    1170 <bcm4751_autobaud+0x40>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
define i32 @bcm4751_autobaud(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca [20 x i32]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
store i32 -1, i32* %6
%7 = getelementptr inbounds [20 x i32], [20 x i32]* %4, i64 0, i64 0
%8 = call i32 @memset(i32* %7, i32 128, i32 80)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %10
br label %11
11: ; preds = %24, %1
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
%14 = xor i1 %13, true
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i32, i32* %2
%17 = getelementptr inbounds [20 x i32], [20 x i32]* %4, i64 0, i64 0
%18 = call i32 @bcm4751_serial_write(i32 %16, i32* %17, i32 80, i32* null)
%19 = load i32, i32* %2
%20 = call i32 @bcm4751_serial_read(i32 %19, i32* null, i32 0, %struct.timeval* %3)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %15
store i32 1, i32* %5
br label %24
24: ; preds = %23, %15
br label %11
25: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @bcm4751_autobaud(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca [20 x i32]
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast [20 x i32]* %3 to i8*
%6 = getelementptr inbounds [20 x i32], [20 x i32]* %3, i64 0, i64 0
%7 = call i32 @memset(i32* %6, i32 128, i32 80)
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 1, i32* %8
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %9
br label %10
10: ; preds = %1, %10
%11 = call i32 @bcm4751_serial_write(i32 %0, i32* %6, i32 80, i32* null)
%12 = call i32 @bcm4751_serial_read(i32 %0, i32* null, i32 0, %struct.timeval* %2)
%13 = icmp slt i32 %12, 1
br i1 %13, label %10, label %14
14: ; preds = %10
ret i32 0
}