#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 atoi(char *);
char *lpc_show_error(int);
int /*<<< orphan*/ printf(char *, ...);
int serial_readln(struct _serial *, char *, int);
scalar_t__ serial_send(struct _serial *, int /*<<< orphan*/ *,
                       int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int lpc_send_command(struct _serial *serial, char *command) {
  char buffer[128];
  int err_code;
  int len;
  printf("lpc_send_command command='%s'\n", command);
  sprintf(buffer, "%s\r\n", command);
  if (serial_send(serial, (uint8_t *)buffer, strlen(buffer), 1) < 0) {
    printf("Error: serial port problem\n");
    return -1;
  }
  len = serial_readln(serial, buffer, 128);
  if (len == 128) {
    printf("Error: read error (128)\n");
    return -1;
  }
  buffer[len - 2] = 0;
  err_code = atoi(buffer);
  printf("buffer='%s'  %s (%d)\n", buffer, lpc_show_error(err_code), err_code);
  if (strcmp(buffer, "0") != 0) {
    printf("Error: lpc_send_command(%s) error status='%s' %s (%d).\n", command,
           buffer, lpc_show_error(err_code), err_code);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<lpc_send_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe68(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb8(%rbp),%rdi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
cmp    $0x0,%rax
jge    121f <lpc_send_command+0x9f>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133a <lpc_send_command+0x1ba>
mov    -0x10(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   10a0 <serial_readln@plt>
mov    %eax,-0xa8(%rbp)
cmpl   $0x80,-0xa8(%rbp)
jne    1264 <lpc_send_command+0xe4>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133a <lpc_send_command+0x1ba>
mov    -0xa8(%rbp),%eax
sub    $0x2,%eax
cltq
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0xa4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
mov    -0xa4(%rbp),%edi
call   1060 <lpc_show_error@plt>
mov    -0xc0(%rbp),%rsi
mov    %rax,%rdx
mov    -0xa4(%rbp),%ecx
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xda1(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1333 <lpc_send_command+0x1b3>
mov    -0x18(%rbp),%rax
mov    %rax,-0xd0(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xc8(%rbp)
mov    -0xa4(%rbp),%edi
call   1060 <lpc_show_error@plt>
mov    -0xd0(%rbp),%rsi
mov    -0xc8(%rbp),%rdx
mov    %rax,%rcx
mov    -0xa4(%rbp),%r8d
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133a <lpc_send_command+0x1ba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lpc_send_command>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe65(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r12,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    $0x1,%ecx
call   1030 <serial_send@plt>
test   %rax,%rax
js     120f <lpc_send_command+0x7f>
mov    %rsp,%r14
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x80,%edx
call   10b0 <serial_readln@plt>
cmp    $0x80,%eax
jne    121d <lpc_send_command+0x8d>
lea    0xe6c(%rip),%rdi
call   1040 <puts@plt>
jmp    1286 <lpc_send_command+0xf6>
lea    0xe76(%rip),%rdi
call   1040 <puts@plt>
jmp    1286 <lpc_send_command+0xf6>
cltq
movb   $0x0,-0x2(%rax,%r14,1)
mov    %r14,%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1070 <lpc_show_error@plt>
lea    0xde6(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
mov    %rax,%rdx
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xde3(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
je     128b <lpc_send_command+0xfb>
mov    %r15d,%edi
call   1070 <lpc_show_error@plt>
lea    0xdc9(%rip),%rdi
mov    %rsp,%rdx
mov    %r12,%rsi
mov    %rax,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct._serial = type { i32 }
@.str = [31 x i8] c"lpc_send_command command='%s'\0A\00"
@.str.1 = [5 x i8] c"%s\0D\0A\00"
@.str.2 = [28 x i8] c"Error: serial port problem\0A\00"
@.str.3 = [25 x i8] c"Error: read error (128)\0A\00"
@.str.4 = [22 x i8] c"buffer='%s' %s (%d)\0A\00"
@.str.5 = [2 x i8] c"0\00"
@.str.6 = [56 x i8] c"Error: lpc_send_command(%s) error status='%s' %s (%d).\0A\00"
define i32 @lpc_send_command(%struct._serial* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct._serial*
%5 = alloca i8*
%6 = alloca [128 x i8]
%7 = alloca i32
%8 = alloca i32
store %struct._serial* %0, %struct._serial** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %5
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = load %struct._serial*, %struct._serial** %4
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%16 = bitcast i8* %15 to i32*
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = call i64 @serial_send(%struct._serial* %14, i32* %16, i32 %18, i32 1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
23: ; preds = %2
%24 = load %struct._serial*, %struct._serial** %4
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%26 = call i32 @serial_readln(%struct._serial* %24, i8* %25, i32 128)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, 128
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
31: ; preds = %23
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %7
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%39 = load i32, i32* %7
%40 = call i8* @lpc_show_error(i32 %39)
%41 = load i32, i32* %7
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %38, i8* %40, i32 %41)
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%44 = call i64 @strcmp(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %53
46: ; preds = %31
%47 = load i8*, i8** %5
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%49 = load i32, i32* %7
%50 = call i8* @lpc_show_error(i32 %49)
%51 = load i32, i32* %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %47, i8* %48, i8* %50, i32 %51)
store i32 -1, i32* %3
br label %54
53: ; preds = %31
store i32 0, i32* %3
br label %54
54: ; preds = %53, %46, %29, %21
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct._serial = type { i32 }
@.str = [31 x i8] c"lpc_send_command command='%s'\0A\00"
@.str.1 = [5 x i8] c"%s\0D\0A\00"
@.str.4 = [22 x i8] c"buffer='%s' %s (%d)\0A\00"
@.str.5 = [2 x i8] c"0\00"
@.str.6 = [56 x i8] c"Error: lpc_send_command(%s) error status='%s' %s (%d).\0A\00"
@str = [24 x i8] c"Error: read error (128)\00"
@str.7 = [27 x i8] c"Error: serial port problem\00"
define i32 @lpc_send_command(%struct._serial* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%7 = bitcast [128 x i8]* %3 to i32*
%8 = call i32 @strlen(i8* %4)
%9 = call i64 @serial_send(%struct._serial* %0, i32* %7, i32 %8, i32 1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
br label %30
13: ; preds = %2
%14 = call i32 @serial_readln(%struct._serial* %0, i8* %4, i32 128)
%15 = icmp eq i32 %14, 128
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %30
18: ; preds = %13
%19 = add nsw i32 %14, -2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @atoi(i8* %4)
%23 = call i8* @lpc_show_error(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %4, i8* %23, i32 %22)
%25 = call i64 @strcmp(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %18
%28 = call i8* @lpc_show_error(i32 %22)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.6, i64 0, i64 0), i8* %1, i8* %4, i8* %28, i32 %22)
br label %30
30: ; preds = %18, %27, %16, %11
%31 = phi i32 [ -1, %11 ], [ -1, %16 ], [ -1, %27 ], [ 0, %18 ]
ret i32 %31
}