#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*/ EBADF;
#define STDERR_FILENO 129
#define STDOUT_FILENO 128
int /*<<< orphan*/ errno;
int /*<<< orphan*/ putChar(int /*<<< orphan*/);
int _write(int file, char *ptr, int len) {
  int n;
  switch (file) {
  case STDOUT_FILENO:
  case STDERR_FILENO:
    for (n = 0; n < len; ++n) {
      putChar(*ptr++);
    }
    break;
  default:
    errno = EBADF;
    return -1;
  }
  return len;
}


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
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     1173 <_write+0x63>
jmp    1136 <_write+0x26>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    116e <_write+0x5e>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <putChar@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113d <_write+0x2d>
jmp    1191 <_write+0x81>
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1197 <_write+0x87>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
push   %r15
push   %r14
push   %rbx
and    $0xfffffffe,%edi
cmp    $0x80,%edi
jne    1145 <_write+0x35>
mov    %edx,%r15d
test   %edx,%edx
jle    115d <_write+0x4d>
mov    %rsi,%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
movsbl (%r14,%rbx,1),%edi
call   1030 <putChar@plt>
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1130 <_write+0x20>
jmp    115d <_write+0x4d>
mov    0x2e8c(%rip),%rax
mov    (%rax),%eax
mov    0x2e8b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EBADF = 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
switch i32 %9, label %25 [
i32 128, label %10
i32 129, label %10
]
10: ; preds = %3, %3
store i32 0, i32* %8
br label %11
11: ; preds = %21, %10
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %24
15: ; preds = %11
%16 = load i8*, i8** %6
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %6
%18 = load i8, i8* %16
%19 = sext i8 %18 to i32
%20 = call i32 @putChar(i32 %19)
br label %21
21: ; preds = %15
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
store i32 %23, i32* %8
br label %11
24: ; preds = %11
br label %27
25: ; preds = %3
%26 = load i32, i32* @EBADF
store i32 %26, i32* @errno
store i32 -1, i32* %4
br label %29
27: ; preds = %24
%28 = load i32, i32* %7
store i32 %28, i32* %4
br label %29
29: ; preds = %27, %25
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@EBADF = global i32 0
@errno = global i32 0
define i32 @_write(i32 %0, i8* nocapture %1, i32 %2) {
%4 = and i32 %0, -2
%5 = icmp eq i32 %4, 128
br i1 %5, label %6, label %17
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %19
8: ; preds = %6, %8
%9 = phi i32 [ %15, %8 ], [ 0, %6 ]
%10 = phi i8* [ %11, %8 ], [ %1, %6 ]
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %10
%13 = sext i8 %12 to i32
%14 = tail call i32 @putChar(i32 %13)
%15 = add nuw nsw i32 %9, 1
%16 = icmp eq i32 %15, %2
br i1 %16, label %19, label %8
17: ; preds = %3
%18 = load i32, i32* @EBADF
store i32 %18, i32* @errno
br label %19
19: ; preds = %8, %6, %17
%20 = phi i32 [ -1, %17 ], [ %2, %6 ], [ %2, %8 ]
ret i32 %20
}