#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 */
scalar_t__ readchar(int, unsigned char *, int);
int write(int, char *, int);
int logitech_command(int fd, char *c) {
  int i;
  unsigned char d;
  for (i = 0; c[i]; i++) {
    if (write(fd, c + i, 1) != 1)
      return -1;
    if (readchar(fd, &d, 1000))
      return -1;
    if (c[i] != d)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<logitech_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11d3 <logitech_command+0xb3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
je     1175 <logitech_command+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <logitech_command+0xba>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
je     119c <logitech_command+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <logitech_command+0xba>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movzbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
je     11c0 <logitech_command+0xa0>
movl   $0xffffffff,-0x4(%rbp)
jmp    11da <logitech_command+0xba>
jmp    11c5 <logitech_command+0xa5>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1136 <logitech_command+0x16>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logitech_command>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rsi)
je     1180 <logitech_command+0x60>
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0x7(%rsp),%r14
mov    $0xffffffff,%ebp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    1182 <logitech_command+0x62>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
test   %rax,%rax
jne    1182 <logitech_command+0x62>
movsbl (%rbx),%eax
movzbl 0x7(%rsp),%ecx
cmp    %ecx,%eax
jne    1182 <logitech_command+0x62>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1140 <logitech_command+0x20>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @logitech_command(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
br label %8
8: ; preds = %41, %2
%9 = load i8*, i8** %5
%10 = load i32, i32* %6
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %8
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = call i32 @write(i32 %16, i8* %20, i32 1)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %15
store i32 -1, i32* %3
br label %45
24: ; preds = %15
%25 = load i32, i32* %4
%26 = call i64 @readchar(i32 %25, i8* %7, i32 1000)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %3
br label %45
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = load i8, i8* %7
%37 = zext i8 %36 to i32
%38 = icmp ne i32 %35, %37
br i1 %38, label %39, label %40
39: ; preds = %29
store i32 -1, i32* %3
br label %45
40: ; preds = %29
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = add nsw i32 %42, 1
store i32 %43, i32* %6
br label %8
44: ; preds = %8
store i32 0, i32* %3
br label %45
45: ; preds = %44, %39, %28, %23
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define i32 @logitech_command(i32 %0, i8* nocapture %1) {
%3 = alloca i8
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
br i1 %5, label %25, label %10
6: ; preds = %18
%7 = getelementptr inbounds i8, i8* %1, i64 %24
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %25, label %10
10: ; preds = %2, %6
%11 = phi i64 [ %24, %6 ], [ 0, %2 ]
%12 = phi i8* [ %7, %6 ], [ %1, %2 ]
%13 = call i32 @write(i32 %0, i8* %12, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = call i64 @readchar(i32 %0, i8* %3, i32 1000)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i8, i8* %12
%20 = sext i8 %19 to i32
%21 = load i8, i8* %3
%22 = zext i8 %21 to i32
%23 = icmp eq i32 %20, %22
%24 = add nuw i64 %11, 1
br i1 %23, label %6, label %25
25: ; preds = %10, %15, %18, %6, %2
%26 = phi i32 [ 0, %2 ], [ 0, %6 ], [ -1, %18 ], [ -1, %15 ], [ -1, %10 ]
ret i32 %26
}