#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 /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ strlen(char *);
int sys_get_clock_usec();
uint32_t samba_read(uint32_t addr, int size, int timeout) {
  char tmpbuf[1024], c = 0;
  int i, xpos = -1;
  uint32_t rval;
  uint32_t tstart;
  serial_purge();
  snprintf(tmpbuf, 1024, "%c%08x,#", size == 1 ? 'o' : (size == 2 ? 'h' : 'w'),
           addr);
  serial_write(tmpbuf, strlen(tmpbuf));
  tstart = sys_get_clock_usec();
  for (i = 0; i < 1024 && c != '>';) {
    if (serial_data_avail()) {
      tmpbuf[i] = c = serial_read_byte();
      if (c == 'x')
        xpos = i;
      i++;
    }
    if (sys_get_clock_usec() - tstart > timeout)
      die("rx timeout");
  }
  if (xpos < 0)
    die("invalid response from samba");
  sscanf(tmpbuf, "%x", &rval);
  return rval;
}


x86 assembly (clang, -O0):
<samba_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
movb   $0x0,-0x411(%rbp)
movl   $0xffffffff,-0x41c(%rbp)
ff ff ff
mov    $0x0,%al
call   1030 <serial_purge@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x430(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11e4 <samba_read+0x54>
mov    $0x6f,%eax
mov    %eax,-0x434(%rbp)
jmp    11fd <samba_read+0x6d>
mov    -0x8(%rbp),%edx
mov    $0x77,%eax
mov    $0x68,%ecx
cmp    $0x2,%edx
cmove  %ecx,%eax
mov    %eax,-0x434(%rbp)
mov    -0x430(%rbp),%rdi
mov    -0x434(%rbp),%eax
mov    -0x4(%rbp),%r8d
mov    $0x400,%esi
lea    0xde6(%rip),%rdx
movsbl %al,%ecx
call   1060 <snprintf@plt>
lea    -0x410(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x440(%rbp),%rdi
mov    %eax,%esi
call   10b0 <serial_write@plt>
mov    $0x0,%al
call   1050 <sys_get_clock_usec@plt>
mov    %eax,-0x424(%rbp)
movl   $0x0,-0x418(%rbp)
xor    %eax,%eax
cmpl   $0x400,-0x418(%rbp)
mov    %al,-0x441(%rbp)
jge    128c <samba_read+0xfc>
movsbl -0x411(%rbp),%eax
cmp    $0x3e,%eax
setne  %al
mov    %al,-0x441(%rbp)
mov    -0x441(%rbp),%al
test   $0x1,%al
jne    129f <samba_read+0x10f>
jmp    131f <samba_read+0x18f>
mov    $0x0,%al
call   1070 <serial_data_avail@plt>
cmp    $0x0,%rax
je     12f8 <samba_read+0x168>
mov    $0x0,%al
call   1080 <serial_read_byte@plt>
mov    %al,%cl
mov    %cl,-0x411(%rbp)
movslq -0x418(%rbp),%rax
mov    %cl,-0x410(%rbp,%rax,1)
movsbl -0x411(%rbp),%eax
cmp    $0x78,%eax
jne    12e9 <samba_read+0x159>
mov    -0x418(%rbp),%eax
mov    %eax,-0x41c(%rbp)
mov    -0x418(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x418(%rbp)
mov    $0x0,%al
call   1050 <sys_get_clock_usec@plt>
sub    -0x424(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    131a <samba_read+0x18a>
lea    0xcf4(%rip),%rdi
call   10a0 <die@plt>
jmp    1261 <samba_read+0xd1>
cmpl   $0x0,-0x41c(%rbp)
jge    1338 <samba_read+0x1a8>
lea    0xce1(%rip),%rdi
call   10a0 <die@plt>
lea    -0x410(%rbp),%rdi
lea    0xcea(%rip),%rsi
lea    -0x420(%rbp),%rdx
call   1090 <sscanf@plt>
mov    -0x420(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<samba_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%ebp
xor    %eax,%eax
call   1030 <serial_purge@plt>
cmp    $0x2,%ebx
mov    $0x68,%eax
mov    $0x77,%edx
cmove  %eax,%edx
cmp    $0x1,%ebx
mov    $0x6f,%ecx
cmovne %edx,%ecx
lea    0xe2f(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %ebp,%r8d
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   10b0 <serial_write@plt>
xor    %eax,%eax
call   1050 <sys_get_clock_usec@plt>
mov    %eax,%r12d
mov    $0xffffffff,%r13d
lea    0xdfa(%rip),%r14
xor    %ebx,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1070 <serial_data_avail@plt>
test   %rax,%rax
je     1245 <samba_read+0xb5>
xor    %eax,%eax
call   1080 <serial_read_byte@plt>
mov    %eax,%ebx
movslq %ebp,%rbp
mov    %al,0x10(%rsp,%rbp,1)
cmp    $0x78,%al
cmove  %ebp,%r13d
add    $0x1,%ebp
xor    %eax,%eax
call   1050 <sys_get_clock_usec@plt>
sub    %r12d,%eax
cmp    %r15d,%eax
jle    125c <samba_read+0xcc>
mov    %r14,%rdi
call   10a0 <die@plt>
cmp    $0x3ff,%ebp
jg     1269 <samba_read+0xd9>
cmp    $0x3e,%bl
jne    1220 <samba_read+0x90>
test   %r13d,%r13d
jns    127a <samba_read+0xea>
lea    0xd9f(%rip),%rdi
call   10a0 <die@plt>
lea    0xdaf(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1090 <sscanf@plt>
mov    0xc(%rsp),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%c%08x,#\00"
@.str.1 = [11 x i8] c"rx timeout\00"
@.str.2 = [28 x i8] c"invalid response from samba\00"
@.str.3 = [3 x i8] c"%x\00"
define i32 @samba_read(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i8
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8 0, i8* %8
store i32 -1, i32* %10
%13 = call i32 (...) @serial_purge()
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %3
br label %23
18: ; preds = %3
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 2
%21 = zext i1 %20 to i64
%22 = select i1 %20, i32 104, i32 119
br label %23
23: ; preds = %18, %17
%24 = phi i32 [ 111, %17 ], [ %22, %18 ]
%25 = trunc i32 %24 to i8
%26 = load i32, i32* %4
%27 = call i32 @snprintf(i8* %14, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8 signext %25, i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, 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* %12
store i32 0, i32* %9
br label %33
33: ; preds = %66, %23
%34 = load i32, i32* %9
%35 = icmp slt i32 %34, 1024
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i8, i8* %8
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 62
br label %40
40: ; preds = %36, %33
%41 = phi i1 [ false, %33 ], [ %39, %36 ]
br i1 %41, label %42, label %67
42: ; preds = %40
%43 = call i64 (...) @serial_data_avail()
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %58
45: ; preds = %42
%46 = call signext i8 (...) @serial_read_byte()
store i8 %46, i8* %8
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %48
store i8 %46, i8* %49
%50 = load i8, i8* %8
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 120
br i1 %52, label %53, label %55
53: ; preds = %45
%54 = load i32, i32* %9
store i32 %54, i32* %10
br label %55
55: ; preds = %53, %45
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %58
58: ; preds = %55, %42
%59 = call i32 (...) @sys_get_clock_usec()
%60 = load i32, i32* %12
%61 = sub nsw i32 %59, %60
%62 = load i32, i32* %6
%63 = icmp sgt i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %66
66: ; preds = %64, %58
br label %33
67: ; preds = %40
%68 = load i32, i32* %10
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %72
72: ; preds = %70, %67
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%74 = call i32 @sscanf(i8* %73, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %11)
%75 = load i32, i32* %11
ret i32 %75
}

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