#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*/ usart_putchar(int /*<<< orphan*/);
int _write(int file, char *ptr, int len) {
  int n;
  switch (file) {
  case STDOUT_FILENO:
    for (n = 0; n < len; n++) {
      usart_putchar(*ptr++);
    }
    break;
  case STDERR_FILENO:
    for (n = 0; n < len; n++) {
      usart_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
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     114b <_write+0x3b>
jmp    1138 <_write+0x28>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     1188 <_write+0x78>
jmp    11c5 <_write+0xb5>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1183 <_write+0x73>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <usart_putchar@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1152 <_write+0x42>
jmp    11e3 <_write+0xd3>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c0 <_write+0xb0>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%edi
call   1030 <usart_putchar@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    118f <_write+0x7f>
jmp    11e3 <_write+0xd3>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e9 <_write+0xd9>
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
mov    %rsi,%r15
mov    %edx,%r14d
cmp    $0x80,%edi
je     1155 <_write+0x45>
cmp    $0x81,%edi
jne    1175 <_write+0x65>
test   %edx,%edx
jle    118d <_write+0x7d>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <usart_putchar@plt>
add    $0x1,%rbx
cmp    %ebx,%r14d
jne    1140 <_write+0x30>
jmp    118d <_write+0x7d>
test   %edx,%edx
jle    118d <_write+0x7d>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <usart_putchar@plt>
add    $0x1,%rbx
cmp    %ebx,%r14d
jne    1160 <_write+0x50>
jmp    118d <_write+0x7d>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    0x2e5b(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
mov    %r14d,%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 %40 [
i32 128, label %10
i32 129, label %25
]
10: ; preds = %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 @usart_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 %42
25: ; preds = %3
store i32 0, i32* %8
br label %26
26: ; preds = %36, %25
%27 = load i32, i32* %8
%28 = load i32, i32* %7
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %39
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %6
%33 = load i8, i8* %31
%34 = sext i8 %33 to i32
%35 = call i32 @usart_putchar(i32 %34)
br label %36
36: ; preds = %30
%37 = load i32, i32* %8
%38 = add nsw i32 %37, 1
store i32 %38, i32* %8
br label %26
39: ; preds = %26
br label %42
40: ; preds = %3
%41 = load i32, i32* @EBADF
store i32 %41, i32* @errno
store i32 -1, i32* %4
br label %44
42: ; preds = %39, %24
%43 = load i32, i32* %7
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %40
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@EBADF = global i32 0
@errno = global i32 0
define i32 @_write(i32 %0, i8* nocapture %1, i32 %2) {
switch i32 %0, label %26 [
i32 128, label %6
i32 129, label %4
]
4: ; preds = %3
%5 = icmp sgt i32 %2, 0
br i1 %5, label %17, label %28
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %28
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 @usart_putchar(i32 %13)
%15 = add nuw nsw i32 %9, 1
%16 = icmp eq i32 %15, %2
br i1 %16, label %28, label %8
17: ; preds = %4, %17
%18 = phi i32 [ %24, %17 ], [ 0, %4 ]
%19 = phi i8* [ %20, %17 ], [ %1, %4 ]
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %19
%22 = sext i8 %21 to i32
%23 = tail call i32 @usart_putchar(i32 %22)
%24 = add nuw nsw i32 %18, 1
%25 = icmp eq i32 %24, %2
br i1 %25, label %28, label %17
26: ; preds = %3
%27 = load i32, i32* @EBADF
store i32 %27, i32* @errno
br label %28
28: ; preds = %17, %8, %4, %6, %26
%29 = phi i32 [ -1, %26 ], [ %2, %6 ], [ %2, %4 ], [ %2, %8 ], [ %2, %17 ]
ret i32 %29
}