#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 /*<<< orphan*/ printf(char *, ...);
size_t write(int, ...);
int send_msg(int fd, const char *msg, size_t len) {
  printf("TRACE: write a message \"%s\" with len: %zu!\n", msg, len);
  if (write(fd, &len, sizeof(size_t)) != sizeof(size_t)) {
    printf("ERROR: Cannot write length!\n");
    return 1;
  }
  if (write(fd, msg, len) != len) {
    printf("ERROR: Cannot write exact message!\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<send_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1030 <write@plt>
cmp    $0x8,%rax
je     1180 <send_msg+0x60>
lea    0xebf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11bd <send_msg+0x9d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    $0x0,%al
call   1030 <write@plt>
cmp    -0x18(%rbp),%rax
je     11b6 <send_msg+0x96>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11bd <send_msg+0x9d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_msg>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rdx,0x8(%rsp)
lea    0xeb7(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
mov    %ebp,%edi
xor    %eax,%eax
call   1040 <write@plt>
cmp    $0x8,%rax
jne    118d <send_msg+0x5d>
mov    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <write@plt>
cmp    0x8(%rsp),%rax
je     119f <send_msg+0x6f>
lea    0xea1(%rip),%rdi
jmp    1194 <send_msg+0x64>
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"TRACE: write a message \22%s\22 with len: %zu!\0A\00"
@.str.1 = [29 x i8] c"ERROR: Cannot write length!\0A\00"
@.str.2 = [36 x i8] c"ERROR: Cannot write exact message!\0A\00"
define i32 @send_msg(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%8 = load i8*, i8** %6
%9 = load i64, i64* %7
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %8, i64 %9)
%11 = load i32, i32* %5
%12 = call i64 (i32, ...) @write(i32 %11, i64* %7, i64 8)
%13 = icmp ne i64 %12, 8
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %26
16: ; preds = %3
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = call i64 (i32, ...) @write(i32 %17, i8* %18, i64 %19)
%21 = load i64, i64* %7
%22 = icmp ne i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %26
25: ; preds = %16
store i32 0, i32* %4
br label %26
26: ; preds = %25, %23, %14
%27 = load i32, i32* %4
ret i32 %27
}

LLVM IR (-O2):
@.str = [44 x i8] c"TRACE: write a message \22%s\22 with len: %zu!\0A\00"
@str = [35 x i8] c"ERROR: Cannot write exact message!\00"
@str.3 = [28 x i8] c"ERROR: Cannot write length!\00"
define i32 @send_msg(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
store i64 %2, i64* %4
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %1, i64 %2)
%6 = call i64 (i32, ...) @write(i32 %0, i64* %4, i64 8)
%7 = icmp eq i64 %6, 8
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = load i64, i64* %4
%10 = call i64 (i32, ...) @write(i32 %0, i8* %1, i64 %9)
%11 = load i64, i64* %4
%12 = icmp eq i64 %10, %11
br i1 %12, label %16, label %13
13: ; preds = %8, %3
%14 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @str.3, i64 0, i64 0), %3 ], [ getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0), %8 ]
%15 = call i32 @puts(i8* dereferenceable(1) %14)
br label %16
16: ; preds = %13, %8
%17 = phi i32 [ 0, %8 ], [ 1, %13 ]
ret i32 %17
}