#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ write(int, int /*<<< orphan*/ *, size_t);
int write_fd(int sock, char *buffer, size_t number_of_bytes) {
  size_t bytes_left_to_write;
  size_t total_bytes_written = 0;
  bytes_left_to_write = number_of_bytes;
  do {
    ssize_t bytes_written = 0;
    bytes_written = write(sock, ((uint8_t *)buffer) + total_bytes_written,
                          bytes_left_to_write);
    if (bytes_written > 0) {
      total_bytes_written += bytes_written;
      bytes_left_to_write -= bytes_written;
    } else if ((errno != EINTR))
      break;
  } while (bytes_left_to_write > 0);
  return number_of_bytes - bytes_left_to_write;
}


x86 assembly (clang, -O0):
<write_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    1185 <write_fd+0x75>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    11a9 <write_fd+0x99>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
je     11a4 <write_fd+0x94>
jmp    11b9 <write_fd+0xa9>
jmp    11a9 <write_fd+0x99>
jmp    11ae <write_fd+0x9e>
cmpq   $0x0,-0x20(%rbp)
ja     1133 <write_fd+0x23>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
mov    0x2eb3(%rip),%r13
mov    0x2ea4(%rip),%r14
mov    %rdx,(%rsp)
jmp    114b <write_fd+0x3b>
nopw   0x0(%rax,%rax,1)
add    %rax,%rbp
sub    %rax,%rbx
test   %rbx,%rbx
je     116a <write_fd+0x5a>
lea    (%r15,%rbp,4),%rsi
mov    %r12d,%edi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
jg     1140 <write_fd+0x30>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
je     1146 <write_fd+0x36>
jmp    116c <write_fd+0x5c>
xor    %ebx,%ebx
mov    (%rsp),%rax
sub    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_fd(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i64 0, i64* %8
%10 = load i64, i64* %6
store i64 %10, i64* %7
br label %11
11: ; preds = %35, %3
store i64 0, i64* %9
%12 = load i32, i32* %4
%13 = load i8*, i8** %5
%14 = bitcast i8* %13 to i32*
%15 = load i64, i64* %8
%16 = getelementptr inbounds i32, i32* %14, i64 %15
%17 = load i64, i64* %7
%18 = call i64 @write(i32 %12, i32* %16, i64 %17)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %11
%22 = load i64, i64* %9
%23 = load i64, i64* %8
%24 = add i64 %23, %22
store i64 %24, i64* %8
%25 = load i64, i64* %9
%26 = load i64, i64* %7
%27 = sub i64 %26, %25
store i64 %27, i64* %7
br label %34
28: ; preds = %11
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %28
br label %38
33: ; preds = %28
br label %34
34: ; preds = %33, %21
br label %35
35: ; preds = %34
%36 = load i64, i64* %7
%37 = icmp ugt i64 %36, 0
br i1 %37, label %11, label %38
38: ; preds = %35, %32
%39 = load i64, i64* %6
%40 = load i64, i64* %7
%41 = sub i64 %39, %40
%42 = trunc i64 %41 to i32
ret i32 %42
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_fd(i32 %0, i8* nocapture %1, i64 %2) {
%4 = bitcast i8* %1 to i32*
br label %5
5: ; preds = %18, %3
%6 = phi i64 [ %2, %3 ], [ %19, %18 ]
%7 = phi i64 [ 0, %3 ], [ %20, %18 ]
%8 = getelementptr inbounds i32, i32* %4, i64 %7
%9 = tail call i64 @write(i32 %0, i32* %8, i64 %6)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = add i64 %9, %7
%13 = sub i64 %6, %9
br label %18
14: ; preds = %5
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %22
18: ; preds = %11, %14
%19 = phi i64 [ %13, %11 ], [ %6, %14 ]
%20 = phi i64 [ %12, %11 ], [ %7, %14 ]
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %5
22: ; preds = %14, %18
%23 = phi i64 [ 0, %18 ], [ %6, %14 ]
%24 = sub i64 %2, %23
%25 = trunc i64 %24 to i32
ret i32 %25
}