#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*/ *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
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) {
      perror("select");
      exit(1);
    }
    if (i != 1) {
      fprintf(stderr, "select returned unexpected value %d\n", i);
      exit(1);
    }
    i = write(fd, buf, buflen);
    if (i < 0) {
      if (errno != EAGAIN)
        perror("write");
      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     127e <write_frame+0x10e>
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <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   1060 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11e0 <write_frame+0x70>
lea    0xe2f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpl   $0x1,-0x20(%rbp)
je     120c <write_frame+0x9c>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe0a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1259 <write_frame+0xe9>
mov    0x2da8(%rip),%rax
mov    (%rax),%rax
mov    0x2d8e(%rip),%rcx
cmp    (%rcx),%rax
je     124f <write_frame+0xdf>
lea    0xde2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <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    1183 <write_frame+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_frame>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rdx,%rdx
je     11ea <write_frame+0x7a>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x1(%r15),%r12d
lea    0xc(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %r12d,%edi
xor    %esi,%esi
mov    %rbp,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1060 <select@plt>
test   %eax,%eax
js     11f7 <write_frame+0x87>
cmp    $0x1,%eax
jne    120d <write_frame+0x9d>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     122e <write_frame+0xbe>
mov    %eax,%eax
add    %rax,%rbx
sub    %rax,%r14
jne    11a0 <write_frame+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe02(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%edi
lea    0xdea(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%rax
je     1250 <write_frame+0xe0>
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"select\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"select returned unexpected value %d\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [6 x i8] c"write\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 = %47, %3
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %56
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 %24
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %12
%25 = load i32, i32* %8
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = load i32, i32* %8
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = load i32, i32* %4
%34 = load i8*, i8** %5
%35 = load i64, i64* %6
%36 = call i32 @write(i32 %33, i8* %34, i64 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %32
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %39
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %32
%48 = load i32, i32* %8
%49 = load i8*, i8** %5
%50 = sext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8* %51, i8** %5
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = load i64, i64* %6
%55 = sub i64 %54, %53
store i64 %55, i64* %6
br label %9
56: ; preds = %9
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"select\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"select returned unexpected value %d\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [6 x i8] c"write\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 %41, label %7
7: ; preds = %3
%8 = add nsw i32 %0, 1
br label %9
9: ; preds = %7, %36
%10 = phi i64 [ %2, %7 ], [ %39, %36 ]
%11 = phi i8* [ %1, %7 ], [ %38, %36 ]
%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 %19
16: ; preds = %9
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = icmp eq i32 %14, 1
br i1 %20, label %25, label %21
21: ; preds = %19
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19
%26 = call i32 @write(i32 %0, i8* %11, i64 %10)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i64, i64* @errno
%30 = load i64, i64* @EAGAIN
%31 = icmp eq i64 %29, %30
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %25
%37 = zext i32 %26 to i64
%38 = getelementptr inbounds i8, i8* %11, i64 %37
%39 = sub i64 %10, %37
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %9
41: ; preds = %36, %3
ret void
}