#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;

/* Variables and functions */
int /*<<< orphan*/ tcdrain(int);
int write(int, int /*<<< orphan*/ const *, size_t);
int msg_write(int fd, const uint8_t *msg, size_t len) {
  while (len) {
    int r = write(fd, msg, len);
    if (r < 0)
      return r;
    msg += r;
    len -= r;
  }
  tcdrain(fd);
  return 0;
}


x86 assembly (clang, -O0):
<msg_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118f <msg_write+0x6f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1166 <msg_write+0x46>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <msg_write+0x7e>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1133 <msg_write+0x13>
mov    -0x8(%rbp),%edi
call   1030 <tcdrain@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msg_write>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r14d
nopl   (%rax)
test   %rbx,%rbx
je     1154 <msg_write+0x34>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
movslq %eax,%rcx
sub    %rcx,%rbx
lea    0x0(%rbp,%rcx,4),%rbp
test   %eax,%eax
jns    1130 <msg_write+0x10>
jmp    115e <msg_write+0x3e>
mov    %r14d,%edi
call   1030 <tcdrain@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @msg_write(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %21, %3
%10 = load i64, i64* %7
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i32*, i32** %6
%15 = load i64, i64* %7
%16 = call i32 @write(i32 %13, i32* %14, i64 %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = load i32, i32* %8
store i32 %20, i32* %4
br label %33
21: ; preds = %12
%22 = load i32, i32* %8
%23 = load i32*, i32** %6
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i32, i32* %23, i64 %24
store i32* %25, i32** %6
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = load i64, i64* %7
%29 = sub i64 %28, %27
store i64 %29, i64* %7
br label %9
30: ; preds = %9
%31 = load i32, i32* %5
%32 = call i32 @tcdrain(i32 %31)
store i32 0, i32* %4
br label %33
33: ; preds = %30, %19
%34 = load i32, i32* %4
ret i32 %34
}

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