#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(size_t);
int send_msg(int, char *, size_t);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int send_ack(int fd, char reason, const char *error) {
  size_t len = 0;
  char *m = 0;
  int result = -1;
  if (reason == '0') {
    len = 3;
    m = malloc((len + 1) * sizeof(char));
    m[0] = '1';
    m[1] = '.';
    m[2] = '0';
    m[3] = 0;
  } else {
    len = strlen(error) + 4;
    m = malloc((len + 1) * sizeof(char));
    m[0] = 1;
    m[1] = '.';
    m[2] = reason;
    m[3] = '.';
    strncpy(m + 4, error, len - 4);
    m[len] = 0;
  }
  result = send_msg(fd, m, len);
  free(m);
  return result;
}


x86 assembly (clang, -O0):
<send_ack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movsbl -0x5(%rbp),%eax
cmp    $0x30,%eax
jne    11ca <send_ack+0x7a>
movq   $0x3,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x31,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x2e,0x1(%rax)
mov    -0x20(%rbp),%rax
movb   $0x30,0x2(%rax)
mov    -0x20(%rbp),%rax
movb   $0x0,0x3(%rax)
jmp    1237 <send_ack+0xe7>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x4,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x1,(%rax)
mov    -0x20(%rbp),%rax
movb   $0x2e,0x1(%rax)
mov    -0x5(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0x20(%rbp),%rax
movb   $0x2e,0x3(%rax)
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    $0x4,%rdx
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <send_msg@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_ack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
mov    %edi,%r14d
cmp    $0x30,%r13b
jne    1189 <send_ack+0x39>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movw   $0x2e31,(%rax)
movb   $0x30,0x2(%rax)
add    $0x3,%rax
mov    $0x3,%r15d
jmp    11cb <send_ack+0x7b>
mov    %rdx,%r12
mov    %rdx,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbp
lea    0x4(%rbp),%r15
lea    0x5(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,%rbx
movw   $0x2e01,(%rax)
mov    %r13b,0x2(%rax)
movb   $0x2e,0x3(%rax)
lea    0x4(%rax),%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1040 <strncpy@plt>
lea    (%rbx,%rbp,1),%rax
add    $0x4,%rax
movb   $0x0,(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <send_msg@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @send_ack(i32 %0, i8 signext %1, i8* %2) {
%4 = icmp eq i8 %1, 48
br i1 %4, label %5, label %10
5: ; preds = %3
%6 = tail call dereferenceable_or_null(4) i8* @malloc(i64 4)
store i8 49, i8* %6
%7 = getelementptr inbounds i8, i8* %6, i64 1
store i8 46, i8* %7
%8 = getelementptr inbounds i8, i8* %6, i64 2
store i8 48, i8* %8
%9 = getelementptr inbounds i8, i8* %6, i64 3
br label %23
10: ; preds = %3
%11 = tail call i32 @strlen(i8* %2)
%12 = add nsw i32 %11, 4
%13 = sext i32 %12 to i64
%14 = add nsw i64 %13, 1
%15 = tail call i8* @malloc(i64 %14)
store i8 1, i8* %15
%16 = getelementptr inbounds i8, i8* %15, i64 1
store i8 46, i8* %16
%17 = getelementptr inbounds i8, i8* %15, i64 2
store i8 %1, i8* %17
%18 = getelementptr inbounds i8, i8* %15, i64 3
store i8 46, i8* %18
%19 = getelementptr inbounds i8, i8* %15, i64 4
%20 = add nsw i64 %13, -4
%21 = tail call i32 @strncpy(i8* %19, i8* %2, i64 %20)
%22 = getelementptr inbounds i8, i8* %15, i64 %13
br label %23
23: ; preds = %10, %5
%24 = phi i8* [ %22, %10 ], [ %9, %5 ]
%25 = phi i64 [ %13, %10 ], [ 3, %5 ]
%26 = phi i8* [ %15, %10 ], [ %6, %5 ]
store i8 0, i8* %24
%27 = tail call i32 @send_msg(i32 %0, i8* %26, i64 %25)
%28 = tail call i32 @free(i8* %26)
ret i32 %27
}