#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 serial_write(char *, int);
int _write(int file, char *ptr, int len) {
  int num = 0;
  switch (file) {
  case STDOUT_FILENO:
    num = serial_write(ptr, len);
    break;
  case STDERR_FILENO:
    num = serial_write(ptr, len);
    break;
  default:
    errno = EBADF;
    return -1;
  }
  return num;
}


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)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1152 <_write+0x42>
jmp    113f <_write+0x2f>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     1166 <_write+0x56>
jmp    117a <_write+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <serial_write@plt>
mov    %eax,-0x18(%rbp)
jmp    1198 <_write+0x88>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <serial_write@plt>
mov    %eax,-0x18(%rbp)
jmp    1198 <_write+0x88>
mov    0x2e57(%rip),%rax
mov    (%rax),%ecx
mov    0x2e56(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <_write+0x8e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
cmp    $0x81,%edi
je     1120 <_write+0x10>
cmp    $0x80,%edi
jne    112a <_write+0x1a>
mov    %rsi,%rdi
mov    %edx,%esi
jmp    1030 <serial_write@plt>
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    0x2ea6(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
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
store i32 0, i32* %8
%9 = load i32, i32* %5
switch i32 %9, label %18 [
i32 128, label %10
i32 129, label %14
]
10: ; preds = %3
%11 = load i8*, i8** %6
%12 = load i32, i32* %7
%13 = call i32 @serial_write(i8* %11, i32 %12)
store i32 %13, i32* %8
br label %20
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i32 @serial_write(i8* %15, i32 %16)
store i32 %17, i32* %8
br label %20
18: ; preds = %3
%19 = load i32, i32* @EBADF
store i32 %19, i32* @errno
store i32 -1, i32* %4
br label %22
20: ; preds = %14, %10
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %22
22: ; preds = %20, %18
%23 = load i32, i32* %4
ret i32 %23
}

LLVM IR (-O2):
@EBADF = global i32 0
@errno = global i32 0
define i32 @_write(i32 %0, i8* %1, i32 %2) {
switch i32 %0, label %8 [
i32 128, label %4
i32 129, label %6
]
4: ; preds = %3
%5 = tail call i32 @serial_write(i8* %1, i32 %2)
br label %10
6: ; preds = %3
%7 = tail call i32 @serial_write(i8* %1, i32 %2)
br label %10
8: ; preds = %3
%9 = load i32, i32* @EBADF
store i32 %9, i32* @errno
br label %10
10: ; preds = %4, %6, %8
%11 = phi i32 [ -1, %8 ], [ %7, %6 ], [ %5, %4 ]
ret i32 %11
}