#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*/ free(char *);
char *malloc(int);
int snprintf(char *, int, char const *, ...);
int strlen(char const *);
scalar_t__ write(int, char *, int);
int user_write_client_msg(int fd, int status, const char *msg, char *add) {
  int len, ret = 0;
  char *st, *buff;
  if (add == NULL) {
    len = strlen(msg) + 10;
  } else {
    len = strlen(msg) + strlen(add) + 20;
  }
  buff = malloc(sizeof(char) * len);
  if (status) {
    st = "FAIL";
    if (status == 1) {
      st = "OK";
    }
    if (add == NULL) {
      len = snprintf(buff, len - 1, msg, st);
    } else {
      len = snprintf(buff, len - 1, msg, st, add);
    }
  } else {
    if (add == NULL) {
      len = snprintf(buff, len - 1, msg);
    } else {
      len = snprintf(buff, len - 1, msg, add);
    }
  }
  if (write(fd, buff, len + 1) > 0) {
    ret = 1;
  }
  free(buff);
  return ret;
}


x86 assembly (clang, -O0):
<user_write_client_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118c <user_write_client_msg+0x3c>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0xa,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ae <user_write_client_msg+0x5e>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x14,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     123c <user_write_client_msg+0xec>
lea    0xe2e(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11eb <user_write_client_msg+0x9b>
lea    0xe1e(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1217 <user_write_client_msg+0xc7>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    1237 <user_write_client_msg+0xe7>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    1285 <user_write_client_msg+0x135>
cmpq   $0x0,-0x18(%rbp)
jne    1264 <user_write_client_msg+0x114>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    1280 <user_write_client_msg+0x130>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    1285 <user_write_client_msg+0x135>
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jle    12a8 <user_write_client_msg+0x158>
movl   $0x1,-0x20(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<user_write_client_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %esi,%r13d
mov    %edi,%r14d
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
test   %rbp,%rbp
je     11c9 <user_write_client_msg+0x79>
mov    %r14d,0x4(%rsp)
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbx,1),%r14d
lea    (%rax,%rbx,1),%edi
add    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %r13d,%r13d
je     1203 <user_write_client_msg+0xb3>
cmp    $0x1,%r13d
lea    0xe60(%rip),%rax
lea    0xe54(%rip),%rcx
cmove  %rax,%rcx
add    $0x13,%r14d
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
mov    %rbp,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    121a <user_write_client_msg+0xca>
lea    0xa(%rbx),%edi
call   1070 <malloc@plt>
mov    %rax,%r12
test   %r13d,%r13d
je     1250 <user_write_client_msg+0x100>
cmp    $0x1,%r13d
lea    0xe21(%rip),%rax
lea    0xe15(%rip),%rcx
cmove  %rax,%rcx
add    $0x9,%ebx
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    121f <user_write_client_msg+0xcf>
add    $0x13,%r14d
mov    %r12,%rdi
mov    %r14d,%esi
mov    %r15,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    0x4(%rsp),%r14d
add    $0x1,%eax
mov    %r14d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %ebx,%ebx
test   %rax,%rax
setg   %bl
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x9,%ebx
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    121f <user_write_client_msg+0xcf>

LLVM IR (-O0):
@.str = [5 x i8] c"FAIL\00"
@.str.1 = [3 x i8] c"OK\00"
define i32 @user_write_client_msg(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i32 0, i32* %10
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %4
%16 = load i8*, i8** %7
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 10
store i32 %18, i32* %9
br label %26
19: ; preds = %4
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = load i8*, i8** %8
%23 = call i32 @strlen(i8* %22)
%24 = add nsw i32 %21, %23
%25 = add nsw i32 %24, 20
store i32 %25, i32* %9
br label %26
26: ; preds = %19, %15
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = mul i64 1, %28
%30 = trunc i64 %29 to i32
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %12
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %57
34: ; preds = %26
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %11
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %34
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %11
br label %38
38: ; preds = %37, %34
%39 = load i8*, i8** %8
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %48
41: ; preds = %38
%42 = load i8*, i8** %12
%43 = load i32, i32* %9
%44 = sub nsw i32 %43, 1
%45 = load i8*, i8** %7
%46 = load i8*, i8** %11
%47 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %42, i32 %44, i8* %45, i8* %46)
store i32 %47, i32* %9
br label %56
48: ; preds = %38
%49 = load i8*, i8** %12
%50 = load i32, i32* %9
%51 = sub nsw i32 %50, 1
%52 = load i8*, i8** %7
%53 = load i8*, i8** %11
%54 = load i8*, i8** %8
%55 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %49, i32 %51, i8* %52, i8* %53, i8* %54)
store i32 %55, i32* %9
br label %56
56: ; preds = %48, %41
br label %74
57: ; preds = %26
%58 = load i8*, i8** %8
%59 = icmp eq i8* %58, null
br i1 %59, label %60, label %66
60: ; preds = %57
%61 = load i8*, i8** %12
%62 = load i32, i32* %9
%63 = sub nsw i32 %62, 1
%64 = load i8*, i8** %7
%65 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %61, i32 %63, i8* %64)
store i32 %65, i32* %9
br label %73
66: ; preds = %57
%67 = load i8*, i8** %12
%68 = load i32, i32* %9
%69 = sub nsw i32 %68, 1
%70 = load i8*, i8** %7
%71 = load i8*, i8** %8
%72 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %67, i32 %69, i8* %70, i8* %71)
store i32 %72, i32* %9
br label %73
73: ; preds = %66, %60
br label %74
74: ; preds = %73, %56
%75 = load i32, i32* %5
%76 = load i8*, i8** %12
%77 = load i32, i32* %9
%78 = add nsw i32 %77, 1
%79 = call i64 @write(i32 %75, i8* %76, i32 %78)
%80 = icmp sgt i64 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %74
store i32 1, i32* %10
br label %82
82: ; preds = %81, %74
%83 = load i8*, i8** %12
%84 = call i32 @free(i8* %83)
%85 = load i32, i32* %10
ret i32 %85
}

