#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,
            int timeout_secs, 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 = timeout_secs;
    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    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-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    118c <_writen+0x4c>
movq   $0x0,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    1240 <_writen+0x100>
mov    -0x18(%rbp),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2e2f(%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    11df <_writen+0x9f>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <_writen+0x109>
cmpl   $0x0,-0x30(%rbp)
jne    11f3 <_writen+0xb3>
mov    $0x0,%al
call   *-0x20(%rbp)
jmp    1236 <_writen+0xf6>
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     121a <_writen+0xda>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <_writen+0x109>
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    123b <_writen+0xfb>
jmp    1192 <_writen+0x52>
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    %r9d,%r15d
mov    %r8,0x10(%rsp)
mov    %ecx,%r13d
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
xor    %eax,%eax
test   %r15d,%r15d
lea    0x18(%rsp),%rbp
cmove  %rax,%rbp
mov    %r14d,%r15d
test   %r14d,%r14d
jle    11f8 <_writen+0xb8>
mov    %r14d,%r15d
jmp    11ab <_writen+0x6b>
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   *0x10(%rsp)
test   %r15d,%r15d
jle    11f8 <_writen+0xb8>
mov    %r13d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
lea    0xc(%rsp),%rdx
xor    %ecx,%ecx
mov    %rbp,%r8
call   1050 <select@plt>
test   %eax,%eax
js     11fd <_writen+0xbd>
je     11a0 <_writen+0x60>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11fd <_writen+0xbd>
sub    %eax,%r15d
mov    %eax,%eax
add    %rax,%rbx
test   %r15d,%r15d
jg     11ab <_writen+0x6b>
sub    %r15d,%r14d
jmp    1200 <_writen+0xc0>
mov    %eax,%r14d
mov    %r14d,%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, i32 %3, void (...)* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca void (...)*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca %struct.timeval
%19 = alloca %struct.timeval*
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store void (...)* %4, void (...)** %12
store i32 %5, i32* %13
store %struct.timeval* %18, %struct.timeval** %19
%20 = call i32 @FD_ZERO(i32* %17)
%21 = load i32, i32* %8
%22 = call i32 @FD_SET(i32 %21, i32* %17)
%23 = load i32, i32* %13
%24 = icmp ne i32 %23, 0
br i1 %24, label %26, label %25
25: ; preds = %6
store %struct.timeval* null, %struct.timeval** %19
br label %26
26: ; preds = %25, %6
%27 = load i32, i32* %10
store i32 %27, i32* %14
br label %28
28: ; preds = %65, %26
%29 = load i32, i32* %14
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %66
31: ; preds = %28
%32 = load i32, i32* %11
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
store i64 0, i64* %34
%35 = load i32, i32* @FD_SETSIZE
%36 = load %struct.timeval*, %struct.timeval** %19
%37 = call i32 @select(i32 %35, i32* null, i32* %17, i32* null, %struct.timeval* %36)
store i32 %37, i32* %16
%38 = load i32, i32* %16
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %31
%41 = load i32, i32* %16
store i32 %41, i32* %7
br label %70
42: ; preds = %31
%43 = load i32, i32* %16
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = load void (...)*, void (...)** %12
br label %64
47: ; preds = %42
%48 = load i32, i32* %8
%49 = load i8*, i8** %9
%50 = load i32, i32* %14
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %15
%52 = load i32, i32* %15
%53 = icmp sle i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = load i32, i32* %15
store i32 %55, i32* %7
br label %70
56: ; preds = %47
%57 = load i32, i32* %15
%58 = load i32, i32* %14
%59 = sub nsw i32 %58, %57
store i32 %59, i32* %14
%60 = load i32, i32* %15
%61 = load i8*, i8** %9
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %61, i64 %62
store i8* %63, i8** %9
br label %64
64: ; preds = %56, %45
br label %65
65: ; preds = %64
br label %28
66: ; preds = %28
%67 = load i32, i32* %10
%68 = load i32, i32* %14
%69 = sub nsw i32 %67, %68
store i32 %69, i32* %7
br label %70
70: ; preds = %66, %54, %40
%71 = load i32, i32* %7
ret i32 %71
}

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