#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*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int strlen(char *);
int write(int, char *, int);
int uarthandler_write(int fd, char *chars) {
  int len = strlen(chars);
  chars[len] = 0x0d;
  chars[len + 1] = 0x00;
  int n = write(fd, chars, strlen(chars));
  if (n < 0) {
    fputs("write failed!\n", stderr);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<uarthandler_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0xd,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11bb <uarthandler_write+0x8b>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1050 <fputs@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <uarthandler_write+0x92>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uarthandler_write>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
cltq
movw   $0xd,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jns    117e <uarthandler_write+0x4e>
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
call   1050 <fputs@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"write failed!\0A\00"
@stderr = global i32 0
define i32 @uarthandler_write(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
store i8 13, i8* %13
%14 = load i8*, i8** %5
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8 0, i8* %18
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i8*, i8** %5
%22 = call i32 @strlen(i8* %21)
%23 = call i32 @write(i32 %19, i8* %20, i32 %22)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = load i32, i32* @stderr
%28 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %27)
store i32 0, i32* %3
br label %30
29: ; preds = %2
store i32 1, i32* %3
br label %30
30: ; preds = %29, %26
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [15 x i8] c"write failed!\0A\00"
@stderr = global i32 0
define i32 @uarthandler_write(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %1, i64 %4
store i8 13, i8* %5
%6 = add nsw i32 %3, 1
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 0, i8* %8
%9 = tail call i32 @strlen(i8* %1)
%10 = tail call i32 @write(i32 %0, i8* %1, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %2, %12
%16 = phi i32 [ 0, %12 ], [ 1, %2 ]
ret i32 %16
}