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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int EINVAL;
scalar_t__ errno;
int write(int, char *, int);
int pty_send(int fd, char d) {
  int r;
  do {
    r = write(fd, &d, 1);
  } while (r < 0 && (errno == EINTR || errno == EAGAIN));
  return (r == 1) ? 0 : -EINVAL;
}


x86 assembly (clang, -O0):
<pty_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0xd(%rbp)
jge    1183 <pty_send+0x73>
mov    0x2e95(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e81(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0xe(%rbp)
je     117d <pty_send+0x6d>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    1121 <pty_send+0x11>
cmpl   $0x1,-0xc(%rbp)
jne    11a2 <pty_send+0x92>
xor    %eax,%eax
mov    %eax,-0x14(%rbp)
jmp    11b0 <pty_send+0xa0>
xor    %eax,%eax
mov    0x2e25(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pty_send>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    %sil,0xf(%rsp)
lea    0xf(%rsp),%r14
mov    0x2eb0(%rip),%r15
mov    0x2ea1(%rip),%r12
mov    0x2e82(%rip),%r13
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    1162 <pty_send+0x52>
mov    (%r15),%rcx
cmp    (%r12),%rcx
je     1140 <pty_send+0x30>
cmp    0x0(%r13),%rcx
je     1140 <pty_send+0x30>
mov    0x2e67(%rip),%rdx
xor    %ecx,%ecx
sub    (%rdx),%ecx
xor    %edx,%edx
cmp    $0x1,%eax
cmove  %edx,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EINVAL = global i32 0
define i32 @pty_send(i32 %0, i8 signext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
br label %6
6: ; preds = %22, %2
%7 = load i32, i32* %3
%8 = call i32 @write(i32 %7, i8* %4, i32 1)
store i32 %8, i32* %5
br label %9
9: ; preds = %6
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %17, %18
br label %20
20: ; preds = %16, %12
%21 = phi i1 [ true, %12 ], [ %19, %16 ]
br label %22
22: ; preds = %20, %9
%23 = phi i1 [ false, %9 ], [ %21, %20 ]
br i1 %23, label %6, label %24
24: ; preds = %22
%25 = load i32, i32* %5
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %24
br label %31
28: ; preds = %24
%29 = load i32, i32* @EINVAL
%30 = sub nsw i32 0, %29
br label %31
31: ; preds = %28, %27
%32 = phi i32 [ 0, %27 ], [ %30, %28 ]
ret i32 %32
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EINVAL = global i32 0
define i32 @pty_send(i32 %0, i8 signext %1) {
%3 = alloca i8
store i8 %1, i8* %3
br label %4
4: ; preds = %7, %2
%5 = call i32 @write(i32 %0, i8* %3, i32 1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %4
%8 = load i64, i64* @errno
%9 = load i64, i64* @EINTR
%10 = icmp eq i64 %8, %9
%11 = load i64, i64* @EAGAIN
%12 = icmp eq i64 %8, %11
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %4, label %14
14: ; preds = %4, %7
%15 = icmp eq i32 %5, 1
%16 = load i32, i32* @EINVAL
%17 = sub nsw i32 0, %16
%18 = select i1 %15, i32 0, i32 %17
ret i32 %18
}