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


x86 assembly (clang, -O0):
<socketRead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     11ad <socketRead+0x6d>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe6c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <socketRead+0x7f>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketRead>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
call   1040 <read@plt>
test   %eax,%eax
jle    1159 <socketRead+0x19>
mov    %eax,%ecx
movb   $0x0,(%rbx,%rcx,1)
jmp    118d <socketRead+0x4d>
mov    0x2e80(%rip),%rax
mov    (%rax),%ebx
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe89(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <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 @socketRead(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 i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
store i32 -1, i32* %4
br label %27
21: ; preds = %3
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
%26 = load i32, i32* %8
store i32 %26, i32* %4
br label %27
27: ; preds = %21, %14
%28 = load i32, i32* %4
ret i32 %28
}

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