#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 write(int, char *, int);
int write_socket(int fd, char *buf, int len) {
  int done, todo = len;
  while (todo) {
    done = write(fd, buf, todo);
    if (done <= 0)
      return (done);
    buf += done;
    todo -= done;
  }
  return (len);
}


x86 assembly (clang, -O0):
<write_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     117a <write_socket+0x6a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1159 <write_socket+0x49>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1180 <write_socket+0x70>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1128 <write_socket+0x18>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     114c <write_socket+0x3c>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%r15d
mov    %edx,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
test   %eax,%eax
jle    1151 <write_socket+0x41>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jne    1130 <write_socket+0x20>
jmp    1154 <write_socket+0x44>
xor    %r14d,%r14d
jmp    1154 <write_socket+0x44>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_socket(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
%10 = load i32, i32* %7
store i32 %10, i32* %9
br label %11
11: ; preds = %23, %3
%12 = load i32, i32* %9
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %31
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %9
%18 = call i32 @write(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %33
23: ; preds = %14
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8* %27, i8** %6
%28 = load i32, i32* %8
%29 = load i32, i32* %9
%30 = sub nsw i32 %29, %28
store i32 %30, i32* %9
br label %11
31: ; preds = %11
%32 = load i32, i32* %7
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %21
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @write_socket(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %15, label %5
5: ; preds = %3, %10
%6 = phi i32 [ %13, %10 ], [ %2, %3 ]
%7 = phi i8* [ %12, %10 ], [ %1, %3 ]
%8 = tail call i32 @write(i32 %0, i8* %7, i32 %6)
%9 = icmp slt i32 %8, 1
br i1 %9, label %15, label %10
10: ; preds = %5
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %7, i64 %11
%13 = sub nsw i32 %6, %8
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %5
15: ; preds = %5, %10, %3
%16 = phi i32 [ 0, %3 ], [ %2, %10 ], [ %8, %5 ]
ret i32 %16
}