#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 dprintf(int, char *, ...);
int writeall(int, char const *, int);
int writechunk(int fd, const char *data, int count) {
  int n;
  n = dprintf(fd, "%x\r\n", count);
  if (n < 0) {
    return n;
  }
  n = writeall(fd, data, count);
  if (n < 0) {
    return n;
  }
  n = dprintf(fd, "\r\n");
  if (n < 0) {
    return n;
  }
  return count;
}


x86 assembly (clang, -O0):
<writechunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%edx
lea    0xec1(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    115e <writechunk+0x3e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <writechunk+0x94>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <writeall@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1185 <writechunk+0x65>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <writechunk+0x94>
mov    -0x8(%rbp),%edi
lea    0xe73(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ae <writechunk+0x8e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b4 <writechunk+0x94>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writechunk>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xece(%rip),%rsi
xor    %eax,%eax
call   1040 <dprintf@plt>
test   %eax,%eax
js     1164 <writechunk+0x44>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <writeall@plt>
test   %eax,%eax
js     1164 <writechunk+0x44>
lea    0xeae(%rip),%rsi
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <dprintf@plt>
test   %eax,%eax
cmovs  %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%x\0D\0A\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i32 @writechunk(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32, i32* %7
%11 = call i32 (i32, i8*, ...) @dprintf(i32 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = load i32, i32* %8
store i32 %15, i32* %4
br label %34
16: ; preds = %3
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %7
%20 = call i32 @writeall(i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = load i32, i32* %8
store i32 %24, i32* %4
br label %34
25: ; preds = %16
%26 = load i32, i32* %5
%27 = call i32 (i32, i8*, ...) @dprintf(i32 %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %34
32: ; preds = %25
%33 = load i32, i32* %7
store i32 %33, i32* %4
br label %34
34: ; preds = %32, %30, %23, %14
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [5 x i8] c"%x\0D\0A\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i32 @writechunk(i32 %0, i8* %1, i32 %2) {
%4 = tail call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %13, label %6
6: ; preds = %3
%7 = tail call i32 @writeall(i32 %0, i8* %1, i32 %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call i32 (i32, i8*, ...) @dprintf(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp slt i32 %10, 0
%12 = select i1 %11, i32 %10, i32 %2
br label %13
13: ; preds = %9, %6, %3
%14 = phi i32 [ %4, %3 ], [ %7, %6 ], [ %12, %9 ]
ret i32 %14
}