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

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, int);
int /*<<< orphan*/ exit(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ warn(char *);
int write(int, char *, unsigned long);
void write_frame(int fd, char *buf, unsigned long buflen) {
  fd_set wfd;
  int i;
  while (buflen) {
    FD_ZERO(&wfd);
    FD_SET(fd, &wfd);
    i = select(fd + 1, NULL, &wfd, NULL, NULL);
    if (i < 0)
      err(1, "select failed");
    if (i != 1) {
      errx(1, "select returned unexpected value %d\n", i);
      exit(1);
    }
    i = write(fd, buf, buflen);
    if (i < 0) {
      if (errno != EAGAIN)
        warn("write failed");
      exit(1);
    }
    buf += i;
    buflen -= i;
  }
}


x86 assembly (clang, -O0):
<write_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1285 <write_frame+0x105>
lea    -0x1c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%r8d
lea    -0x1c(%rbp),%rdx
mov    %r8,%rsi
mov    %r8,%rcx
call   1070 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11eb <write_frame+0x6b>
mov    $0x1,%edi
lea    0xe1a(%rip),%rsi
call   1080 <err@plt>
cmpl   $0x1,-0x20(%rbp)
je     1213 <write_frame+0x93>
mov    -0x20(%rbp),%edx
mov    $0x1,%edi
lea    0xe0a(%rip),%rsi
call   1050 <errx@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1260 <write_frame+0xe0>
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
je     1256 <write_frame+0xd6>
lea    0xde2(%rip),%rdi
call   1030 <warn@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1193 <write_frame+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_frame>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11fc <write_frame+0x7c>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x1(%r15),%r12d
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   10a0 <FD_SET@plt>
mov    %r12d,%edi
xor    %esi,%esi
mov    %r13,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1070 <select@plt>
mov    %eax,%ebp
test   %eax,%eax
js     120b <write_frame+0x8b>
cmp    $0x1,%ebp
jne    121c <write_frame+0x9c>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <write@plt>
test   %eax,%eax
js     1239 <write_frame+0xb9>
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%r14
jne    11b0 <write_frame+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
lea    0xdeb(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1050 <errx@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
je     125b <write_frame+0xdb>
lea    0xddd(%rip),%rdi
call   1030 <warn@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"select failed\00"
@.str.1 = [37 x i8] c"select returned unexpected value %d\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [13 x i8] c"write failed\00"
define void @write_frame(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %9
9: ; preds = %45, %3
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %54
12: ; preds = %9
%13 = call i32 @FD_ZERO(i32* %7)
%14 = load i32, i32* %4
%15 = call i32 @FD_SET(i32 %14, i32* %7)
%16 = load i32, i32* %4
%17 = add nsw i32 %16, 1
%18 = call i32 @select(i32 %17, i32* null, i32* %7, i32* null, i32* null)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %12
%22 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %12
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* %8
%28 = call i32 @errx(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = load i32, i32* %4
%32 = load i8*, i8** %5
%33 = load i64, i64* %6
%34 = call i32 @write(i32 %31, i8* %32, i64 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %45
37: ; preds = %30
%38 = load i64, i64* @errno
%39 = load i64, i64* @EAGAIN
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30
%46 = load i32, i32* %8
%47 = load i8*, i8** %5
%48 = sext i32 %46 to i64
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %5
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = load i64, i64* %6
%53 = sub i64 %52, %51
store i64 %53, i64* %6
br label %9
54: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"select failed\00"
@.str.1 = [37 x i8] c"select returned unexpected value %d\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [13 x i8] c"write failed\00"
define void @write_frame(i32 %0, i8* nocapture %1, i64 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %39, label %7
7: ; preds = %3
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %7, %34
%10 = phi i64 [ %2, %7 ], [ %37, %34 ]
%11 = phi i8* [ %1, %7 ], [ %36, %34 ]
%12 = call i32 @FD_ZERO(i32* %4)
%13 = call i32 @FD_SET(i32 %0, i32* %4)
%14 = call i32 @select(i32 %8, i32* null, i32* %4, i32* null, i32* null)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 @err(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %20
18: ; preds = %9
%19 = icmp eq i32 %14, 1
br i1 %19, label %23, label %20
20: ; preds = %18, %16
%21 = call i32 @errx(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %18
%24 = call i32 @write(i32 %0, i8* %11, i64 %10)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = load i64, i64* @errno
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %27, %28
br i1 %29, label %32, label %30
30: ; preds = %26
%31 = call i32 @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %23
%35 = zext i32 %24 to i64
%36 = getelementptr inbounds i8, i8* %11, i64 %35
%37 = sub i64 %10, %35
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %9
39: ; preds = %34, %3
ret void
}