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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int write(int, ...);
int msg_write(int fd, char *buf, int len) {
  unsigned short msg_len = (unsigned short)len;
  int bytes_w = 0, bytes_w_tot = 0;
  if (write(fd, &msg_len, sizeof(msg_len)) < sizeof(msg_len)) {
    perror("Error writing msg len");
    return -1;
  }
  while (bytes_w_tot < msg_len) {
    if ((bytes_w = write(fd, buf, msg_len)) < 0) {
      perror("Error writing msg");
      return -1;
    }
    bytes_w_tot += bytes_w;
  }
  return 0;
}


x86 assembly (clang, -O0):
<msg_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %ax,-0x16(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0x2,%edx
mov    $0x0,%al
call   1030 <write@plt>
cltq
cmp    $0x2,%rax
jae    117e <msg_write+0x5e>
lea    0xe93(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <msg_write+0xbd>
jmp    1183 <msg_write+0x63>
mov    -0x20(%rbp),%eax
movzwl -0x16(%rbp),%ecx
cmp    %ecx,%eax
jge    11d6 <msg_write+0xb6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movzwl -0x16(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c8 <msg_write+0xa8>
lea    0xe5f(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <msg_write+0xbd>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1183 <msg_write+0x63>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msg_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
mov    %dx,0x6(%rsp)
xor    %r14d,%r14d
lea    0x6(%rsp),%rsi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x2,%eax
jb     118c <msg_write+0x6c>
movzwl 0x6(%rsp),%edx
test   %edx,%edx
je     117e <msg_write+0x5e>
lea    0xebc(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <write@plt>
test   %eax,%eax
js     1193 <msg_write+0x73>
add    %eax,%ebx
movzwl 0x6(%rsp),%edx
cmp    %edx,%ebx
jl     1160 <msg_write+0x40>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe6d(%rip),%r14
mov    %r14,%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    117e <msg_write+0x5e>

LLVM IR (-O0):
@.str = [22 x i8] c"Error writing msg len\00"
@.str.1 = [18 x i8] c"Error writing msg\00"
define i32 @msg_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i16
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = trunc i32 %11 to i16
store i16 %12, i16* %8
store i32 0, i32* %9
store i32 0, i32* %10
%13 = load i32, i32* %5
%14 = call i32 (i32, ...) @write(i32 %13, i16* %8, i64 2)
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %15, 2
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
19: ; preds = %3
br label %20
20: ; preds = %34, %19
%21 = load i32, i32* %10
%22 = load i16, i16* %8
%23 = zext i16 %22 to i32
%24 = icmp slt i32 %21, %23
br i1 %24, label %25, label %38
25: ; preds = %20
%26 = load i32, i32* %5
%27 = load i8*, i8** %6
%28 = load i16, i16* %8
%29 = zext i16 %28 to i32
%30 = call i32 (i32, ...) @write(i32 %26, i8* %27, i32 %29)
store i32 %30, i32* %9
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %39
34: ; preds = %25
%35 = load i32, i32* %9
%36 = load i32, i32* %10
%37 = add nsw i32 %36, %35
store i32 %37, i32* %10
br label %20
38: ; preds = %20
store i32 0, i32* %4
br label %39
39: ; preds = %38, %32, %17
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str = [22 x i8] c"Error writing msg len\00"
@.str.1 = [18 x i8] c"Error writing msg\00"
define i32 @msg_write(i32 %0, i8* %1, i32 %2) {
%4 = alloca i16
%5 = bitcast i16* %4 to i8*
%6 = trunc i32 %2 to i16
store i16 %6, i16* %4
%7 = call i32 (i32, ...) @write(i32 %0, i16* %4, i64 2)
%8 = icmp ult i32 %7, 2
br i1 %8, label %24, label %9
9: ; preds = %3
%10 = load i16, i16* %4
%11 = icmp eq i16 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %9
%13 = zext i16 %10 to i32
br label %18
14: ; preds = %18
%15 = load i16, i16* %4
%16 = zext i16 %15 to i32
%17 = icmp slt i32 %23, %16
br i1 %17, label %18, label %27
18: ; preds = %12, %14
%19 = phi i32 [ %16, %14 ], [ %13, %12 ]
%20 = phi i32 [ %23, %14 ], [ 0, %12 ]
%21 = call i32 (i32, ...) @write(i32 %0, i8* %1, i32 %19)
%22 = icmp slt i32 %21, 0
%23 = add nsw i32 %21, %20
br i1 %22, label %24, label %14
24: ; preds = %18, %3
%25 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), %18 ]
%26 = call i32 @perror(i8* %25)
br label %27
27: ; preds = %14, %24, %9
%28 = phi i32 [ 0, %9 ], [ -1, %24 ], [ 0, %14 ]
ret i32 %28
}