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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int write(int, void *, int);
int bcm4751_serial_write(int fd, void *data, int length,
                         struct timeval *timeout) {
  fd_set fds;
  int rc = -1;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  rc = select(fd + 1, NULL, &fds, NULL, timeout);
  if (rc > 0 && length > 0) {
    rc = write(fd, data, length);
  }
  return rc;
}


x86 assembly (clang, -O0):
<bcm4751_serial_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x28(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x20(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x24(%rbp),%rdx
mov    %rcx,%rsi
call   1050 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    11b5 <bcm4751_serial_write+0x75>
cmpl   $0x0,-0x14(%rbp)
jle    11b5 <bcm4751_serial_write+0x75>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bcm4751_serial_write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r12
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
test   %r15d,%r15d
jle    1196 <bcm4751_serial_write+0x56>
test   %eax,%eax
jle    1196 <bcm4751_serial_write+0x56>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define i32 @bcm4751_serial_write(i32 %0, i8* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.timeval*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store %struct.timeval* %3, %struct.timeval** %8
store i32 -1, i32* %10
%11 = call i32 @FD_ZERO(i32* %9)
%12 = load i32, i32* %5
%13 = call i32 @FD_SET(i32 %12, i32* %9)
%14 = load i32, i32* %5
%15 = add nsw i32 %14, 1
%16 = load %struct.timeval*, %struct.timeval** %8
%17 = call i32 @select(i32 %15, i32* null, i32* %9, i32* null, %struct.timeval* %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %4
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %10
br label %28
28: ; preds = %23, %20, %4
%29 = load i32, i32* %10
ret i32 %29
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i32 @bcm4751_serial_write(i32 %0, i8* nocapture %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @FD_ZERO(i32* %5)
%8 = call i32 @FD_SET(i32 %0, i32* %5)
%9 = add nsw i32 %0, 1
%10 = call i32 @select(i32 %9, i32* null, i32* %5, i32* null, %struct.timeval* %3)
%11 = icmp sgt i32 %10, 0
%12 = icmp sgt i32 %2, 0
%13 = and i1 %12, %11
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = call i32 @write(i32 %0, i8* %1, i32 %2)
br label %16
16: ; preds = %14, %4
%17 = phi i32 [ %15, %14 ], [ %10, %4 ]
ret i32 %17
}