#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*/ die();
int /*<<< orphan*/ err_msg(char const *);
void write_bytes(const char *name, unsigned char *buf, unsigned int buf_size,
                 unsigned int *bytes_written, const unsigned char *to_write,
                 unsigned int count) {
  unsigned int i;
  if (buf_size < (*bytes_written) + count) {
    err_msg("buf_size too small for ");
    err_msg(name);
    err_msg("\n");
    die();
  }
  for (i = 0; i < count; i++) {
    buf[(*bytes_written)++] = to_write[i];
  }
}


x86 assembly (clang, -O0):
<write_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    (%rcx),%ecx
add    -0x2c(%rbp),%ecx
cmp    %ecx,%eax
jae    117b <write_bytes+0x5b>
lea    0xea6(%rip),%rdi
call   1040 <err_msg@plt>
mov    -0x8(%rbp),%rdi
call   1040 <err_msg@plt>
lea    0xea9(%rip),%rdi
call   1040 <err_msg@plt>
mov    $0x0,%al
call   1030 <die@plt>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    11bc <write_bytes+0x9c>
mov    -0x28(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1182 <write_bytes+0x62>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r9d,%ebp
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rsi,%r14
mov    (%rcx),%eax
add    %r9d,%eax
cmp    %edx,%eax
jbe    1167 <write_bytes+0x47>
mov    %rdi,%r12
lea    0xeb9(%rip),%rdi
call   1040 <err_msg@plt>
mov    %r12,%rdi
call   1040 <err_msg@plt>
lea    0xebd(%rip),%rdi
call   1040 <err_msg@plt>
xor    %eax,%eax
call   1030 <die@plt>
test   %ebp,%ebp
je     11bf <write_bytes+0x9f>
mov    %ebp,%esi
cmp    $0x1,%ebp
jne    1176 <write_bytes+0x56>
xor    %ecx,%ecx
jmp    11aa <write_bytes+0x8a>
mov    %esi,%edx
and    $0xfffffffe,%edx
xor    %ecx,%ecx
nopl   (%rax)
movzbl (%r15,%rcx,1),%eax
mov    (%rbx),%edi
lea    0x1(%rdi),%ebp
mov    %ebp,(%rbx)
mov    %al,(%r14,%rdi,1)
movzbl 0x1(%r15,%rcx,1),%eax
mov    (%rbx),%edi
lea    0x1(%rdi),%ebp
mov    %ebp,(%rbx)
mov    %al,(%r14,%rdi,1)
add    $0x2,%rcx
cmp    %rcx,%rdx
jne    1180 <write_bytes+0x60>
test   $0x1,%sil
je     11bf <write_bytes+0x9f>
mov    (%r15,%rcx,1),%al
mov    (%rbx),%ecx
lea    0x1(%rcx),%edx
mov    %edx,(%rbx)
mov    %al,(%r14,%rcx,1)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"buf_size too small for \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @write_bytes(i8* %0, i8* %1, i32 %2, i32* %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i8* %4, i8** %11
store i32 %5, i32* %12
%14 = load i32, i32* %9
%15 = load i32*, i32** %10
%16 = load i32, i32* %15
%17 = load i32, i32* %12
%18 = add i32 %16, %17
%19 = icmp ult i32 %14, %18
br i1 %19, label %20, label %26
20: ; preds = %6
%21 = call i32 @err_msg(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %7
%23 = call i32 @err_msg(i8* %22)
%24 = call i32 @err_msg(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 (...) @die()
br label %26
26: ; preds = %20, %6
store i32 0, i32* %13
br label %27
27: ; preds = %43, %26
%28 = load i32, i32* %13
%29 = load i32, i32* %12
%30 = icmp ult i32 %28, %29
br i1 %30, label %31, label %46
31: ; preds = %27
%32 = load i8*, i8** %11
%33 = load i32, i32* %13
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = load i8*, i8** %8
%38 = load i32*, i32** %10
%39 = load i32, i32* %38
%40 = add i32 %39, 1
store i32 %40, i32* %38
%41 = zext i32 %39 to i64
%42 = getelementptr inbounds i8, i8* %37, i64 %41
store i8 %36, i8* %42
br label %43
43: ; preds = %31
%44 = load i32, i32* %13
%45 = add i32 %44, 1
store i32 %45, i32* %13
br label %27
46: ; preds = %27
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"buf_size too small for \00"
@.str.1 = [2 x i8] c"\0A\00"
define void @write_bytes(i8* %0, i8* nocapture writeonly %1, i32 %2, i32* nocapture %3, i8* nocapture %4, i32 %5) {
%7 = load i32, i32* %3
%8 = add i32 %7, %5
%9 = icmp ugt i32 %8, %2
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = tail call i32 @err_msg(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @err_msg(i8* %0)
%13 = tail call i32 @err_msg(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 (...) @die()
br label %15
15: ; preds = %10, %6
%16 = icmp eq i32 %5, 0
br i1 %16, label %52, label %17
17: ; preds = %15
%18 = zext i32 %5 to i64
%19 = and i64 %18, 1
%20 = icmp eq i32 %5, 1
br i1 %20, label %42, label %21
21: ; preds = %17
%22 = and i64 %18, 4294967294
br label %23
23: ; preds = %23, %21
%24 = phi i64 [ 0, %21 ], [ %39, %23 ]
%25 = phi i64 [ 0, %21 ], [ %40, %23 ]
%26 = getelementptr inbounds i8, i8* %4, i64 %24
%27 = load i8, i8* %26
%28 = load i32, i32* %3
%29 = add i32 %28, 1
store i32 %29, i32* %3
%30 = zext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 %27, i8* %31
%32 = or i64 %24, 1
%33 = getelementptr inbounds i8, i8* %4, i64 %32
%34 = load i8, i8* %33
%35 = load i32, i32* %3
%36 = add i32 %35, 1
store i32 %36, i32* %3
%37 = zext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
store i8 %34, i8* %38
%39 = add nuw nsw i64 %24, 2
%40 = add i64 %25, 2
%41 = icmp eq i64 %40, %22
br i1 %41, label %42, label %23
42: ; preds = %23, %17
%43 = phi i64 [ 0, %17 ], [ %39, %23 ]
%44 = icmp eq i64 %19, 0
br i1 %44, label %52, label %45
45: ; preds = %42
%46 = getelementptr inbounds i8, i8* %4, i64 %43
%47 = load i8, i8* %46
%48 = load i32, i32* %3
%49 = add i32 %48, 1
store i32 %49, i32* %3
%50 = zext i32 %48 to i64
%51 = getelementptr inbounds i8, i8* %1, i64 %50
store i8 %47, i8* %51
br label %52
52: ; preds = %45, %42, %15
ret void
}