#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 strlen(char const *);
int write(int, char const *, int);
int send_tcp_message(int sockfd, const char *message) {
  int remaining = strlen(message), written = 0;
  while (remaining > 0) {
    if ((written = write(sockfd, message, remaining)) < 0) {
      return -1;
    }
    remaining -= written;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_tcp_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1183 <send_tcp_message+0x63>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1173 <send_tcp_message+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    118a <send_tcp_message+0x6a>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <send_tcp_message+0x22>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_tcp_message>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %rsi,%rdi
call   1040 <strlen@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    115f <send_tcp_message+0x3f>
mov    %eax,%ebp
xchg   %ax,%ax
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1159 <send_tcp_message+0x39>
sub    %eax,%ebp
test   %ebp,%ebp
jg     1140 <send_tcp_message+0x20>
jmp    115f <send_tcp_message+0x3f>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @send_tcp_message(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
store i32 0, i32* %7
br label %10
10: ; preds = %20, %2
%11 = load i32, i32* %6
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %3
br label %25
20: ; preds = %13
%21 = load i32, i32* %7
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, %21
store i32 %23, i32* %6
br label %10
24: ; preds = %10
store i32 0, i32* %3
br label %25
25: ; preds = %24, %19
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
define i32 @send_tcp_message(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %7, label %12
5: ; preds = %7
%6 = icmp sgt i32 %11, 0
br i1 %6, label %7, label %12
7: ; preds = %2, %5
%8 = phi i32 [ %11, %5 ], [ %3, %2 ]
%9 = tail call i32 @write(i32 %0, i8* %1, i32 %8)
%10 = icmp slt i32 %9, 0
%11 = sub nsw i32 %8, %9
br i1 %10, label %12, label %5
12: ; preds = %7, %5, %2
%13 = phi i32 [ 0, %2 ], [ 0, %5 ], [ -1, %7 ]
ret i32 %13
}