#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ input_flush_serial_port();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write_serial_port(char *, int /*<<< orphan*/);
int xmodem_transmit(unsigned char const *, int);
int write_memory(int addr, const unsigned char *data, int len) {
  int r;
  char buf[256];
  input_flush_serial_port();
  snprintf(buf, sizeof(buf), "S%X,%X#", addr, len);
  printf("sending command \"%s\"\n", buf);
  write_serial_port(buf, strlen(buf));
  r = xmodem_transmit(data, len);
  return r;
}


x86 assembly (clang, -O0):
<write_memory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   1080 <input_flush_serial_port@plt>
lea    -0x120(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    $0x100,%esi
lea    0xe6a(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x120(%rbp),%rsi
lea    0xe5f(%rip),%rdi
call   1060 <printf@plt>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    -0x120(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <xmodem_transmit@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_memory>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1080 <input_flush_serial_port@plt>
lea    0xe7d(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %ebp,%ecx
mov    %r15d,%r8d
call   1070 <snprintf@plt>
lea    0xe69(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <write_serial_port@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <xmodem_transmit@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"S%X,%X#\00"
@.str.1 = [22 x i8] c"sending command \22%s\22\0A\00"
define i32 @write_memory(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [256 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i32 (...) @input_flush_serial_port()
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%11 = load i32, i32* %4
%12 = load i32, i32* %6
%13 = call i32 @snprintf(i8* %10, i32 256, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %11, i32 %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write_serial_port(i8* %16, i32 %18)
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i32 @xmodem_transmit(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
ret i32 %23
}

LLVM IR (-O2):
@.str = [8 x i8] c"S%X,%X#\00"
@.str.1 = [22 x i8] c"sending command \22%s\22\0A\00"
define i32 @write_memory(i32 %0, i8* %1, i32 %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (...) @input_flush_serial_port()
%7 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %2)
%8 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%9 = call i32 @strlen(i8* %5)
%10 = call i32 @write_serial_port(i8* %5, i32 %9)
%11 = call i32 @xmodem_transmit(i8* %1, i32 %2)
ret i32 %11
}