#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;
int STDERR_FILENO;
int STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ outbyte(char);
int _write(int file, char *buf, int nbytes) {
  int i;
  if ((file != STDOUT_FILENO) && (file != STDERR_FILENO)) {
    errno = EBADF;
    return -1;
  }
  for (i = 0; i < nbytes; i++) {
    outbyte(buf[i]);
  }
  return nbytes;
}


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     1164 <_write+0x54>
mov    -0x8(%rbp),%eax
mov    0x2e92(%rip),%rcx
cmp    (%rcx),%eax
je     1164 <_write+0x54>
mov    0x2e7b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e82(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <_write+0x8c>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1196 <_write+0x86>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <outbyte@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116b <_write+0x5b>
mov    -0x14(%rbp),%eax
mov    %eax,-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,%r15
mov    0x2ecc(%rip),%rax
cmp    %edi,(%rax)
je     114d <_write+0x3d>
mov    0x2ea1(%rip),%rax
cmp    %edi,(%rax)
je     114d <_write+0x3d>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    0x2e95(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%r14d
jmp    1173 <_write+0x63>
test   %r14d,%r14d
jle    1173 <_write+0x63>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%edi
call   1030 <outbyte@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <_write+0x50>
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
@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
%10 = load i32, i32* @STDOUT_FILENO
%11 = icmp ne i32 %9, %10
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = load i32, i32* %5
%14 = load i32, i32* @STDERR_FILENO
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = load i32, i32* @EBADF
store i32 %17, i32* @errno
store i32 -1, i32* %4
br label %35
18: ; preds = %12, %3
store i32 0, i32* %8
br label %19
19: ; preds = %30, %18
%20 = load i32, i32* %8
%21 = load i32, i32* %7
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %33
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = call i32 @outbyte(i8 signext %28)
br label %30
30: ; preds = %23
%31 = load i32, i32* %8
%32 = add nsw i32 %31, 1
store i32 %32, i32* %8
br label %19
33: ; preds = %19
%34 = load i32, i32* %7
store i32 %34, i32* %4
br label %35
35: ; preds = %33, %16
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@EBADF = 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 %13
9: ; preds = %3
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %22
11: ; preds = %9
%12 = zext i32 %2 to i64
br label %15
13: ; preds = %3
%14 = load i32, i32* @EBADF
store i32 %14, i32* @errno
br label %22
15: ; preds = %11, %15
%16 = phi i64 [ 0, %11 ], [ %20, %15 ]
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = tail call i32 @outbyte(i8 signext %18)
%20 = add nuw nsw i64 %16, 1
%21 = icmp eq i64 %20, %12
br i1 %21, label %22, label %15
22: ; preds = %15, %9, %13
%23 = phi i32 [ -1, %13 ], [ %2, %9 ], [ %2, %15 ]
ret i32 %23
}