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


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,-0x18(%rbp)
sub    $0x80,%eax
je     115b <_write+0x3b>
jmp    1148 <_write+0x28>
mov    -0x18(%rbp),%eax
sub    $0x81,%eax
je     116f <_write+0x4f>
jmp    1183 <_write+0x63>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <write_stdout@plt>
mov    %eax,-0x4(%rbp)
jmp    119c <_write+0x7c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <write_stderr@plt>
mov    %eax,-0x4(%rbp)
jmp    119c <_write+0x7c>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write>:
cmp    $0x81,%edi
je     113a <_write+0x1a>
cmp    $0x80,%edi
jne    1144 <_write+0x24>
mov    %rsi,%rdi
mov    %edx,%esi
jmp    1030 <write_stdout@plt>
mov    %rsi,%rdi
mov    %edx,%esi
jmp    1040 <write_stderr@plt>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    0x2e8c(%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
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i32, i32* %5
switch i32 %8, label %17 [
i32 128, label %9
i32 129, label %13
]
9: ; preds = %3
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @write_stdout(i8* %10, i32 %11)
store i32 %12, i32* %4
br label %19
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i32, i32* %7
%16 = call i32 @write_stderr(i8* %14, i32 %15)
store i32 %16, i32* %4
br label %19
17: ; preds = %3
%18 = load i32, i32* @EBADF
store i32 %18, i32* @errno
store i32 -1, i32* %4
br label %19
19: ; preds = %17, %13, %9
%20 = load i32, i32* %4
ret i32 %20
}

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 @write_stdout(i8* %1, i32 %2)
br label %10
6: ; preds = %3
%7 = tail call i32 @write_stderr(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 = %8, %6, %4
%11 = phi i32 [ -1, %8 ], [ %7, %6 ], [ %5, %4 ]
ret i32 %11
}