#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 */
unsigned int write(int, ...);
int write_msg(char *buf, unsigned int len) {
  unsigned char c;
  c = (len >> 8) & 0xff;
  if (write(1, &c, 1) != 1) {
    return -1;
  }
  c = len & 0xff;
  if (write(1, &c, 1) != 1) {
    return -1;
  }
  if (write(1, buf, len) != len) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x15(%rbp)
mov    $0x1,%edx
lea    -0x15(%rbp),%rsi
mov    %edx,%edi
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x1,%eax
je     1154 <write_msg+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <write_msg+0xa5>
mov    -0x14(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x15(%rbp)
mov    $0x1,%edx
lea    -0x15(%rbp),%rsi
mov    %edx,%edi
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x1,%eax
je     1186 <write_msg+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <write_msg+0xa5>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
mov    $0x0,%al
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     11ae <write_msg+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b5 <write_msg+0xa5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_msg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %bh,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
mov    $0xffffffff,%ebp
cmp    $0x1,%eax
jne    117b <write_msg+0x6b>
mov    %bl,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    $0x1,%edi
mov    $0x1,%edx
xor    %eax,%eax
call   1030 <write@plt>
cmp    $0x1,%eax
jne    117b <write_msg+0x6b>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    %ebx,%eax
setne  %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_msg(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = lshr i32 %7, 8
%9 = and i32 %8, 255
%10 = trunc i32 %9 to i8
store i8 %10, i8* %6
%11 = call i32 (i32, ...) @write(i32 1, i8* %6, i32 1)
%12 = icmp ne i32 %11, 1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %29
14: ; preds = %2
%15 = load i32, i32* %5
%16 = and i32 %15, 255
%17 = trunc i32 %16 to i8
store i8 %17, i8* %6
%18 = call i32 (i32, ...) @write(i32 1, i8* %6, i32 1)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %3
br label %29
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = call i32 (i32, ...) @write(i32 1, i8* %22, i32 %23)
%25 = load i32, i32* %5
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %3
br label %29
28: ; preds = %21
store i32 0, i32* %3
br label %29
29: ; preds = %28, %27, %20, %13
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @write_msg(i8* %0, i32 %1) {
%3 = alloca i8
%4 = lshr i32 %1, 8
%5 = trunc i32 %4 to i8
store i8 %5, i8* %3
%6 = call i32 (i32, ...) @write(i32 1, i8* %3, i32 1)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %16
8: ; preds = %2
%9 = trunc i32 %1 to i8
store i8 %9, i8* %3
%10 = call i32 (i32, ...) @write(i32 1, i8* %3, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = call i32 (i32, ...) @write(i32 1, i8* %0, i32 %1)
%14 = icmp ne i32 %13, %1
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %12, %8, %2
%17 = phi i32 [ -1, %2 ], [ -1, %8 ], [ %15, %12 ]
ret i32 %17
}