#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 ssize_t;

/* Variables and functions */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ clean_buff(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send_stuff(int, char *, int);
int /*<<< orphan*/ stderr;
ssize_t forward_file(int sock_src, int sock_dest, long int dim_file) {
  char *buff;
  int nread, nwrite;
  long int tot_sent = 0;
  buff = (char *)calloc(1024, sizeof(char));
  if (buff == NULL) {
    fprintf(stderr, "ERROR 18: cannot allocate memory for buffer.\n");
    return (-1);
  }
  while ((nread = recv(sock_src, buff, 1024, 0)) > 0 && tot_sent < dim_file) {
    if (nread == -1)
      return (nread);
    nwrite = send_stuff(sock_dest, buff, nread);
    clean_buff(buff);
    tot_sent += (long int)nwrite;
    if (tot_sent == dim_file)
      return (tot_sent);
    if (nwrite < 0)
      return (nwrite);
  }
  return (tot_sent);
}


x86 assembly (clang, -O0):
<forward_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a9 <forward_file+0x59>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1269 <forward_file+0x119>
jmp    11ae <forward_file+0x5e>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
jle    11e2 <forward_file+0x92>
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
setl   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11f2 <forward_file+0xa2>
jmp    1262 <forward_file+0x112>
cmpl   $0xffffffff,-0x24(%rbp)
jne    1207 <forward_file+0xb7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1269 <forward_file+0x119>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <send_stuff@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <clean_buff@plt>
movslq -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1248 <forward_file+0xf8>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1269 <forward_file+0x119>
cmpl   $0x0,-0x28(%rbp)
jge    125d <forward_file+0x10d>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1269 <forward_file+0x119>
jmp    11ae <forward_file+0x5e>
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forward_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %esi,%r14d
mov    %edi,%r15d
mov    $0x400,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11c5 <forward_file+0x75>
mov    %rax,%r13
xor    %ebx,%ebx
nopl   (%rax)
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11e4 <forward_file+0x94>
cmp    %r12,%rbx
jge    11e4 <forward_file+0x94>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1050 <send_stuff@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1040 <clean_buff@plt>
movslq %ebp,%rax
add    %rax,%rbx
cmp    %r12,%rbx
je     11e1 <forward_file+0x91>
test   %ebp,%ebp
jns    1180 <forward_file+0x30>
mov    %ebp,%ebx
jmp    11e4 <forward_file+0x94>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11e4 <forward_file+0x94>
mov    %r12d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"ERROR 18: cannot allocate memory for buffer.\0A\00"
define i32 @forward_file(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64 0, i64* %11
%12 = call i64 @calloc(i32 1024, i32 1)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %62
19: ; preds = %3
br label %20
20: ; preds = %58, %19
%21 = load i32, i32* %5
%22 = load i8*, i8** %8
%23 = call i32 @recv(i32 %21, i8* %22, i32 1024, i32 0)
store i32 %23, i32* %9
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i64, i64* %11
%27 = load i64, i64* %7
%28 = icmp slt i64 %26, %27
br label %29
29: ; preds = %25, %20
%30 = phi i1 [ false, %20 ], [ %28, %25 ]
br i1 %30, label %31, label %59
31: ; preds = %29
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = load i32, i32* %9
store i32 %35, i32* %4
br label %62
36: ; preds = %31
%37 = load i32, i32* %6
%38 = load i8*, i8** %8
%39 = load i32, i32* %9
%40 = call i32 @send_stuff(i32 %37, i8* %38, i32 %39)
store i32 %40, i32* %10
%41 = load i8*, i8** %8
%42 = call i32 @clean_buff(i8* %41)
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = load i64, i64* %11
%46 = add nsw i64 %45, %44
store i64 %46, i64* %11
%47 = load i64, i64* %11
%48 = load i64, i64* %7
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %36
%51 = load i64, i64* %11
%52 = trunc i64 %51 to i32
store i32 %52, i32* %4
br label %62
53: ; preds = %36
%54 = load i32, i32* %10
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = load i32, i32* %10
store i32 %57, i32* %4
br label %62
58: ; preds = %53
br label %20
59: ; preds = %29
%60 = load i64, i64* %11
%61 = trunc i64 %60 to i32
store i32 %61, i32* %4
br label %62
62: ; preds = %59, %56, %50, %34, %16
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"ERROR 18: cannot allocate memory for buffer.\0A\00"
define i32 @forward_file(i32 %0, i32 %1, i64 %2) {
%4 = tail call i64 @calloc(i32 1024, i32 1)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %28
10: ; preds = %3, %24
%11 = phi i64 [ %20, %24 ], [ 0, %3 ]
%12 = tail call i32 @recv(i32 %0, i8* %5, i32 1024, i32 0)
%13 = icmp sgt i32 %12, 0
%14 = icmp slt i64 %11, %2
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %16, label %26
16: ; preds = %10
%17 = tail call i32 @send_stuff(i32 %1, i8* %5, i32 %12)
%18 = tail call i32 @clean_buff(i8* %5)
%19 = sext i32 %17 to i64
%20 = add nsw i64 %11, %19
%21 = icmp eq i64 %20, %2
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = trunc i64 %2 to i32
br label %28
24: ; preds = %16
%25 = icmp slt i32 %17, 0
br i1 %25, label %28, label %10
26: ; preds = %10
%27 = trunc i64 %11 to i32
br label %28
28: ; preds = %24, %26, %22, %7
%29 = phi i32 [ -1, %7 ], [ %23, %22 ], [ %27, %26 ], [ %17, %24 ]
ret i32 %29
}