#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 /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int gpm2_open_ps2(int fd) {
  char obuf, ibuf;
  obuf = 0xff;
  write(fd, &obuf, 1);
  read(fd, &ibuf, 1);
  printf("RX: %#hhx\n", ibuf);
  obuf = 0xf2;
  write(fd, &obuf, 1);
  read(fd, &ibuf, 1);
  printf("ST: %#hhx\n", ibuf);
  read(fd, &ibuf, 1);
  printf("DI: %#hhx\n", ibuf);
  usleep(30000);
  tcflush(fd, TCIFLUSH);
  return 0;
}


x86 assembly (clang, -O0):
<gpm2_open_ps2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movb   $0xff,-0x5(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
lea    0xe78(%rip),%rdi
movsbl -0x6(%rbp),%esi
call   1050 <printf@plt>
movb   $0xf2,-0x5(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
lea    0xe4d(%rip),%rdi
movsbl -0x6(%rbp),%esi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
lea    0xe37(%rip),%rdi
movsbl -0x6(%rbp),%esi
call   1050 <printf@plt>
mov    $0x7530,%edi
call   1070 <usleep@plt>
mov    -0x4(%rbp),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gpm2_open_ps2>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movb   $0xff,0xf(%rsp)
lea    0xf(%rsp),%r15
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xe(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl 0xe(%rsp),%esi
lea    0xe6e(%rip),%rdi
call   1050 <printf@plt>
movb   $0xf2,0xf(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl 0xe(%rsp),%esi
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl 0xe(%rsp),%esi
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
mov    $0x7530,%edi
call   1070 <usleep@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"RX: %#hhx\0A\00"
@.str.1 = [11 x i8] c"ST: %#hhx\0A\00"
@.str.2 = [11 x i8] c"DI: %#hhx\0A\00"
@TCIFLUSH = global i32 0
define i32 @gpm2_open_ps2(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8
store i32 %0, i32* %2
store i8 -1, i8* %3
%5 = load i32, i32* %2
%6 = call i32 @write(i32 %5, i8* %3, i32 1)
%7 = load i32, i32* %2
%8 = call i32 @read(i32 %7, i8* %4, i32 1)
%9 = load i8, i8* %4
%10 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %9)
store i8 -14, i8* %3
%11 = load i32, i32* %2
%12 = call i32 @write(i32 %11, i8* %3, i32 1)
%13 = load i32, i32* %2
%14 = call i32 @read(i32 %13, i8* %4, i32 1)
%15 = load i8, i8* %4
%16 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8 signext %15)
%17 = load i32, i32* %2
%18 = call i32 @read(i32 %17, i8* %4, i32 1)
%19 = load i8, i8* %4
%20 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8 signext %19)
%21 = call i32 @usleep(i32 30000)
%22 = load i32, i32* %2
%23 = load i32, i32* @TCIFLUSH
%24 = call i32 @tcflush(i32 %22, i32 %23)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"RX: %#hhx\0A\00"
@.str.1 = [11 x i8] c"ST: %#hhx\0A\00"
@.str.2 = [11 x i8] c"DI: %#hhx\0A\00"
@TCIFLUSH = global i32 0
define i32 @gpm2_open_ps2(i32 %0) {
%2 = alloca i8
%3 = alloca i8
store i8 -1, i8* %2
%4 = call i32 @write(i32 %0, i8* %2, i32 1)
%5 = call i32 @read(i32 %0, i8* %3, i32 1)
%6 = load i8, i8* %3
%7 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %6)
store i8 -14, i8* %2
%8 = call i32 @write(i32 %0, i8* %2, i32 1)
%9 = call i32 @read(i32 %0, i8* %3, i32 1)
%10 = load i8, i8* %3
%11 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8 signext %10)
%12 = call i32 @read(i32 %0, i8* %3, i32 1)
%13 = load i8, i8* %3
%14 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8 signext %13)
%15 = tail call i32 @usleep(i32 30000)
%16 = load i32, i32* @TCIFLUSH
%17 = tail call i32 @tcflush(i32 %0, i32 %16)
ret i32 0
}