#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 *);
unsigned int iDoRemoteFunc(unsigned char *, unsigned int, int);
unsigned char iGetParaInt(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, unsigned char *, unsigned int);
unsigned int sprintf(char *, char *, ...);
int /*<<< orphan*/ vWriteLogTxt(char *, unsigned int);
unsigned char _ucRcAT1608_Write(unsigned char Level, unsigned char Addr,
                                unsigned int Len, unsigned char *DataBuf) {
  unsigned int uiDataLen, uiRet;
  char *psFunCall = malloc(1024);
  uiDataLen =
      sprintf(psFunCall, "%u(%u,%u,%u,p%04u", 9505, Level, Addr, Len, Len);
  memcpy(psFunCall + uiDataLen, DataBuf, Len);
  uiDataLen += Len;
  uiDataLen += sprintf(psFunCall + uiDataLen, ")");
  uiRet = iDoRemoteFunc((unsigned char *)psFunCall, uiDataLen, 8);
  free(psFunCall);
  if (uiRet) {
    vWriteLogTxt("_AT1608_Write iDoRemoteFunc ret = %d", uiRet);
    return 1;
  }
  return iGetParaInt(0);
}


x86 assembly (clang, -O0):
<_ucRcAT1608_Write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %dil,%sil
mov    %sil,-0x2(%rbp)
mov    %al,-0x3(%rbp)
mov    %edx,-0x8(%rbp)
mov    %rcx,-0x10(%rbp)
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
movzbl -0x2(%rbp),%ecx
movzbl -0x3(%rbp),%r8d
mov    -0x8(%rbp),%r9d
mov    -0x8(%rbp),%eax
lea    0xe4b(%rip),%rsi
mov    $0x2521,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%eax
add    %rax,%rdi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x8,%edx
call   1080 <iDoRemoteFunc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x18(%rbp)
je     1243 <_ucRcAT1608_Write+0xd3>
mov    -0x18(%rbp),%esi
lea    0xddf(%rip),%rdi
call   1050 <vWriteLogTxt@plt>
movb   $0x1,-0x1(%rbp)
jmp    124d <_ucRcAT1608_Write+0xdd>
xor    %edi,%edi
call   1040 <iGetParaInt@plt>
mov    %al,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_ucRcAT1608_Write>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %edx,%ebx
mov    %esi,%r12d
mov    %edi,%ebp
mov    $0x400,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %ebx,(%rsp)
lea    0xe63(%rip),%rsi
mov    %rax,%rdi
mov    $0x2521,%edx
mov    %ebp,%ecx
mov    %r12d,%r8d
mov    %ebx,%r9d
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %eax,%ebp
mov    %eax,%edi
add    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <memcpy@plt>
lea    (%rbx,%rbp,1),%eax
movw   $0x29,(%r14,%rax,1)
lea    (%rbx,%rbp,1),%esi
add    $0x1,%esi
mov    %r14,%rdi
mov    $0x8,%edx
call   1080 <iDoRemoteFunc@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
test   %ebx,%ebx
je     120d <_ucRcAT1608_Write+0x9d>
lea    0xe1b(%rip),%rdi
mov    %ebx,%esi
call   1050 <vWriteLogTxt@plt>
mov    $0x1,%al
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <iGetParaInt@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"%u(%u,%u,%u,p%04u\00"
@.str.1 = [2 x i8] c")\00"
@.str.2 = [37 x i8] c"_AT1608_Write iDoRemoteFunc ret = %d\00"
define zeroext i8 @_ucRcAT1608_Write(i8 zeroext %0, i8 zeroext %1, i32 %2, i8* %3) {
%5 = alloca i8
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8 %0, i8* %6
store i8 %1, i8* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = call i8* @malloc(i32 1024)
store i8* %13, i8** %12
%14 = load i8*, i8** %12
%15 = load i8, i8* %6
%16 = zext i8 %15 to i32
%17 = load i8, i8* %7
%18 = zext i8 %17 to i32
%19 = load i32, i32* %8
%20 = load i32, i32* %8
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 9505, i32 %16, i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %10
%22 = load i8*, i8** %12
%23 = load i32, i32* %10
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8*, i8** %9
%27 = load i32, i32* %8
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i32, i32* %8
%30 = load i32, i32* %10
%31 = add i32 %30, %29
store i32 %31, i32* %10
%32 = load i8*, i8** %12
%33 = load i32, i32* %10
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = call i32 (i8*, i8*, ...) @sprintf(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* %10
%38 = add i32 %37, %36
store i32 %38, i32* %10
%39 = load i8*, i8** %12
%40 = load i32, i32* %10
%41 = call i32 @iDoRemoteFunc(i8* %39, i32 %40, i32 8)
store i32 %41, i32* %11
%42 = load i8*, i8** %12
%43 = call i32 @free(i8* %42)
%44 = load i32, i32* %11
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %4
%47 = load i32, i32* %11
%48 = call i32 @vWriteLogTxt(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %47)
store i8 1, i8* %5
br label %51
49: ; preds = %4
%50 = call zeroext i8 @iGetParaInt(i32 0)
store i8 %50, i8* %5
br label %51
51: ; preds = %49, %46
%52 = load i8, i8* %5
ret i8 %52
}

LLVM IR (-O2):
@.str = [18 x i8] c"%u(%u,%u,%u,p%04u\00"
@.str.2 = [37 x i8] c"_AT1608_Write iDoRemoteFunc ret = %d\00"
define zeroext i8 @_ucRcAT1608_Write(i8 zeroext %0, i8 zeroext %1, i32 %2, i8* %3) {
%5 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%6 = zext i8 %0 to i32
%7 = zext i8 %1 to i32
%8 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %5, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 9505, i32 %6, i32 %7, i32 %2, i32 %2)
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %5, i64 %9
%11 = tail call i32 @memcpy(i8* %10, i8* %3, i32 %2)
%12 = add i32 %8, %2
%13 = zext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %5, i64 %13
%15 = bitcast i8* %14 to i16*
store i16 41, i16* %15
%16 = add i32 %12, 1
%17 = tail call i32 @iDoRemoteFunc(i8* %5, i32 %16, i32 8)
%18 = tail call i32 @free(i8* %5)
%19 = icmp eq i32 %17, 0
br i1 %19, label %22, label %20
20: ; preds = %4
%21 = tail call i32 @vWriteLogTxt(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %24
22: ; preds = %4
%23 = tail call zeroext i8 @iGetParaInt(i32 0)
br label %24
24: ; preds = %22, %20
%25 = phi i8 [ 1, %20 ], [ %23, %22 ]
ret i8 %25
}