#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 */
int /*<<< orphan*/ clean_buff(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ malloc(int);
int read(int, char *, int);
int send_stuff(int, char *, int);
int /*<<< orphan*/ stderr;
ssize_t send_file(int sockfd, int fd, long int dim_file) {
  int nwrite, nread;
  long int tot_write = 0;
  char *buff;
  buff = (char *)malloc(1024 * sizeof(char));
  if (buff == NULL) {
    fprintf(stderr, "ERROR 17: cannot allocate memory for buffer.\n");
    return (-1);
  }
  clean_buff(buff);
  while ((nread = read(fd, buff, 1024)) > 0 && tot_write <= dim_file) {
    nwrite = send_stuff(sockfd, buff, nread);
    clean_buff(buff);
    tot_write += (long int)nwrite;
    if (tot_write == dim_file)
      break;
    if (nwrite < 0)
      return (nwrite);
  }
  return (tot_write);
}


x86 assembly (clang, -O0):
<send_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a4 <send_file+0x54>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe6d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    124a <send_file+0xfa>
mov    -0x30(%rbp),%rdi
call   1030 <clean_buff@plt>
mov    -0xc(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
jle    11df <send_file+0x8f>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
setle  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    11ef <send_file+0x9f>
jmp    1243 <send_file+0xf3>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <send_stuff@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <clean_buff@plt>
movslq -0x1c(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    1229 <send_file+0xd9>
jmp    1243 <send_file+0xf3>
cmpl   $0x0,-0x1c(%rbp)
jge    123e <send_file+0xee>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124a <send_file+0xfa>
jmp    11ad <send_file+0x5d>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r12d
mov    %edi,%r15d
mov    $0x400,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11c3 <send_file+0x73>
mov    %rax,%r13
mov    %rax,%rdi
call   1030 <clean_buff@plt>
xor    %ebx,%ebx
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e2 <send_file+0x92>
cmp    %r14,%rbx
jg     11e2 <send_file+0x92>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <send_stuff@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1030 <clean_buff@plt>
movslq %ebp,%rax
add    %rax,%rbx
cmp    %r14,%rbx
je     11df <send_file+0x8f>
test   %ebp,%ebp
jns    1180 <send_file+0x30>
mov    %ebp,%ebx
jmp    11e2 <send_file+0x92>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    11e2 <send_file+0x92>
mov    %r14,%rbx
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 17: cannot allocate memory for buffer.\0A\00"
define i32 @send_file(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64 0, i64* %10
%12 = call i64 @malloc(i32 1024)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %11
%14 = load i8*, i8** %11
%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 %57
19: ; preds = %3
%20 = load i8*, i8** %11
%21 = call i32 @clean_buff(i8* %20)
br label %22
22: ; preds = %53, %19
%23 = load i32, i32* %6
%24 = load i8*, i8** %11
%25 = call i32 @read(i32 %23, i8* %24, i32 1024)
store i32 %25, i32* %9
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = load i64, i64* %10
%29 = load i64, i64* %7
%30 = icmp sle i64 %28, %29
br label %31
31: ; preds = %27, %22
%32 = phi i1 [ false, %22 ], [ %30, %27 ]
br i1 %32, label %33, label %54
33: ; preds = %31
%34 = load i32, i32* %5
%35 = load i8*, i8** %11
%36 = load i32, i32* %9
%37 = call i32 @send_stuff(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %8
%38 = load i8*, i8** %11
%39 = call i32 @clean_buff(i8* %38)
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = load i64, i64* %10
%43 = add nsw i64 %42, %41
store i64 %43, i64* %10
%44 = load i64, i64* %10
%45 = load i64, i64* %7
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %33
br label %54
48: ; preds = %33
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %57
53: ; preds = %48
br label %22
54: ; preds = %47, %31
%55 = load i64, i64* %10
%56 = trunc i64 %55 to i32
store i32 %56, i32* %4
br label %57
57: ; preds = %54, %51, %16
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"ERROR 17: cannot allocate memory for buffer.\0A\00"
define i32 @send_file(i32 %0, i32 %1, i64 %2) {
%4 = tail call i64 @malloc(i32 1024)
%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 %29
10: ; preds = %3
%11 = tail call i32 @clean_buff(i8* %5)
br label %12
12: ; preds = %24, %10
%13 = phi i64 [ 0, %10 ], [ %22, %24 ]
%14 = tail call i32 @read(i32 %1, i8* %5, i32 1024)
%15 = icmp sgt i32 %14, 0
%16 = icmp sle i64 %13, %2
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = tail call i32 @send_stuff(i32 %0, i8* %5, i32 %14)
%20 = tail call i32 @clean_buff(i8* %5)
%21 = sext i32 %19 to i64
%22 = add nsw i64 %13, %21
%23 = icmp eq i64 %22, %2
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = icmp slt i32 %19, 0
br i1 %25, label %29, label %12
26: ; preds = %18, %12
%27 = phi i64 [ %2, %18 ], [ %13, %12 ]
%28 = trunc i64 %27 to i32
br label %29
29: ; preds = %24, %26, %7
%30 = phi i32 [ -1, %7 ], [ %28, %26 ], [ %19, %24 ]
ret i32 %30
}