#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 SioCmdportFd(int);
int SioWaitAck(int, unsigned char);
int write(int, unsigned char *, int);
int sio_lctrl(int fd, int mode) {
  unsigned char buf[8];
  if ((fd = SioCmdportFd(fd)) == -1)
    return (-1);
  buf[0] = 18;
  buf[1] = 2;
  if (mode & 1)
    buf[2] = 1;
  else
    buf[2] = 0;
  if (mode & 2)
    buf[3] = 1;
  else
    buf[3] = 0;
  if (write(fd, buf, 4) != 4)
    return (-1);
  return (SioWaitAck(fd, buf[0]));
}


x86 assembly (clang, -O0):
<sio_lctrl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <SioCmdportFd@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    115e <sio_lctrl+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <sio_lctrl+0xa3>
movb   $0x12,-0x14(%rbp)
movb   $0x2,-0x13(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     117e <sio_lctrl+0x4e>
movb   $0x1,-0x12(%rbp)
jmp    1182 <sio_lctrl+0x52>
movb   $0x0,-0x12(%rbp)
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     119a <sio_lctrl+0x6a>
movb   $0x1,-0x11(%rbp)
jmp    119e <sio_lctrl+0x6e>
movb   $0x0,-0x11(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     11c4 <sio_lctrl+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <sio_lctrl+0xa3>
mov    -0x8(%rbp),%edi
movzbl -0x14(%rbp),%esi
call   1040 <SioWaitAck@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sio_lctrl>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
call   1050 <SioCmdportFd@plt>
cmp    $0xffffffff,%eax
je     1181 <sio_lctrl+0x51>
mov    %eax,%ebx
movw   $0x212,(%rsp)
mov    %ebp,%eax
and    $0x1,%al
mov    %al,0x2(%rsp)
shr    %bpl
and    $0x1,%bpl
mov    %bpl,0x3(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1181 <sio_lctrl+0x51>
mov    %ebx,%edi
mov    $0x12,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <SioWaitAck@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @sio_lctrl(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [8 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = call i32 @SioCmdportFd(i32 %7)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %40
11: ; preds = %2
%12 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
store i8 18, i8* %12
%13 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 1
store i8 2, i8* %13
%14 = load i32, i32* %5
%15 = and i32 %14, 1
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %11
%18 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 2
store i8 1, i8* %18
br label %21
19: ; preds = %11
%20 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 2
store i8 0, i8* %20
br label %21
21: ; preds = %19, %17
%22 = load i32, i32* %5
%23 = and i32 %22, 2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 3
store i8 1, i8* %26
br label %29
27: ; preds = %21
%28 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 3
store i8 0, i8* %28
br label %29
29: ; preds = %27, %25
%30 = load i32, i32* %4
%31 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%32 = call i32 @write(i32 %30, i8* %31, i32 4)
%33 = icmp ne i32 %32, 4
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %3
br label %40
35: ; preds = %29
%36 = load i32, i32* %4
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
%38 = load i8, i8* %37
%39 = call i32 @SioWaitAck(i32 %36, i8 zeroext %38)
store i32 %39, i32* %3
br label %40
40: ; preds = %35, %34, %10
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
define i32 @sio_lctrl(i32 %0, i32 %1) {
%3 = alloca [8 x i8]
%4 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @SioCmdportFd(i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %20, label %7
7: ; preds = %2
store i8 18, i8* %4
%8 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 1
store i8 2, i8* %8
%9 = trunc i32 %1 to i8
%10 = and i8 %9, 1
%11 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 2
store i8 %10, i8* %11
%12 = trunc i32 %1 to i8
%13 = lshr i8 %12, 1
%14 = and i8 %13, 1
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %3, i64 0, i64 3
store i8 %14, i8* %15
%16 = call i32 @write(i32 %5, i8* %4, i32 4)
%17 = icmp eq i32 %16, 4
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = tail call i32 @SioWaitAck(i32 %5, i8 zeroext 18)
br label %20
20: ; preds = %7, %2, %18
%21 = phi i32 [ %19, %18 ], [ -1, %2 ], [ -1, %7 ]
ret i32 %21
}