#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 */
int read(int, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int set_drive(int fd, int channel, int output, int value) {
  unsigned char message[4];
  int n;
  message[0] = 6;
  message[1] = value;
  message[2] = channel;
  message[3] = output;
  write(fd, message, 4);
  usleep(100000);
  n = read(fd, message, 1);
  return n != 1 || message[0] != 'k';
}


x86 assembly (clang, -O0):
<set_drive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
movb   $0x6,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x13(%rbp)
mov    -0x8(%rbp),%eax
mov    %al,-0x12(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,-0x11(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
mov    $0x1,%al
cmpl   $0x1,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jne    11a5 <set_drive+0x75>
movzbl -0x14(%rbp),%eax
cmp    $0x6b,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_drive>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movb   $0x6,0x4(%rsp)
mov    %cl,0x5(%rsp)
mov    %sil,0x6(%rsp)
mov    %dl,0x7(%rsp)
lea    0x4(%rsp),%r14
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    $0x186a0,%edi
call   1050 <usleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
setne  %al
cmpb   $0x6b,0x4(%rsp)
setne  %cl
or     %al,%cl
movzbl %cl,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @set_drive(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [4 x i8]
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
store i8 6, i8* %11
%12 = load i32, i32* %8
%13 = trunc i32 %12 to i8
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %13, i8* %14
%15 = load i32, i32* %6
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %16, i8* %17
%18 = load i32, i32* %7
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %19, i8* %20
%21 = load i32, i32* %5
%22 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%23 = call i32 @write(i32 %21, i8* %22, i32 4)
%24 = call i32 @usleep(i32 100000)
%25 = load i32, i32* %5
%26 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 1)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 1
br i1 %29, label %35, label %30
30: ; preds = %4
%31 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 107
br label %35
35: ; preds = %30, %4
%36 = phi i1 [ true, %4 ], [ %34, %30 ]
%37 = zext i1 %36 to i32
ret i32 %37
}

LLVM IR (-O2):
define i32 @set_drive(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [4 x i8]
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
store i8 6, i8* %6
%7 = trunc i32 %3 to i8
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 1
store i8 %7, i8* %8
%9 = trunc i32 %1 to i8
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
store i8 %9, i8* %10
%11 = trunc i32 %2 to i8
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 3
store i8 %11, i8* %12
%13 = call i32 @write(i32 %0, i8* %6, i32 4)
%14 = tail call i32 @usleep(i32 100000)
%15 = call i32 @read(i32 %0, i8* %6, i32 1)
%16 = icmp ne i32 %15, 1
%17 = load i8, i8* %6
%18 = icmp ne i8 %17, 107
%19 = select i1 %16, i1 true, i1 %18
%20 = zext i1 %19 to i32
ret i32 %20
}