#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 uint32_t;

/* Variables and functions */
int /*<<< orphan*/ die(char *);
scalar_t__ serial_data_avail();
int /*<<< orphan*/ serial_purge();
char serial_read_byte();
int /*<<< orphan*/ serial_write(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char, int, int);
int /*<<< orphan*/ strlen(char *);
int sys_get_clock_usec();
void samba_write(uint32_t addr, uint32_t data, int size, int timeout) {
  char tmpbuf[1024], c;
  uint32_t tstart;
  serial_purge();
  snprintf(tmpbuf, 1024, "%c%08x,%08x#",
           size == 1 ? 'O' : (size == 2 ? 'H' : 'W'), addr, data);
  serial_write(tmpbuf, strlen(tmpbuf));
  tstart = sys_get_clock_usec();
  while (1) {
    if (serial_data_avail()) {
      if (serial_read_byte() == '>')
        break;
    }
    if (sys_get_clock_usec() - tstart > timeout)
      die("tx timeout");
  }
}


x86 assembly (clang, -O0):
<samba_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    $0x0,%al
call   1030 <serial_purge@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    11c6 <samba_write+0x46>
mov    $0x4f,%eax
mov    %eax,-0x424(%rbp)
jmp    11df <samba_write+0x5f>
mov    -0xc(%rbp),%edx
mov    $0x57,%eax
mov    $0x48,%ecx
cmp    $0x2,%edx
cmove  %ecx,%eax
mov    %eax,-0x424(%rbp)
mov    -0x420(%rbp),%rdi
mov    -0x424(%rbp),%eax
mov    -0x4(%rbp),%r8d
mov    -0x8(%rbp),%r9d
mov    $0x400,%esi
lea    0xe00(%rip),%rdx
movsbl %al,%ecx
call   1060 <snprintf@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x430(%rbp),%rdi
mov    %eax,%esi
call   10a0 <serial_write@plt>
mov    $0x0,%al
call   1050 <sys_get_clock_usec@plt>
mov    %eax,-0x418(%rbp)
mov    $0x0,%al
call   1070 <serial_data_avail@plt>
cmp    $0x0,%rax
je     126b <samba_write+0xeb>
mov    $0x0,%al
call   1080 <serial_read_byte@plt>
movsbl %al,%eax
cmp    $0x3e,%eax
jne    1266 <samba_write+0xe6>
jmp    1292 <samba_write+0x112>
jmp    126b <samba_write+0xeb>
mov    $0x0,%al
call   1050 <sys_get_clock_usec@plt>
sub    -0x418(%rbp),%eax
cmp    -0x10(%rbp),%eax
jle    128d <samba_write+0x10d>
lea    0xd85(%rip),%rdi
call   1090 <die@plt>
jmp    123d <samba_write+0xbd>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<samba_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r14d
mov    %edi,%r15d
xor    %eax,%eax
call   1030 <serial_purge@plt>
cmp    $0x2,%ebp
mov    $0x48,%eax
mov    $0x57,%edx
cmove  %eax,%edx
cmp    $0x1,%ebp
mov    $0x4f,%ecx
cmovne %edx,%ecx
lea    0xe40(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r15d,%r8d
mov    %r14d,%r9d
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   10a0 <serial_write@plt>
xor    %eax,%eax
call   1050 <sys_get_clock_usec@plt>
mov    %eax,%ebp
lea    0xe15(%rip),%r14
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <serial_data_avail@plt>
test   %rax,%rax
je     1217 <samba_write+0x97>
xor    %eax,%eax
call   1080 <serial_read_byte@plt>
cmp    $0x3e,%al
je     122e <samba_write+0xae>
xor    %eax,%eax
call   1050 <sys_get_clock_usec@plt>
sub    %ebp,%eax
cmp    %ebx,%eax
jle    1200 <samba_write+0x80>
mov    %r14,%rdi
call   1090 <die@plt>
jmp    1200 <samba_write+0x80>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"%c%08x,%08x#\00"
@.str.1 = [11 x i8] c"tx timeout\00"
define void @samba_write(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i8
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = call i32 (...) @serial_purge()
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %4
br label %22
17: ; preds = %4
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, 2
%20 = zext i1 %19 to i64
%21 = select i1 %19, i32 72, i32 87
br label %22
22: ; preds = %17, %16
%23 = phi i32 [ 79, %16 ], [ %21, %17 ]
%24 = trunc i32 %23 to i8
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = call i32 @snprintf(i8* %13, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8 signext %24, i32 %25, i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @serial_write(i8* %28, i32 %30)
%32 = call i32 (...) @sys_get_clock_usec()
store i32 %32, i32* %11
br label %33
33: ; preds = %22, %50
%34 = call i64 (...) @serial_data_avail()
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %33
%37 = call signext i8 (...) @serial_read_byte()
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 62
br i1 %39, label %40, label %41
40: ; preds = %36
br label %51
41: ; preds = %36
br label %42
42: ; preds = %41, %33
%43 = call i32 (...) @sys_get_clock_usec()
%44 = load i32, i32* %11
%45 = sub nsw i32 %43, %44
%46 = load i32, i32* %8
%47 = icmp sgt i32 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
br label %33
51: ; preds = %40
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"%c%08x,%08x#\00"
@.str.1 = [11 x i8] c"tx timeout\00"
define void @samba_write(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = tail call i32 (...) @serial_purge()
%8 = icmp eq i32 %2, 1
%9 = icmp eq i32 %2, 2
%10 = select i1 %9, i8 72, i8 87
%11 = select i1 %8, i8 79, i8 %10
%12 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8 signext %11, i32 %0, i32 %1)
%13 = call i32 @strlen(i8* %6)
%14 = call i32 @serial_write(i8* %6, i32 %13)
%15 = call i32 (...) @sys_get_clock_usec()
br label %16
16: ; preds = %28, %4
%17 = call i64 (...) @serial_data_avail()
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call signext i8 (...) @serial_read_byte()
%21 = icmp eq i8 %20, 62
br i1 %21, label %29, label %22
22: ; preds = %19, %16
%23 = call i32 (...) @sys_get_clock_usec()
%24 = sub nsw i32 %23, %15
%25 = icmp sgt i32 %24, %3
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
br label %16
29: ; preds = %19
ret void
}