#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*/ exit(int);
int /*<<< orphan*/ perror(char *);
int write(int, char const *, size_t);
void transmit(const char *buffer, size_t buf_size) {
  size_t buf_pos = 0;
  while (buf_pos < buf_size) {
    int size = write(1, buffer + buf_pos, buf_size - buf_pos);
    if (size == -1) {
      perror("write");
      exit(2);
    }
    buf_pos += size;
  }
}


x86 assembly (clang, -O0):
<transmit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a4 <transmit+0x74>
mov    -0x8(%rbp),%rsi
add    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
sub    -0x18(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1193 <transmit+0x63>
lea    0xe7c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
movslq -0x1c(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1148 <transmit+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transmit>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1173 <transmit+0x43>
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%r14,%rbx,1),%rsi
mov    %r15,%rdx
sub    %rbx,%rdx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1179 <transmit+0x49>
cltq
add    %rax,%rbx
cmp    %r15,%rbx
jb     1150 <transmit+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe80(%rip),%rdi
call   1040 <perror@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define void @transmit(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* %5
br label %7
7: ; preds = %24, %2
%8 = load i64, i64* %5
%9 = load i64, i64* %4
%10 = icmp ult i64 %8, %9
br i1 %10, label %11, label %29
11: ; preds = %7
%12 = load i8*, i8** %3
%13 = load i64, i64* %5
%14 = getelementptr inbounds i8, i8* %12, i64 %13
%15 = load i64, i64* %4
%16 = load i64, i64* %5
%17 = sub i64 %15, %16
%18 = call i32 @write(i32 1, i8* %14, i64 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %11
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 2)
unreachable
24: ; preds = %11
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = load i64, i64* %5
%28 = add i64 %27, %26
store i64 %28, i64* %5
br label %7
29: ; preds = %7
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define void @transmit(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %17, label %6
4: ; preds = %6
%5 = icmp ult i64 %13, %1
br i1 %5, label %6, label %17
6: ; preds = %2, %4
%7 = phi i64 [ %13, %4 ], [ 0, %2 ]
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = sub i64 %1, %7
%10 = tail call i32 @write(i32 1, i8* %8, i64 %9)
%11 = icmp eq i32 %10, -1
%12 = sext i32 %10 to i64
%13 = add i64 %7, %12
br i1 %11, label %14, label %4
14: ; preds = %6
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %4, %2
ret void
}