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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t read(int, char *, int);
int /*<<< orphan*/ stdout;
int read_response(int fd) {
  char buf[8192];
  ssize_t bytes = read(fd, buf, sizeof(buf) - 1);
  if (bytes <= 0) {
    perror("Error reading response");
    return 1;
  }
  buf[bytes] = 0;
  int result = atoi(buf);
  do {
    fwrite(buf, 1, bytes, stdout);
    bytes = read(fd, buf, sizeof(buf));
  } while (bytes > 0);
  return result;
}


x86 assembly (clang, -O0):
<read_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x1fff,%edx
call   1030 <read@plt>
mov    %rax,-0x2018(%rbp)
cmpq   $0x0,-0x2018(%rbp)
ja     118f <read_response+0x4f>
lea    0xe82(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1203 <read_response+0xc3>
mov    -0x2018(%rbp),%rax
movb   $0x0,-0x2010(%rbp,%rax,1)
lea    -0x2010(%rbp),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x201c(%rbp)
lea    -0x2010(%rbp),%rdi
mov    -0x2018(%rbp),%rdx
mov    0x2e1b(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x8(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <read@plt>
mov    %rax,-0x2018(%rbp)
cmpq   $0x0,-0x2018(%rbp)
ja     11b0 <read_response+0x70>
mov    -0x201c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_response>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %edi,%r15d
mov    %rsp,%rsi
mov    $0x1fff,%edx
call   1030 <read@plt>
test   %rax,%rax
je     11bf <read_response+0x7f>
mov    %rax,%rbx
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1050 <atoi@plt>
mov    %eax,%r14d
mov    0x2e60(%rip),%r12
mov    (%r12),%ecx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
call   1060 <fwrite@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x2000,%edx
call   1030 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1180 <read_response+0x40>
mov    %r14d,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3a(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%r14d
jmp    11ac <read_response+0x6c>

LLVM IR (-O0):
@.str = [23 x i8] c"Error reading response\00"
@stdout = global i32 0
define i32 @read_response(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [8192 x i8]
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%9 = call i64 @read(i32 %7, i8* %8, i32 8191)
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = icmp ule i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %32
14: ; preds = %1
%15 = load i64, i64* %5
%16 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 %15
store i8 0, i8* %16
%17 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %6
br label %19
19: ; preds = %27, %14
%20 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%21 = load i64, i64* %5
%22 = load i32, i32* @stdout
%23 = call i32 @fwrite(i8* %20, i32 1, i64 %21, i32 %22)
%24 = load i32, i32* %3
%25 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%26 = call i64 @read(i32 %24, i8* %25, i32 8192)
store i64 %26, i64* %5
br label %27
27: ; preds = %19
%28 = load i64, i64* %5
%29 = icmp ugt i64 %28, 0
br i1 %29, label %19, label %30
30: ; preds = %27
%31 = load i32, i32* %6
store i32 %31, i32* %2
br label %32
32: ; preds = %30, %12
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [23 x i8] c"Error reading response\00"
@stdout = global i32 0
define i32 @read_response(i32 %0) {
%2 = alloca [8192 x i8]
%3 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%4 = call i64 @read(i32 %0, i8* %3, i32 8191)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %17
8: ; preds = %1
%9 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 %4
store i8 0, i8* %9
%10 = call i32 @atoi(i8* %3)
br label %11
11: ; preds = %11, %8
%12 = phi i64 [ %4, %8 ], [ %15, %11 ]
%13 = load i32, i32* @stdout
%14 = call i32 @fwrite(i8* %3, i32 1, i64 %12, i32 %13)
%15 = call i64 @read(i32 %0, i8* %3, i32 8192)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %11
17: ; preds = %11, %6
%18 = phi i32 [ 1, %6 ], [ %10, %11 ]
ret i32 %18
}