#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int read_socket(int sock) {
  char buf[1024];
  int len;
  len = read(sock, buf, sizeof(buf) - 1);
  if (len < 0) {
    perror("read");
    return -1;
  }
  if (len == 0) {
    return -1;
  }
  buf[len] = 0;
  printf("%s", buf);
  fflush(stdout);
  return len;
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    118d <read_socket+0x4d>
lea    0xe84(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <read_socket+0x9f>
cmpl   $0x0,-0x414(%rbp)
jne    11a6 <read_socket+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <read_socket+0x9f>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1182 <read_socket+0x42>
je     118e <read_socket+0x4e>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xe9b(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
jmp    1193 <read_socket+0x53>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@.str.1 = [3 x i8] c"%s\00"
@stdout = global i32 0
define i32 @read_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 1023)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %26
13: ; preds = %1
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -1, i32* %2
br label %26
17: ; preds = %13
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %19
store i8 0, i8* %20
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
%25 = load i32, i32* %5
store i32 %25, i32* %2
br label %26
26: ; preds = %17, %16, %11
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@.str.1 = [3 x i8] c"%s\00"
@stdout = global i32 0
define i32 @read_socket(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 1023)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
8: ; preds = %1
%9 = icmp eq i32 %4, 0
br i1 %9, label %16, label %10
10: ; preds = %8
%11 = zext i32 %4 to i64
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %11
store i8 0, i8* %12
%13 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%14 = load i32, i32* @stdout
%15 = call i32 @fflush(i32 %14)
br label %16
16: ; preds = %8, %10, %6
%17 = phi i32 [ -1, %6 ], [ %4, %10 ], [ -1, %8 ]
ret i32 %17
}