#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*/ debug(int, char *);
int /*<<< orphan*/ socket_wait_write(int, int);
int write(int, char *, int);
int socket_write(int socket, void *buffer, int size, int timeout) {
  int sent;
  int writeval;
  for (sent = 0; sent < size;) {
    if (!socket_wait_write(socket, timeout)) {
      debug(2, "Socket write timed out.");
      return 0;
    }
    writeval = write(socket, ((char *)buffer) + sent, size - sent);
    if (writeval <= 0) {
      debug(2, "Write in socket write failed.");
      return 0;
    }
    sent += writeval;
  }
  return 1;
}


x86 assembly (clang, -O0):
<socket_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11da <socket_write+0xaa>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <socket_wait_write@plt>
cmp    $0x0,%eax
jne    1189 <socket_write+0x59>
mov    $0x2,%edi
lea    0xe88(%rip),%rsi
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <socket_write+0xb1>
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,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     11cc <socket_write+0x9c>
mov    $0x2,%edi
lea    0xe5d(%rip),%rsi
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <socket_write+0xb1>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <socket_write+0x1c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r15d
test   %edx,%edx
jle    11ab <socket_write+0x7b>
mov    %ecx,%r12d
mov    %edx,%ebx
mov    %rsi,%r13
mov    %edi,%ebp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r12d,%esi
call   1050 <socket_wait_write@plt>
test   %eax,%eax
je     118e <socket_write+0x5e>
movslq %r14d,%rax
lea    (%rax,%r13,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %ebp,%edi
call   1030 <write@plt>
test   %eax,%eax
jle    1197 <socket_write+0x67>
add    %eax,%r14d
cmp    %ebx,%r14d
jl     1160 <socket_write+0x30>
jmp    11ab <socket_write+0x7b>
lea    0xe6b(%rip),%rsi
jmp    119e <socket_write+0x6e>
lea    0xe7a(%rip),%rsi
mov    $0x2,%edi
call   1040 <debug@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [24 x i8] c"Socket write timed out.\00"
@.str.1 = [30 x i8] c"Write in socket write failed.\00"
define i32 @socket_write(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %8, label %22
6: ; preds = %12
%7 = icmp slt i32 %18, %2
br i1 %7, label %8, label %22
8: ; preds = %4, %6
%9 = phi i32 [ %18, %6 ], [ 0, %4 ]
%10 = tail call i32 @socket_wait_write(i32 %0, i32 %3)
%11 = icmp eq i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %8
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %1, i64 %13
%15 = sub nsw i32 %2, %9
%16 = tail call i32 @write(i32 %0, i8* %14, i32 %15)
%17 = icmp slt i32 %16, 1
%18 = add nsw i32 %16, %9
br i1 %17, label %19, label %6
19: ; preds = %12, %8
%20 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %12 ]
%21 = tail call i32 @debug(i32 2, i8* %20)
br label %22
22: ; preds = %6, %19, %4
%23 = phi i32 [ 1, %4 ], [ 0, %19 ], [ 1, %6 ]
ret i32 %23
}