#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 */

/* Variables and functions */
char *calloc(int, int);
int /*<<< orphan*/ perror(char *);
int serialport_read_until(int, char *, char);
int serialport_write(int, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int);
int communicate(int fd, int comm1, int comm2) {
  char buf[256];
  int rc, n;
  char *tosend;
  tosend = calloc(5, sizeof(char));
  snprintf(tosend, 3, "%d%d", comm1, comm2);
  sleep(1);
  rc = serialport_write(fd, tosend);
  if (rc != 0)
    perror("write failed.");
  sleep(1);
  rc = serialport_read_until(fd, buf, '\n');
  if (rc != 0)
    perror("read failed.");
  rc = buf[0] - '0';
  return rc;
}


x86 assembly (clang, -O0):
<communicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x5,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    $0x3,%esi
lea    0xe5c(%rip),%rdx
call   1040 <snprintf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x4(%rbp),%edi
mov    -0x120(%rbp),%rsi
call   1060 <serialport_write@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
je     11e1 <communicate+0x81>
lea    0xe29(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xa,%edx
call   1030 <serialport_read_until@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
je     121e <communicate+0xbe>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
movsbl -0x110(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<communicate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%r14d
mov    $0x5,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%rbp
lea    0xe72(%rip),%rdx
mov    %rax,%rdi
mov    $0x3,%esi
mov    %ebx,%ecx
mov    %r15d,%r8d
call   1040 <snprintf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
call   1060 <serialport_write@plt>
test   %eax,%eax
jne    11ec <communicate+0x8c>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xa,%edx
call   1030 <serialport_read_until@plt>
test   %eax,%eax
jne    11fa <communicate+0x9a>
movsbl (%rsp),%eax
add    $0xffffffd0,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
jmp    11b9 <communicate+0x59>
lea    0xe12(%rip),%rdi
call   1070 <perror@plt>
jmp    11d7 <communicate+0x77>

LLVM IR (-O0):
@.str = [5 x i8] c"%d%d\00"
@.str.1 = [14 x i8] c"write failed.\00"
@.str.2 = [13 x i8] c"read failed.\00"
define i32 @communicate(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = call i8* @calloc(i32 5, i32 1)
store i8* %11, i8** %10
%12 = load i8*, i8** %10
%13 = load i32, i32* %5
%14 = load i32, i32* %6
%15 = call i32 @snprintf(i8* %12, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = call i32 @sleep(i32 1)
%17 = load i32, i32* %4
%18 = load i8*, i8** %10
%19 = call i32 @serialport_write(i32 %17, i8* %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %3
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %3
%25 = call i32 @sleep(i32 1)
%26 = load i32, i32* %4
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%28 = call i32 @serialport_read_until(i32 %26, i8* %27, i8 signext 10)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = sub nsw i32 %36, 48
store i32 %37, i32* %8
%38 = load i32, i32* %8
ret i32 %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d%d\00"
@.str.1 = [14 x i8] c"write failed.\00"
@.str.2 = [13 x i8] c"read failed.\00"
define i32 @communicate(i32 %0, i32 %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 dereferenceable_or_null(5) i8* @calloc(i32 5, i32 1)
%7 = tail call i32 @snprintf(i8* %6, i32 3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%8 = tail call i32 @sleep(i32 1)
%9 = tail call i32 @serialport_write(i32 %0, i8* %6)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = tail call i32 @sleep(i32 1)
%15 = call i32 @serialport_read_until(i32 %0, i8* %5, i8 signext 10)
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = load i8, i8* %5
%21 = sext i8 %20 to i32
%22 = add nsw i32 %21, -48
ret i32 %22
}