#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 read(int, char *, int);
int write_all(int, char *, int);
int send_file_socket(int confd, int fd, size_t size) {
  char buf[1024];
  size_t total;
  int rcount;
  int wcount;
  for (total = 0; total < size; total += wcount) {
    if ((rcount = read(fd, buf, 1024)) <= 0) {
      return rcount;
    }
    if ((wcount = write_all(confd, buf, rcount)) <= 0) {
      return wcount;
    }
  }
  return total;
}


x86 assembly (clang, -O0):
<send_file_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x428(%rbp)
mov    -0x428(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d3 <send_file_socket+0xb3>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,-0x42c(%rbp)
cmp    $0x0,%eax
jg     1182 <send_file_socket+0x62>
mov    -0x42c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <send_file_socket+0xbd>
mov    -0x8(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    -0x42c(%rbp),%edx
call   1040 <write_all@plt>
mov    %eax,-0x430(%rbp)
cmp    $0x0,%eax
jg     11b4 <send_file_socket+0x94>
mov    -0x430(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <send_file_socket+0xbd>
jmp    11b9 <send_file_socket+0x99>
movslq -0x430(%rbp),%rax
add    -0x428(%rbp),%rax
mov    %rax,-0x428(%rbp)
jmp    1140 <send_file_socket+0x20>
mov    -0x428(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_file_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
test   %rdx,%rdx
je     1183 <send_file_socket+0x63>
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %edi,%r14d
xor    %ebp,%ebp
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1185 <send_file_socket+0x65>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write_all@plt>
test   %eax,%eax
jle    1185 <send_file_socket+0x65>
mov    %eax,%eax
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1150 <send_file_socket+0x30>
mov    %ebp,%eax
jmp    1185 <send_file_socket+0x65>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_file_socket(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i64 0, i64* %9
br label %12
12: ; preds = %32, %3
%13 = load i64, i64* %9
%14 = load i64, i64* %7
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %37
16: ; preds = %12
%17 = load i32, i32* %6
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 1024)
store i32 %19, i32* %10
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* %10
store i32 %22, i32* %4
br label %40
23: ; preds = %16
%24 = load i32, i32* %5
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%26 = load i32, i32* %10
%27 = call i32 @write_all(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %11
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = load i32, i32* %11
store i32 %30, i32* %4
br label %40
31: ; preds = %23
br label %32
32: ; preds = %31
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = load i64, i64* %9
%36 = add i64 %35, %34
store i64 %36, i64* %9
br label %12
37: ; preds = %12
%38 = load i64, i64* %9
%39 = trunc i64 %38 to i32
store i32 %39, i32* %4
br label %40
40: ; preds = %37, %29, %21
%41 = load i32, i32* %4
ret i32 %41
}

LLVM IR (-O2):
define i32 @send_file_socket(i32 %0, i32 %1, i64 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = icmp eq i64 %2, 0
br i1 %6, label %20, label %9
7: ; preds = %13
%8 = icmp ult i64 %17, %2
br i1 %8, label %9, label %18
9: ; preds = %3, %7
%10 = phi i64 [ %17, %7 ], [ 0, %3 ]
%11 = call i32 @read(i32 %1, i8* %5, i32 1024)
%12 = icmp slt i32 %11, 1
br i1 %12, label %20, label %13
13: ; preds = %9
%14 = call i32 @write_all(i32 %0, i8* %5, i32 %11)
%15 = icmp slt i32 %14, 1
%16 = zext i32 %14 to i64
%17 = add i64 %10, %16
br i1 %15, label %20, label %7
18: ; preds = %7
%19 = trunc i64 %17 to i32
br label %20
20: ; preds = %13, %9, %3, %18
%21 = phi i32 [ 0, %3 ], [ %19, %18 ], [ %14, %13 ], [ %11, %9 ]
ret i32 %21
}