#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_ioctl(int fd, int baud, int mode) {
  unsigned char buf[4];
  if ((fd = SioCmdportFd(fd)) == -1)
    return (-1);
  buf[0] = 16;
  buf[1] = 2;
  buf[2] = (unsigned char)baud;
  buf[3] = (unsigned char)mode;
  if (write(fd, buf, 4) != 4)
    return (-1);
  return (SioWaitAck(fd, buf[0]));
}


x86 assembly (clang, -O0):
<sio_ioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <SioCmdportFd@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1161 <sio_ioctl+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <sio_ioctl+0x7a>
movb   $0x10,-0x14(%rbp)
movb   $0x2,-0x13(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,-0x12(%rbp)
mov    -0x10(%rbp),%eax
mov    %al,-0x11(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     119b <sio_ioctl+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <sio_ioctl+0x7a>
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_ioctl>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
call   1050 <SioCmdportFd@plt>
cmp    $0xffffffff,%eax
je     1184 <sio_ioctl+0x54>
mov    %eax,%ebx
movw   $0x210,0xc(%rsp)
mov    %bpl,0xe(%rsp)
mov    %r14b,0xf(%rsp)
lea    0xc(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1184 <sio_ioctl+0x54>
mov    %ebx,%edi
mov    $0x10,%esi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <SioWaitAck@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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