#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 /*<<< orphan*/ write(int, char *, int);
int logitech_command(int fd, char *c) {
  int i;
  unsigned char d;
  for (i = 0; c[i]; i++) {
    write(fd, c + i, 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     11be <logitech_command+0x9e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
cmp    $0x0,%rax
je     1187 <logitech_command+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <logitech_command+0xa5>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
movzbl -0x15(%rbp),%ecx
cmp    %ecx,%eax
je     11ab <logitech_command+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <logitech_command+0xa5>
jmp    11b0 <logitech_command+0x90>
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   %r14
push   %rbx
sub    $0x10,%rsp
cmpb   $0x0,(%rsi)
je     1181 <logitech_command+0x61>
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xf(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x3e8,%edx
call   1030 <readchar@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    1183 <logitech_command+0x63>
movsbl (%rbx),%ecx
movzbl 0xf(%rsp),%edx
cmp    %edx,%ecx
jne    1183 <logitech_command+0x63>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1140 <logitech_command+0x20>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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 = %38, %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 %41
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 = load i32, i32* %4
%23 = call i64 @readchar(i32 %22, i8* %7, i32 1000)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i32 -1, i32* %3
br label %42
26: ; preds = %15
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = load i8, i8* %7
%34 = zext i8 %33 to i32
%35 = icmp ne i32 %32, %34
br i1 %35, label %36, label %37
36: ; preds = %26
store i32 -1, i32* %3
br label %42
37: ; preds = %26
br label %38
38: ; preds = %37
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %8
41: ; preds = %8
store i32 0, i32* %3
br label %42
42: ; preds = %41, %36, %25
%43 = load i32, i32* %3
ret i32 %43
}

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 %23, label %10
6: ; preds = %16
%7 = getelementptr inbounds i8, i8* %1, i64 %22
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %2, %6
%11 = phi i64 [ %22, %6 ], [ 0, %2 ]
%12 = phi i8* [ %7, %6 ], [ %1, %2 ]
%13 = call i32 @write(i32 %0, i8* %12, i32 1)
%14 = call i64 @readchar(i32 %0, i8* %3, i32 1000)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %10
%17 = load i8, i8* %12
%18 = sext i8 %17 to i32
%19 = load i8, i8* %3
%20 = zext i8 %19 to i32
%21 = icmp eq i32 %18, %20
%22 = add nuw i64 %11, 1
br i1 %21, label %6, label %23
23: ; preds = %10, %16, %6, %2
%24 = phi i32 [ 0, %2 ], [ 0, %6 ], [ -1, %16 ], [ -1, %10 ]
ret i32 %24
}