#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*/ perror(char *);
scalar_t__ tcp_wait_for_socket(int, int, int /*<<< orphan*/);
int write(int, char *, int);
int smtp_send_data(int fd, char *buffer, int bufferlen) {
  int len;
  int outlen = 0;
  while (1) {
    if (tcp_wait_for_socket(fd, 5, 0) <= 0)
      return (-1);
    len = write(fd, buffer + outlen, bufferlen - outlen);
    if (len <= 0) {
      perror("SMTP_SEND_DATA (WRITE)\n");
      return (-1);
    }
    if (len == (bufferlen - outlen))
      break;
    outlen += len;
  }
  return (0);
}


x86 assembly (clang, -O0):
<smtp_send_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
xor    %edx,%edx
call   1040 <tcp_wait_for_socket@plt>
cmp    $0x0,%rax
jg     116e <smtp_send_data+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <smtp_send_data+0xa7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11ac <smtp_send_data+0x7c>
lea    0xe65(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <smtp_send_data+0xa7>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jne    11c2 <smtp_send_data+0x92>
jmp    11d0 <smtp_send_data+0xa0>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1149 <smtp_send_data+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smtp_send_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r12
mov    %edi,%r13d
xor    %ebp,%ebp
mov    $0x5,%esi
xor    %edx,%edx
call   1040 <tcp_wait_for_socket@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
jle    1199 <smtp_send_data+0x69>
nopl   (%rax)
movslq %ebp,%rax
lea    (%r12,%rax,1),%rsi
mov    %r15d,%ebx
sub    %eax,%ebx
mov    %r13d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    11ab <smtp_send_data+0x7b>
cmp    %ebx,%eax
je     1196 <smtp_send_data+0x66>
add    %eax,%ebp
mov    %r13d,%edi
mov    $0x5,%esi
xor    %edx,%edx
call   1040 <tcp_wait_for_socket@plt>
test   %rax,%rax
jg     1160 <smtp_send_data+0x30>
jmp    1199 <smtp_send_data+0x69>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4e(%rip),%rdi
call   1050 <perror@plt>
jmp    1199 <smtp_send_data+0x69>

LLVM IR (-O0):
@.str = [24 x i8] c"SMTP_SEND_DATA (WRITE)\0A\00"
define i32 @smtp_send_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %10
10: ; preds = %3, %36
%11 = load i32, i32* %5
%12 = call i64 @tcp_wait_for_socket(i32 %11, i32 5, i32 0)
%13 = icmp sle i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 -1, i32* %4
br label %41
15: ; preds = %10
%16 = load i32, i32* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %9
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %7
%22 = load i32, i32* %9
%23 = sub nsw i32 %21, %22
%24 = call i32 @write(i32 %16, i8* %20, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %15
%28 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %41
29: ; preds = %15
%30 = load i32, i32* %8
%31 = load i32, i32* %7
%32 = load i32, i32* %9
%33 = sub nsw i32 %31, %32
%34 = icmp eq i32 %30, %33
br i1 %34, label %35, label %36
35: ; preds = %29
br label %40
36: ; preds = %29
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = add nsw i32 %38, %37
store i32 %39, i32* %9
br label %10
40: ; preds = %35
store i32 0, i32* %4
br label %41
41: ; preds = %40, %27, %14
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
@.str = [24 x i8] c"SMTP_SEND_DATA (WRITE)\0A\00"
define i32 @smtp_send_data(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i64 @tcp_wait_for_socket(i32 %0, i32 5, i32 0)
%5 = icmp slt i64 %4, 1
br i1 %5, label %21, label %10
6: ; preds = %19
%7 = add nsw i32 %15, %11
%8 = tail call i64 @tcp_wait_for_socket(i32 %0, i32 5, i32 0)
%9 = icmp slt i64 %8, 1
br i1 %9, label %21, label %10
10: ; preds = %3, %6
%11 = phi i32 [ %7, %6 ], [ 0, %3 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = sub nsw i32 %2, %11
%15 = tail call i32 @write(i32 %0, i8* %13, i32 %14)
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %21
19: ; preds = %10
%20 = icmp eq i32 %15, %14
br i1 %20, label %21, label %6
21: ; preds = %6, %19, %3, %17
%22 = phi i32 [ -1, %17 ], [ -1, %3 ], [ -1, %6 ], [ 0, %19 ]
ret i32 %22
}