#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*/ fclose(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
size_t recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
void request(int sfd) {
  char buff[1024];
  int err;
  size_t n, cnt = 0;
  while ((n = fread(buff, 1, 1024, stdin)) > 0) {
    send(sfd, buff, n, 0);
  }
  fprintf(stderr, "sending request\n");
  while ((n = recv(sfd, buff, 1024, 0)) > 0) {
    fwrite(buff, 1, n, stdout);
    fprintf(stderr, "recved %zu bytes\n", n);
    cnt += n;
    if (n < 1024)
      break;
  }
  fprintf(stderr, "total bytes received: %zu\n", cnt);
  if (n < 0)
    perror("recv error");
  fclose(stdout);
  close(sfd);
}


x86 assembly (clang, -O0):
<request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x428(%rbp)
lea    -0x410(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
jbe    11e6 <request+0x66>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x420(%rbp),%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
jmp    1199 <request+0x19>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x420(%rbp)
cmp    $0x0,%rax
jbe    1293 <request+0x113>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rdx
mov    0x2d97(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
mov    -0x420(%rbp),%rdx
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x420(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
cmpq   $0x400,-0x420(%rbp)
jae    128e <request+0x10e>
jmp    1293 <request+0x113>
jmp    11fd <request+0x7d>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
mov    -0x428(%rbp),%rdx
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
cmpq   $0x0,-0x420(%rbp)
jae    12cb <request+0x14b>
lea    0xd78(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   1050 <fclose@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<request>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r12d
mov    0x2e5d(%rip),%rbp
mov    0x0(%rbp),%ecx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
test   %rax,%rax
je     11da <request+0x6a>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    0x0(%rbp),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1040 <fread@plt>
test   %rax,%rax
jne    11b0 <request+0x40>
mov    0x2df7(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe14(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rsp,%r14
lea    0xe14(%rip),%r15
xor    %ebx,%ebx
nop
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1250 <request+0xe0>
mov    %rax,%rbp
mov    0x2daf(%rip),%rax
mov    (%rax),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
call   1090 <fwrite@plt>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
add    %rbp,%rbx
cmp    $0x3ff,%rbp
ja     1200 <request+0x90>
mov    0x0(%r13),%edi
lea    0xdc8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
call   1050 <fclose@plt>
mov    %r12d,%edi
call   1070 <close@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"sending request\0A\00"
@stdout = global i32 0
@.str.1 = [18 x i8] c"recved %zu bytes\0A\00"
@.str.2 = [27 x i8] c"total bytes received: %zu\0A\00"
@.str.3 = [11 x i8] c"recv error\00"
define void @request(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %6
br label %7
7: ; preds = %12, %1
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i64 @fread(i8* %8, i32 1, i32 1024, i32 %9)
store i64 %10, i64* %5
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i32, i32* %2
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = load i64, i64* %5
%16 = call i32 @send(i32 %13, i8* %14, i64 %15, i32 0)
br label %7
17: ; preds = %7
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %39, %17
%21 = load i32, i32* %2
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = call i64 @recv(i32 %21, i8* %22, i32 1024, i32 0)
store i64 %23, i64* %5
%24 = icmp ugt i64 %23, 0
br i1 %24, label %25, label %40
25: ; preds = %20
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%27 = load i64, i64* %5
%28 = load i32, i32* @stdout
%29 = call i32 @fwrite(i8* %26, i32 1, i64 %27, i32 %28)
%30 = load i32, i32* @stderr
%31 = load i64, i64* %5
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %31)
%33 = load i64, i64* %5
%34 = load i64, i64* %6
%35 = add i64 %34, %33
store i64 %35, i64* %6
%36 = load i64, i64* %5
%37 = icmp ult i64 %36, 1024
br i1 %37, label %38, label %39
38: ; preds = %25
br label %40
39: ; preds = %25
br label %20
40: ; preds = %38, %20
%41 = load i32, i32* @stderr
%42 = load i64, i64* %6
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 %42)
%44 = load i64, i64* %5
%45 = icmp ult i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %40
%49 = load i32, i32* @stdout
%50 = call i32 @fclose(i32 %49)
%51 = load i32, i32* %2
%52 = call i32 @close(i32 %51)
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"sending request\0A\00"
@stdout = global i32 0
@.str.1 = [18 x i8] c"recved %zu bytes\0A\00"
@.str.2 = [27 x i8] c"total bytes received: %zu\0A\00"
define void @request(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @stdin
%5 = call i64 @fread(i8* %3, i32 1, i32 1024, i32 %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %1, %7
%8 = phi i64 [ %11, %7 ], [ %5, %1 ]
%9 = call i32 @send(i32 %0, i8* %3, i64 %8, i32 0)
%10 = load i32, i32* @stdin
%11 = call i64 @fread(i8* %3, i32 1, i32 1024, i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %7
13: ; preds = %7, %1
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %20, %13
%17 = phi i64 [ 0, %13 ], [ %25, %20 ]
%18 = call i64 @recv(i32 %0, i8* %3, i32 1024, i32 0)
%19 = icmp eq i64 %18, 0
br i1 %19, label %27, label %20
20: ; preds = %16
%21 = load i32, i32* @stdout
%22 = call i32 @fwrite(i8* %3, i32 1, i64 %18, i32 %21)
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i64 %18)
%25 = add i64 %18, %17
%26 = icmp ult i64 %18, 1024
br i1 %26, label %27, label %16
27: ; preds = %20, %16
%28 = phi i64 [ %25, %20 ], [ %17, %16 ]
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i64 %28)
%31 = load i32, i32* @stdout
%32 = call i32 @fclose(i32 %31)
%33 = call i32 @close(i32 %0)
ret void
}