#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 ENOMEM;
int errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int write(int const, char const *, int const);
void send(const int fd, const char *buf, const int len) {
  int res;
  res = write(fd, buf, len);
  if (res < 0) {
    printf("Error: %d\n", errno);
    perror("write");
  } else if (res == len) {
  } else {
    errno = ENOMEM;
    printf("write: %d were written instead of %d.\n", res, len);
  }
}


x86 assembly (clang, -O0):
<send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1186 <send+0x56>
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8f(%rip),%rdi
call   1050 <perror@plt>
jmp    11c2 <send+0x92>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1197 <send+0x67>
jmp    11bd <send+0x8d>
mov    0x2e42(%rip),%rax
mov    (%rax),%ecx
mov    0x2e31(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11c2 <send+0x92>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send>:
push   %rbx
mov    %edx,%ebx
call   1030 <write@plt>
test   %eax,%eax
js     1167 <send+0x37>
cmp    %ebx,%eax
jne    1142 <send+0x12>
pop    %rbx
ret
mov    0x2e97(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e86(%rip),%rdx
mov    %ecx,(%rdx)
lea    0xeb6(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
lea    0xe89(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe86(%rip),%rdi
pop    %rbx
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Error: %d\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"write\00"
@ENOMEM = global i32 0
@.str.2 = [39 x i8] c"write: %d were written instead of %d.\0A\00"
define void @send(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = call i32 @write(i32 %8, i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @errno
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
18: ; preds = %3
%19 = load i32, i32* %7
%20 = load i32, i32* %6
%21 = icmp eq i32 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %28
23: ; preds = %18
%24 = load i32, i32* @ENOMEM
store i32 %24, i32* @errno
%25 = load i32, i32* %7
%26 = load i32, i32* %6
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26)
br label %28
28: ; preds = %23, %22
br label %29
29: ; preds = %28, %14
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"Error: %d\0A\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"write\00"
@ENOMEM = global i32 0
@.str.2 = [39 x i8] c"write: %d were written instead of %d.\0A\00"
define void @send(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @write(i32 %0, i8* %1, i32 %2)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %15
10: ; preds = %3
%11 = icmp eq i32 %4, %2
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = load i32, i32* @ENOMEM
store i32 %13, i32* @errno
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %4, i32 %2)
br label %15
15: ; preds = %12, %10, %6
ret void
}