#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*/ uint8_t;
struct _serial {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ lpc_read(struct _serial *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ serial_readln(struct _serial *, char *, int);
scalar_t__ serial_send(struct _serial *, int /*<<< orphan*/ *, int, int);
scalar_t__ strcmp(char *, char *);
int lpc_synchronize(struct _serial *serial) {
  char buffer[128];
  if (serial_send(serial, (uint8_t *)"?", 1, 1) < 0) {
    return -1;
  }
  lpc_read(serial, buffer, 128);
  if (strcmp(buffer, "Synchronized") != 0) {
    printf("Error: Cannot synchronize.  Try resetting chip.\n");
    return -1;
  }
  if (serial_send(serial, (uint8_t *)"Synchronized\r\n",
                  sizeof("Synchronized\r\n") - 1, 1) < 0) {
    return -1;
  }
  serial_readln(serial, buffer, 128);
  lpc_read(serial, buffer, 128);
  if (strcmp(buffer, "OK") != 0) {
    printf("Error: Cannot synchronize (No sync OK).  Try resetting chip.\n");
    return -1;
  }
  if (serial_send(serial, (uint8_t *)"10000\r\n", sizeof("10000\r\n") - 1, 1) <
      0) {
    return -1;
  }
  serial_readln(serial, buffer, 128);
  lpc_read(serial, buffer, 128);
  if (strcmp(buffer, "OK") != 0) {
    printf("Error: Cannot synchronize (No freq OK).  Try resetting chip.\n");
    return -1;
  }
  if (serial_send(serial, (uint8_t *)"A 0\r\n", sizeof("A 0\r\n") - 1, 1) < 0) {
    return -1;
  }
  serial_readln(serial, buffer, 128);
  lpc_read(serial, buffer, 128);
  if (strcmp(buffer, "0") != 0) {
    printf("Error: Cannot synchronize (No echo OK).  Try resetting chip.\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<lpc_synchronize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1030 <serial_send@plt>
cmp    $0x0,%rax
jge    118c <lpc_synchronize+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    -0x90(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11d6 <lpc_synchronize+0x86>
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0xe,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
cmp    $0x0,%rax
jge    1206 <lpc_synchronize+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <serial_readln@plt>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    -0x90(%rbp),%rdi
lea    0xe11(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1265 <lpc_synchronize+0x115>
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    0xe20(%rip),%rsi
mov    $0x7,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
cmp    $0x0,%rax
jge    1295 <lpc_synchronize+0x145>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <serial_readln@plt>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    -0x90(%rbp),%rdi
lea    0xd82(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12f4 <lpc_synchronize+0x1a4>
lea    0xdb5(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    0xdd7(%rip),%rsi
mov    $0x5,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
cmp    $0x0,%rax
jge    1324 <lpc_synchronize+0x1d4>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <serial_readln@plt>
mov    -0x10(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    -0x90(%rbp),%rdi
lea    0xd80(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1383 <lpc_synchronize+0x233>
lea    0xd6c(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138a <lpc_synchronize+0x23a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lpc_synchronize>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rdi,%rbx
lea    0xe9b(%rip),%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     11b2 <lpc_synchronize+0x62>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    0xe69(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c1 <lpc_synchronize+0x71>
lea    0xf3b(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
mov    $0xe,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
test   %rax,%rax
js     11b2 <lpc_synchronize+0x62>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1070 <serial_readln@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    0xe15(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     121f <lpc_synchronize+0xcf>
lea    0xe8e(%rip),%rdi
jmp    11ad <lpc_synchronize+0x5d>
lea    0xdfb(%rip),%rsi
mov    %rbx,%rdi
mov    $0x7,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
test   %rax,%rax
js     11b2 <lpc_synchronize+0x62>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1070 <serial_readln@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    0xdb3(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1284 <lpc_synchronize+0x134>
lea    0xdef(%rip),%rdi
jmp    11ad <lpc_synchronize+0x5d>
lea    0xd9e(%rip),%rsi
mov    %rbx,%rdi
mov    $0x5,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
test   %rax,%rax
js     11b2 <lpc_synchronize+0x62>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1070 <serial_readln@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1060 <lpc_read@plt>
lea    0xd5f(%rip),%rsi
mov    %rbp,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     12e9 <lpc_synchronize+0x199>
lea    0xd4d(%rip),%rdi
jmp    11ad <lpc_synchronize+0x5d>
xor    %r14d,%r14d
jmp    11b2 <lpc_synchronize+0x62>

LLVM IR (-O0):
%struct._serial = type { i32 }
@.str = [2 x i8] c"?\00"
@.str.1 = [13 x i8] c"Synchronized\00"
@.str.2 = [49 x i8] c"Error: Cannot synchronize. Try resetting chip.\0A\00"
@.str.3 = [15 x i8] c"Synchronized\0D\0A\00"
@.str.4 = [3 x i8] c"OK\00"
@.str.5 = [62 x i8] c"Error: Cannot synchronize (No sync OK). Try resetting chip.\0A\00"
@.str.6 = [8 x i8] c"10000\0D\0A\00"
@.str.7 = [62 x i8] c"Error: Cannot synchronize (No freq OK). Try resetting chip.\0A\00"
@.str.8 = [6 x i8] c"A 0\0D\0A\00"
@.str.9 = [2 x i8] c"0\00"
@.str.10 = [62 x i8] c"Error: Cannot synchronize (No echo OK). Try resetting chip.\0A\00"
define i32 @lpc_synchronize(%struct._serial* %0) {
%2 = alloca i32
%3 = alloca %struct._serial*
%4 = alloca [128 x i8]
store %struct._serial* %0, %struct._serial** %3
%5 = load %struct._serial*, %struct._serial** %3
%6 = call i64 @serial_send(%struct._serial* %5, i32* bitcast ([2 x i8]* @.str to i32*), i32 1, i32 1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %70
9: ; preds = %1
%10 = load %struct._serial*, %struct._serial** %3
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%12 = call i32 @lpc_read(%struct._serial* %10, i8* %11, i32 128)
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%14 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %9
%17 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %70
18: ; preds = %9
%19 = load %struct._serial*, %struct._serial** %3
%20 = call i64 @serial_send(%struct._serial* %19, i32* bitcast ([15 x i8]* @.str.3 to i32*), i32 14, i32 1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %2
br label %70
23: ; preds = %18
%24 = load %struct._serial*, %struct._serial** %3
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = call i32 @serial_readln(%struct._serial* %24, i8* %25, i32 128)
%27 = load %struct._serial*, %struct._serial** %3
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%29 = call i32 @lpc_read(%struct._serial* %27, i8* %28, i32 128)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %2
br label %70
35: ; preds = %23
%36 = load %struct._serial*, %struct._serial** %3
%37 = call i64 @serial_send(%struct._serial* %36, i32* bitcast ([8 x i8]* @.str.6 to i32*), i32 7, i32 1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %2
br label %70
40: ; preds = %35
%41 = load %struct._serial*, %struct._serial** %3
%42 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%43 = call i32 @serial_readln(%struct._serial* %41, i8* %42, i32 128)
%44 = load %struct._serial*, %struct._serial** %3
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%46 = call i32 @lpc_read(%struct._serial* %44, i8* %45, i32 128)
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %2
br label %70
52: ; preds = %40
%53 = load %struct._serial*, %struct._serial** %3
%54 = call i64 @serial_send(%struct._serial* %53, i32* bitcast ([6 x i8]* @.str.8 to i32*), i32 5, i32 1)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 -1, i32* %2
br label %70
57: ; preds = %52
%58 = load %struct._serial*, %struct._serial** %3
%59 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%60 = call i32 @serial_readln(%struct._serial* %58, i8* %59, i32 128)
%61 = load %struct._serial*, %struct._serial** %3
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%63 = call i32 @lpc_read(%struct._serial* %61, i8* %62, i32 128)
%64 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%65 = call i64 @strcmp(i8* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %57
%68 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.10, i64 0, i64 0))
store i32 -1, i32* %2
br label %70
69: ; preds = %57
store i32 0, i32* %2
br label %70
70: ; preds = %69, %67, %56, %50, %39, %33, %22, %16, %8
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct._serial = type { i32 }
@.str = [2 x i8] c"?\00"
@.str.1 = [13 x i8] c"Synchronized\00"
@.str.3 = [15 x i8] c"Synchronized\0D\0A\00"
@.str.4 = [3 x i8] c"OK\00"
@.str.6 = [8 x i8] c"10000\0D\0A\00"
@.str.8 = [6 x i8] c"A 0\0D\0A\00"
@.str.9 = [2 x i8] c"0\00"
@str = [61 x i8] c"Error: Cannot synchronize (No echo OK). Try resetting chip.\00"
@str.11 = [61 x i8] c"Error: Cannot synchronize (No freq OK). Try resetting chip.\00"
@str.12 = [61 x i8] c"Error: Cannot synchronize (No sync OK). Try resetting chip.\00"
@str.13 = [48 x i8] c"Error: Cannot synchronize. Try resetting chip.\00"
define i32 @lpc_synchronize(%struct._serial* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @serial_send(%struct._serial* %0, i32* bitcast ([2 x i8]* @.str to i32*), i32 1, i32 1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %37, label %6
6: ; preds = %1
%7 = call i32 @lpc_read(%struct._serial* %0, i8* %3, i32 128)
%8 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %34
10: ; preds = %6
%11 = call i64 @serial_send(%struct._serial* %0, i32* bitcast ([15 x i8]* @.str.3 to i32*), i32 14, i32 1)
%12 = icmp slt i64 %11, 0
br i1 %12, label %37, label %13
13: ; preds = %10
%14 = call i32 @serial_readln(%struct._serial* %0, i8* %3, i32 128)
%15 = call i32 @lpc_read(%struct._serial* %0, i8* %3, i32 128)
%16 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %13
%19 = call i64 @serial_send(%struct._serial* %0, i32* bitcast ([8 x i8]* @.str.6 to i32*), i32 7, i32 1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %37, label %21
21: ; preds = %18
%22 = call i32 @serial_readln(%struct._serial* %0, i8* %3, i32 128)
%23 = call i32 @lpc_read(%struct._serial* %0, i8* %3, i32 128)
%24 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = call i64 @serial_send(%struct._serial* %0, i32* bitcast ([6 x i8]* @.str.8 to i32*), i32 5, i32 1)
%28 = icmp slt i64 %27, 0
br i1 %28, label %37, label %29
29: ; preds = %26
%30 = call i32 @serial_readln(%struct._serial* %0, i8* %3, i32 128)
%31 = call i32 @lpc_read(%struct._serial* %0, i8* %3, i32 128)
%32 = call i64 @strcmp(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29, %21, %13, %6
%35 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @str.13, i64 0, i64 0), %6 ], [ getelementptr inbounds ([61 x i8], [61 x i8]* @str.12, i64 0, i64 0), %13 ], [ getelementptr inbounds ([61 x i8], [61 x i8]* @str.11, i64 0, i64 0), %21 ], [ getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0), %29 ]
%36 = call i32 @puts(i8* dereferenceable(1) %35)
br label %37
37: ; preds = %34, %29, %26, %18, %10, %1
%38 = phi i32 [ -1, %1 ], [ -1, %10 ], [ -1, %18 ], [ -1, %26 ], [ 0, %29 ], [ -1, %34 ]
ret i32 %38
}