#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*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int socketWrite(int new_fd, char *writebuff, int lenth) {
  int nbytes;
  writebuff[lenth] = '\r';
  writebuff[lenth + 1] = '\n';
  if ((nbytes = write(new_fd, writebuff, lenth + 2)) <= 0) {
    fprintf(stderr, "Read Error:%s\n", strerror(errno));
    close(new_fd);
    return -1;
  }
  return nbytes;
}


x86 assembly (clang, -O0):
<socketWrite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-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   $0xa,(%rax,%rcx,1)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x2,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     11cd <socketWrite+0x8d>
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe4c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <socketWrite+0x93>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketWrite>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
movslq %edx,%rdx
movw   $0xa0d,(%rsi,%rdx,1)
add    $0x2,%edx
call   1030 <write@plt>
test   %eax,%eax
jg     118e <socketWrite+0x4e>
mov    0x2e7f(%rip),%rax
mov    (%rax),%ebp
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe88(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"Read Error:%s\0A\00"
@errno = global i32 0
define i32 @socketWrite(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 i8*, i8** %6
%10 = load i32, i32* %7
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
store i8 13, i8* %12
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
store i8 10, i8* %17
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 2
%22 = call i32 @write(i32 %18, i8* %19, i32 %21)
store i32 %22, i32* %8
%23 = icmp sle i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %3
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %4
br label %33
31: ; preds = %3
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %24
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"Read Error:%s\0A\00"
@errno = global i32 0
define i32 @socketWrite(i32 %0, i8* nocapture %1, i32 %2) {
%4 = sext i32 %2 to i64
%5 = getelementptr inbounds i8, i8* %1, i64 %4
store i8 13, i8* %5
%6 = add nsw i32 %2, 1
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 10, i8* %8
%9 = add nsw i32 %2, 2
%10 = tail call i32 @write(i32 %0, i8* %1, i32 %9)
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = tail call i32 @close(i32 %0)
br label %18
18: ; preds = %3, %12
%19 = phi i32 [ -1, %12 ], [ %10, %3 ]
ret i32 %19
}