LLVM IR (-O2):
@.str = [5 x i8] c"FAIL\00"
@.str.1 = [3 x i8] c"OK\00"
define i32 @user_write_client_msg(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = icmp eq i8* %3, null
%6 = tail call i32 @strlen(i8* %2)
br i1 %5, label %7, label %11
7: ; preds = %4
%8 = add nsw i32 %6, 10
%9 = tail call i8* @malloc(i32 %8)
%10 = icmp eq i32 %1, 0
br i1 %10, label %27, label %17
11: ; preds = %4
%12 = tail call i32 @strlen(i8* %3)
%13 = add nsw i32 %12, %6
%14 = add nsw i32 %13, 20
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i32 %1, 0
br i1 %16, label %30, label %22
17: ; preds = %7
%18 = icmp eq i32 %1, 1
%19 = select i1 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0)
%20 = add nsw i32 %6, 9
%21 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %9, i32 %20, i8* %2, i8* %19)
br label %33
22: ; preds = %11
%23 = icmp eq i32 %1, 1
%24 = select i1 %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0)
%25 = add nsw i32 %13, 19
%26 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %15, i32 %25, i8* %2, i8* %24, i8* %3)
br label %33
27: ; preds = %7
%28 = add nsw i32 %6, 9
%29 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %9, i32 %28, i8* %2)
br label %33
30: ; preds = %11
%31 = add nsw i32 %13, 19
%32 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* %15, i32 %31, i8* %2, i8* %3)
br label %33
33: ; preds = %27, %30, %17, %22
%34 = phi i8* [ %9, %17 ], [ %15, %22 ], [ %9, %27 ], [ %15, %30 ]
%35 = phi i32 [ %21, %17 ], [ %26, %22 ], [ %29, %27 ], [ %32, %30 ]
%36 = add nsw i32 %35, 1
%37 = tail call i64 @write(i32 %0, i8* %34, i32 %36)
%38 = icmp sgt i64 %37, 0
%39 = zext i1 %38 to i32
%40 = tail call i32 @free(i8* %34)
ret i32 %39
}