#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_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int write(int, char *, int);
int _writen(register int fd, register char *ptr, register int nbytes,
            void (*timeout_function)(), int doselect) {
  int nleft, nwritten;
  int r;
  fd_set set;
  struct timeval ltspfs_timeout;
  struct timeval *timeout_ptr = &ltspfs_timeout;
  FD_ZERO(&set);
  FD_SET(fd, &set);
  if (!doselect)
    timeout_ptr = NULL;
  nleft = nbytes;
  while (nleft > 0) {
    ltspfs_timeout.tv_sec = 30;
    ltspfs_timeout.tv_usec = 0;
    r = select(FD_SETSIZE, NULL, &set, NULL, timeout_ptr);
    if (r < 0)
      return r;
    else if (r == 0)
      timeout_function();
    else {
      nwritten = write(fd, ptr, nleft);
      if (nwritten <= 0)
        return (nwritten);
      nleft -= nwritten;
      ptr += nwritten;
    }
  }
  return (nbytes - nleft);
}


x86 assembly (clang, -O0):
<_writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
lea    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x34(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1189 <_writen+0x49>
movq   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    123e <_writen+0xfe>
movl   $0x1e,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%r8
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x34(%rbp),%rdx
mov    %rcx,%rsi
call   1050 <select@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11dd <_writen+0x9d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <_writen+0x107>
cmpl   $0x0,-0x30(%rbp)
jne    11f1 <_writen+0xb1>
mov    $0x0,%al
call   *-0x20(%rbp)
jmp    1234 <_writen+0xf4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     1218 <_writen+0xd8>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <_writen+0x107>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1239 <_writen+0xf9>
jmp    118f <_writen+0x4f>
mov    -0x14(%rbp),%eax
sub    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_writen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%ebp
mov    %rcx,0x10(%rsp)
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
xor    %eax,%eax
test   %ebp,%ebp
lea    0x18(%rsp),%r13
cmove  %rax,%r13
mov    %r15d,%ebp
test   %r15d,%r15d
jle    11f5 <_writen+0xb5>
lea    0xc(%rsp),%r14
mov    %r15d,%ebp
jmp    11aa <_writen+0x6a>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   *0x10(%rsp)
test   %ebp,%ebp
jle    11f5 <_writen+0xb5>
movl   $0x1e,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    %r13,%r8
call   1050 <select@plt>
test   %eax,%eax
js     11fa <_writen+0xba>
je     11a0 <_writen+0x60>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11fa <_writen+0xba>
sub    %eax,%ebp
mov    %eax,%eax
add    %rax,%rbx
test   %ebp,%ebp
jg     11aa <_writen+0x6a>
sub    %ebp,%r15d
jmp    11fd <_writen+0xbd>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define i32 @_writen(i32 %0, i8* %1, i32 %2, void (...)* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca void (...)*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
%17 = alloca %struct.timeval*
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store void (...)* %3, void (...)** %10
store i32 %4, i32* %11
store %struct.timeval* %16, %struct.timeval** %17
%18 = call i32 @FD_ZERO(i32* %15)
%19 = load i32, i32* %7
%20 = call i32 @FD_SET(i32 %19, i32* %15)
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %5
store %struct.timeval* null, %struct.timeval** %17
br label %24
24: ; preds = %23, %5
%25 = load i32, i32* %9
store i32 %25, i32* %12
br label %26
26: ; preds = %62, %24
%27 = load i32, i32* %12
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %63
29: ; preds = %26
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 30, i32* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 0, i64* %31
%32 = load i32, i32* @FD_SETSIZE
%33 = load %struct.timeval*, %struct.timeval** %17
%34 = call i32 @select(i32 %32, i32* null, i32* %15, i32* null, %struct.timeval* %33)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = load i32, i32* %14
store i32 %38, i32* %6
br label %67
39: ; preds = %29
%40 = load i32, i32* %14
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = load void (...)*, void (...)** %10
br label %61
44: ; preds = %39
%45 = load i32, i32* %7
%46 = load i8*, i8** %8
%47 = load i32, i32* %12
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
store i32 %48, i32* %13
%49 = load i32, i32* %13
%50 = icmp sle i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %44
%52 = load i32, i32* %13
store i32 %52, i32* %6
br label %67
53: ; preds = %44
%54 = load i32, i32* %13
%55 = load i32, i32* %12
%56 = sub nsw i32 %55, %54
store i32 %56, i32* %12
%57 = load i32, i32* %13
%58 = load i8*, i8** %8
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %8
br label %61
61: ; preds = %53, %42
br label %62
62: ; preds = %61
br label %26
63: ; preds = %26
%64 = load i32, i32* %9
%65 = load i32, i32* %12
%66 = sub nsw i32 %64, %65
store i32 %66, i32* %6
br label %67
67: ; preds = %63, %51, %37
%68 = load i32, i32* %6
ret i32 %68
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define i32 @_writen(i32 %0, i8* nocapture %1, i32 %2, void (...)* nocapture %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %6 to i8*
%9 = bitcast %struct.timeval* %7 to i8*
%10 = call i32 @FD_ZERO(i32* %6)
%11 = call i32 @FD_SET(i32 %0, i32* %6)
%12 = icmp eq i32 %4, 0
%13 = select i1 %12, %struct.timeval* null, %struct.timeval* %7
%14 = icmp sgt i32 %2, 0
br i1 %14, label %15, label %38
15: ; preds = %5
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
br label %18
18: ; preds = %15, %34
%19 = phi i8* [ %1, %15 ], [ %36, %34 ]
%20 = phi i32 [ %2, %15 ], [ %35, %34 ]
store i32 30, i32* %16
store i64 0, i64* %17
%21 = load i32, i32* @FD_SETSIZE
%22 = call i32 @select(i32 %21, i32* null, i32* %6, i32* null, %struct.timeval* %13)
%23 = icmp slt i32 %22, 0
br i1 %23, label %41, label %24
24: ; preds = %18
%25 = icmp eq i32 %22, 0
br i1 %25, label %26, label %27
26: ; preds = %24
br label %34
27: ; preds = %24
%28 = call i32 @write(i32 %0, i8* %19, i32 %20)
%29 = icmp slt i32 %28, 1
br i1 %29, label %41, label %30
30: ; preds = %27
%31 = sub nsw i32 %20, %28
%32 = zext i32 %28 to i64
%33 = getelementptr inbounds i8, i8* %19, i64 %32
br label %34
34: ; preds = %26, %30
%35 = phi i32 [ %20, %26 ], [ %31, %30 ]
%36 = phi i8* [ %19, %26 ], [ %33, %30 ]
%37 = icmp sgt i32 %35, 0
br i1 %37, label %18, label %38
38: ; preds = %34, %5
%39 = phi i32 [ %2, %5 ], [ %35, %34 ]
%40 = sub nsw i32 %2, %39
br label %41
41: ; preds = %27, %18, %38
%42 = phi i32 [ %40, %38 ], [ %28, %27 ], [ %22, %18 ]
ret i32 %42
}