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


x86 assembly (clang, -O0):
<smtp_write_client_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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 <smtp_write_client_msg+0x3c>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0xa,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ae <smtp_write_client_msg+0x5e>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%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,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ec <smtp_write_client_msg+0x9c>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%ecx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    120b <smtp_write_client_msg+0xbb>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%ecx
mov    -0x18(%rbp),%r8
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jle    122e <smtp_write_client_msg+0xde>
movl   $0x1,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smtp_write_client_msg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %esi,%r15d
mov    %edi,%r14d
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
test   %rbp,%rbp
je     11b8 <smtp_write_client_msg+0x68>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r15d,0x4(%rsp)
mov    %r14d,%r15d
mov    %eax,%r14d
lea    (%r14,%rbx,1),%edi
add    $0x14,%edi
call   1070 <malloc@plt>
mov    %rax,%r13
lea    (%r14,%rbx,1),%esi
add    $0x13,%esi
mov    %r15d,%r14d
mov    %rax,%rdi
mov    %r12,%rdx
mov    0x4(%rsp),%ecx
mov    %rbp,%r8
xor    %eax,%eax
call   1060 <snprintf@plt>
jmp    11d8 <smtp_write_client_msg+0x88>
lea    0xa(%rbx),%edi
call   1070 <malloc@plt>
mov    %rax,%r13
add    $0x9,%ebx
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
add    $0x1,%eax
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %ebx,%ebx
test   %rax,%rax
setg   %bl
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @smtp_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*
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
%12 = load i8*, i8** %8
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %4
%15 = load i8*, i8** %7
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 10
store i32 %17, i32* %9
br label %25
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = call i32 @strlen(i8* %19)
%21 = load i8*, i8** %8
%22 = call i32 @strlen(i8* %21)
%23 = add nsw i32 %20, %22
%24 = add nsw i32 %23, 20
store i32 %24, i32* %9
br label %25
25: ; preds = %18, %14
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = mul i64 1, %27
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %11
%31 = load i8*, i8** %8
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %40
33: ; preds = %25
%34 = load i8*, i8** %11
%35 = load i32, i32* %9
%36 = sub nsw i32 %35, 1
%37 = load i8*, i8** %7
%38 = load i32, i32* %6
%39 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %34, i32 %36, i8* %37, i32 %38)
store i32 %39, i32* %9
br label %48
40: ; preds = %25
%41 = load i8*, i8** %11
%42 = load i32, i32* %9
%43 = sub nsw i32 %42, 1
%44 = load i8*, i8** %7
%45 = load i32, i32* %6
%46 = load i8*, i8** %8
%47 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %41, i32 %43, i8* %44, i32 %45, i8* %46)
store i32 %47, i32* %9
br label %48
48: ; preds = %40, %33
%49 = load i32, i32* %5
%50 = load i8*, i8** %11
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
%53 = call i64 @write(i32 %49, i8* %50, i32 %52)
%54 = icmp sgt i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %48
store i32 1, i32* %10
br label %56
56: ; preds = %55, %48
%57 = load i8*, i8** %11
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %10
ret i32 %59
}

LLVM IR (-O2):
define i32 @smtp_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 %12
7: ; preds = %4
%8 = add nsw i32 %6, 10
%9 = tail call i8* @malloc(i32 %8)
%10 = add nsw i32 %6, 9
%11 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %9, i32 %10, i8* %2, i32 %1)
br label %19
12: ; preds = %4
%13 = tail call i32 @strlen(i8* %3)
%14 = add nsw i32 %13, %6
%15 = add nsw i32 %14, 20
%16 = tail call i8* @malloc(i32 %15)
%17 = add nsw i32 %14, 19
%18 = tail call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %16, i32 %17, i8* %2, i32 %1, i8* %3)
br label %19
19: ; preds = %12, %7
%20 = phi i8* [ %9, %7 ], [ %16, %12 ]
%21 = phi i32 [ %11, %7 ], [ %18, %12 ]
%22 = add nsw i32 %21, 1
%23 = tail call i64 @write(i32 %0, i8* %20, i32 %22)
%24 = icmp sgt i64 %23, 0
%25 = zext i1 %24 to i32
%26 = tail call i32 @free(i8* %20)
ret i32 %25
}