#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 */
int /*<<< orphan*/ EIO;
int STDERR_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ trace_send_blocking8(int /*<<< orphan*/, char);
int _write(int file, char *ptr, int len) {
  int i;
  if (file == STDOUT_FILENO || file == STDERR_FILENO) {
    for (i = 0; i < len; i++) {
      if (ptr[i] == '\n') {
        trace_send_blocking8(0, '\r');
      }
      trace_send_blocking8(0, ptr[i]);
    }
    return i;
  }
  errno = EIO;
  return -1;
}


x86 assembly (clang, -O0):
<_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2ec4(%rip),%rcx
cmp    (%rcx),%eax
je     1146 <_write+0x36>
mov    -0x8(%rbp),%eax
mov    0x2e8a(%rip),%rcx
cmp    (%rcx),%eax
jne    11a6 <_write+0x96>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    119b <_write+0x8b>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    117a <_write+0x6a>
xor    %edi,%edi
mov    $0xd,%esi
call   1030 <trace_send_blocking8@plt>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
xor    %edi,%edi
movsbl (%rax,%rcx,1),%esi
call   1030 <trace_send_blocking8@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    114d <_write+0x3d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bf <_write+0xaf>
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    0x2ecc(%rip),%rax
cmp    %edi,(%rax)
je     114d <_write+0x3d>
mov    0x2e99(%rip),%rax
cmp    %edi,(%rax)
je     114d <_write+0x3d>
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    0x2e8d(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
jmp    1190 <_write+0x80>
test   %r14d,%r14d
jle    118d <_write+0x7d>
mov    %r14d,%r15d
xor    %ebp,%ebp
jmp    1173 <_write+0x63>
nopl   0x0(%rax)
movsbl %al,%esi
xor    %edi,%edi
call   1030 <trace_send_blocking8@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
je     1190 <_write+0x80>
movzbl (%rbx,%rbp,1),%eax
cmp    $0xa,%al
jne    1160 <_write+0x50>
xor    %edi,%edi
mov    $0xd,%esi
call   1030 <trace_send_blocking8@plt>
movzbl (%rbx,%rbp,1),%eax
jmp    1160 <_write+0x50>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EIO = global i32 0
@errno = global i32 0
define i32 @_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* @STDOUT_FILENO
%11 = icmp eq i32 %9, %10
br i1 %11, label %16, label %12
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i32, i32* @STDERR_FILENO
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %43
16: ; preds = %12, %3
store i32 0, i32* %8
br label %17
17: ; preds = %38, %16
%18 = load i32, i32* %8
%19 = load i32, i32* %7
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %41
21: ; preds = %17
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @trace_send_blocking8(i32 0, i8 signext 13)
br label %31
31: ; preds = %29, %21
%32 = load i8*, i8** %6
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = call i32 @trace_send_blocking8(i32 0, i8 signext %36)
br label %38
38: ; preds = %31
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %17
41: ; preds = %17
%42 = load i32, i32* %8
store i32 %42, i32* %4
br label %45
43: ; preds = %12
%44 = load i32, i32* @EIO
store i32 %44, i32* @errno
store i32 -1, i32* %4
br label %45
45: ; preds = %43, %41
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EIO = global i32 0
@errno = global i32 0
define i32 @_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @STDOUT_FILENO
%5 = icmp eq i32 %4, %0
%6 = load i32, i32* @STDERR_FILENO
%7 = icmp eq i32 %6, %0
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %9, label %26
9: ; preds = %3
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %28
11: ; preds = %9
%12 = zext i32 %2 to i64
br label %13
13: ; preds = %11, %21
%14 = phi i64 [ 0, %11 ], [ %24, %21 ]
%15 = getelementptr inbounds i8, i8* %1, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 10
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @trace_send_blocking8(i32 0, i8 signext 13)
%20 = load i8, i8* %15
br label %21
21: ; preds = %18, %13
%22 = phi i8 [ %20, %18 ], [ %16, %13 ]
%23 = tail call i32 @trace_send_blocking8(i32 0, i8 signext %22)
%24 = add nuw nsw i64 %14, 1
%25 = icmp eq i64 %24, %12
br i1 %25, label %28, label %13
26: ; preds = %3
%27 = load i32, i32* @EIO
store i32 %27, i32* @errno
br label %28
28: ; preds = %21, %9, %26
%29 = phi i32 [ -1, %26 ], [ 0, %9 ], [ %2, %21 ]
ret i32 %29
}