#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*/ buf;

/* Variables and functions */
int snprintf(char *, int, char *, int);
int socket_write(int, char const *, int);
int strlen(char *);
int http_chunk_send(int fd, const char *data, int datalen) {
  int n;
  char buf[11];
  n = snprintf(buf, sizeof(buf),
               "%x"
               "\r\n",
               datalen);
  socket_write(fd, buf, n);
  if (datalen > 0)
    n += socket_write(fd, data, datalen);
  return n + socket_write((fd), ("\r\n"), strlen("\r\n"));
}


x86 assembly (clang, -O0):
<http_chunk_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x23(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0xb,%esi
lea    0xeab(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x23(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <socket_write@plt>
cmpl   $0x0,-0x14(%rbp)
jle    118b <http_chunk_send+0x5b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <socket_write@plt>
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    0xe64(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xe53(%rip),%rsi
call   1050 <socket_write@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<http_chunk_send>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r15
mov    %edi,%r14d
lea    0xeb5(%rip),%rdx
lea    0x5(%rsp),%r12
mov    %r12,%rdi
mov    $0xb,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <socket_write@plt>
test   %ebx,%ebx
jle    1181 <http_chunk_send+0x51>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <socket_write@plt>
add    %eax,%ebp
lea    0xe7a(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <socket_write@plt>
add    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 @http_chunk_send(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [11 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 0
%10 = load i32, i32* %6
%11 = call i32 @snprintf(i8* %9, i32 11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %4
%13 = getelementptr inbounds [11 x i8], [11 x i8]* %8, i64 0, i64 0
%14 = load i32, i32* %7
%15 = call i32 @socket_write(i32 %12, i8* %13, i32 %14)
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %3
%19 = load i32, i32* %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i32 @socket_write(i32 %19, i8* %20, i32 %21)
%23 = load i32, i32* %7
%24 = add nsw i32 %23, %22
store i32 %24, i32* %7
br label %25
25: ; preds = %18, %3
%26 = load i32, i32* %7
%27 = load i32, i32* %4
%28 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @socket_write(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = add nsw i32 %26, %29
ret i32 %30
}

LLVM IR (-O2):
@.str = [5 x i8] c"%x\0D\0A\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i32 @http_chunk_send(i32 %0, i8* %1, i32 %2) {
%4 = alloca [11 x i8]
%5 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%6 = call i32 @snprintf(i8* %5, i32 11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %2)
%7 = call i32 @socket_write(i32 %0, i8* %5, i32 %6)
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %12
9: ; preds = %3
%10 = call i32 @socket_write(i32 %0, i8* %1, i32 %2)
%11 = add nsw i32 %10, %6
br label %12
12: ; preds = %9, %3
%13 = phi i32 [ %11, %9 ], [ %6, %3 ]
%14 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @socket_write(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = add nsw i32 %15, %13
ret i32 %16
}