#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*/ sprintf(char *, char *, int, ...);
int /*<<< orphan*/ strcat(char *, char *);
int strlen(char const *);
void BuildCommandMessage(char *msg, const char *cmd, int addr) {
  int sum;
  char *pChar;
  char s[8];
  sprintf(msg, "{FB;%02X;%02X|64:%s|", addr, strlen(cmd) + 19, cmd);
  sum = 0;
  pChar = msg + 1;
  while (*pChar != 0) {
    sum += (int)*pChar++;
  }
  sprintf(s, "%04X}", sum);
  strcat(msg, s);
}


x86 assembly (clang, -O0):
<BuildCommandMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    %eax,%ecx
add    $0x13,%ecx
mov    -0x10(%rbp),%r8
lea    0xe8f(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b8 <BuildCommandMessage+0x88>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    118b <BuildCommandMessage+0x5b>
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    0xe4f(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <strcat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BuildCommandMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r15
mov    %rsi,%rdi
call   1030 <strlen@plt>
lea    0x13(%rax),%ecx
lea    0xeae(%rip),%rsi
xor    %ebx,%ebx
mov    %r15,%rdi
mov    %r14d,%edx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <sprintf@plt>
lea    0x1(%r15),%rax
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edx
movsbl (%rax),%ecx
lea    (%rdx,%rcx,1),%ebx
add    $0x1,%rax
test   %ecx,%ecx
jne    1170 <BuildCommandMessage+0x40>
lea    0xe8e(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <sprintf@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   1040 <strcat@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"{FB;%02X;%02X|64:%s|\00"
@.str.1 = [6 x i8] c"%04X}\00"
define void @BuildCommandMessage(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [8 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %6
%12 = load i8*, i8** %5
%13 = call i32 @strlen(i8* %12)
%14 = add nsw i32 %13, 19
%15 = load i8*, i8** %5
%16 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %14, i8* %15)
store i32 0, i32* %7
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8* %18, i8** %8
br label %19
19: ; preds = %24, %3
%20 = load i8*, i8** %8
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load i8*, i8** %8
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %8
%27 = load i8, i8* %25
%28 = sext i8 %27 to i32
%29 = load i32, i32* %7
%30 = add nsw i32 %29, %28
store i32 %30, i32* %7
br label %19
31: ; preds = %19
%32 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%33 = load i32, i32* %7
%34 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i8*, i8** %4
%36 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%37 = call i32 @strcat(i8* %35, i8* %36)
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"{FB;%02X;%02X|64:%s|\00"
@.str.1 = [6 x i8] c"%04X}\00"
define void @BuildCommandMessage(i8* %0, i8* %1, i32 %2) {
%4 = alloca [8 x i8]
%5 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %1)
%7 = add nsw i32 %6, 19
%8 = tail call i32 (i8*, i8*, i32, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %7, i8* %1)
br label %9
9: ; preds = %9, %3
%10 = phi i32 [ 0, %3 ], [ %16, %9 ]
%11 = phi i8* [ %0, %3 ], [ %12, %9 ]
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
%15 = sext i8 %13 to i32
%16 = add nsw i32 %10, %15
br i1 %14, label %17, label %9
17: ; preds = %9
%18 = call i32 (i8*, i8*, i32, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%19 = call i32 @strcat(i8* %0, i8* %5)
ret void
}