#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
void read_routine(int fd) {
  char buf[1024];
  int str_len;
  while (1) {
    str_len = read(fd, buf, sizeof(buf));
    if (str_len < 0) {
      perror("read() failed");
      return;
    } else if (str_len == 0)
      return;
    buf[str_len] = '\0';
    printf("[recv %d bytes] %s\n", str_len, buf);
  }
}


x86 assembly (clang, -O0):
<read_routine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1176 <read_routine+0x46>
lea    0xe94(%rip),%rdi
call   1050 <perror@plt>
jmp    11ba <read_routine+0x8a>
cmpl   $0x0,-0x414(%rbp)
jne    1188 <read_routine+0x58>
jmp    11ba <read_routine+0x8a>
jmp    118d <read_routine+0x5d>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%esi
lea    -0x410(%rbp),%rdx
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
jmp    113e <read_routine+0xe>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_routine>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
js     118a <read_routine+0x5a>
lea    0xeb9(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
test   %eax,%eax
je     1196 <read_routine+0x66>
mov    %eax,%ecx
movb   $0x0,(%rsp,%rcx,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    1160 <read_routine+0x30>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [14 x i8] c"read() failed\00"
@.str.1 = [20 x i8] c"[recv %d bytes] %s\0A\00"
define void @read_routine(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 1024)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %11, %1
%7 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %1, %11
%9 = phi i32 [ %15, %11 ], [ %4, %1 ]
%10 = icmp eq i32 %9, 0
br i1 %10, label %17, label %11
11: ; preds = %8
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %3)
%15 = call i32 @read(i32 %0, i8* %3, i32 1024)
%16 = icmp slt i32 %15, 0
br i1 %16, label %6, label %8
17: ; preds = %8, %6
ret void
}