#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*/ snprintf(char *, int, char *, int);
int strlen(char const *);
int writex(int, char const *, int);
int sendfailmsg(int fd, const char *reason) {
  char buf[9];
  int len;
  len = strlen(reason);
  if (len > 0xffff)
    len = 0xffff;
  snprintf(buf, sizeof buf, "FAIL%04x", len);
  if (writex(fd, buf, 8))
    return -1;
  return writex(fd, reason, len);
}


x86 assembly (clang, -O0):
<sendfailmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffff,-0x20(%rbp)
jle    115f <sendfailmsg+0x2f>
movl   $0xffff,-0x20(%rbp)
lea    -0x19(%rbp),%rdi
mov    -0x20(%rbp),%ecx
mov    $0x9,%esi
lea    0xe8e(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x8,%edx
call   1050 <writex@plt>
cmp    $0x0,%eax
je     119d <sendfailmsg+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11af <sendfailmsg+0x7f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1050 <writex@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfailmsg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    %rsi,%rdi
call   1030 <strlen@plt>
cmp    $0xffff,%eax
mov    $0xffff,%ebx
cmovl  %eax,%ebx
lea    0xea4(%rip),%rdx
lea    0xf(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x9,%esi
mov    %ebx,%ecx
call   1040 <snprintf@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1050 <writex@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    1198 <sendfailmsg+0x68>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <writex@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"FAIL%04x\00"
define i32 @sendfailmsg(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [9 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp sgt i32 %10, 65535
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 65535, i32* %7
br label %13
13: ; preds = %12, %2
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%15 = load i32, i32* %7
%16 = call i32 @snprintf(i8* %14, i32 9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %4
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%19 = call i32 @writex(i32 %17, i8* %18, i32 8)
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %13
store i32 -1, i32* %3
br label %27
22: ; preds = %13
%23 = load i32, i32* %4
%24 = load i8*, i8** %5
%25 = load i32, i32* %7
%26 = call i32 @writex(i32 %23, i8* %24, i32 %25)
store i32 %26, i32* %3
br label %27
27: ; preds = %22, %21
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [9 x i8] c"FAIL%04x\00"
define i32 @sendfailmsg(i32 %0, i8* %1) {
%3 = alloca [9 x i8]
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp slt i32 %5, 65535
%7 = select i1 %6, i32 %5, i32 65535
%8 = call i32 @snprintf(i8* %4, i32 9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @writex(i32 %0, i8* %4, i32 8)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @writex(i32 %0, i8* %1, i32 %7)
br label %13
13: ; preds = %2, %11
%14 = phi i32 [ %12, %11 ], [ -1, %2 ]
ret i32 %14
}