#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 */
struct termios2 {
  int c_iflag;
  int c_cflag;
  unsigned int c_ospeed;
  unsigned int c_ispeed;
  scalar_t__ c_oflag;
};

/* Variables and functions */
int BOTHER;
int CLOCAL;
int CS8;
int IGNBRK;
int IGNPAR;
int /*<<< orphan*/ TCGETS2;
int /*<<< orphan*/ TCSETS2;
int ioctl(int, int /*<<< orphan*/, struct termios2 *);
int /*<<< orphan*/ printf(char *);
int matrix_set_custom_baud(int fd, unsigned int baudrate) {
  struct termios2 t;
  if (ioctl(fd, TCGETS2, &t) == -1) {
    printf("Error on ioctl TCGETS2\r\n");
    return -1;
  }
  t.c_iflag = IGNBRK | IGNPAR;
  t.c_oflag = 0;
  t.c_cflag = BOTHER | CS8 | CLOCAL;
  t.c_ospeed = t.c_ispeed = baudrate;
  if (ioctl(fd, TCSETS2, &t)) {
    printf("Error on ioctl TCGETS2\r\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<matrix_set_custom_baud>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    1164 <matrix_set_custom_baud+0x44>
lea    0xead(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <matrix_set_custom_baud+0xc5>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    0x2e44(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%eax
mov    0x2e47(%rip),%rcx
or     (%rcx),%eax
mov    0x2e0e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%eax
je     11de <matrix_set_custom_baud+0xbe>
lea    0xe33(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <matrix_set_custom_baud+0xc5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrix_set_custom_baud>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eaf(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1040 <ioctl@plt>
cmp    $0xffffffff,%eax
je     119c <matrix_set_custom_baud+0x7c>
mov    0x2e79(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e63(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e31(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x4(%rsp)
mov    %ebp,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
je     11af <matrix_set_custom_baud+0x8f>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    11b1 <matrix_set_custom_baud+0x91>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios2 = type { i32, i32, i32, i32, i64 }
@TCGETS2 = global i32 0
@.str = [25 x i8] c"Error on ioctl TCGETS2\0D\0A\00"
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@BOTHER = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@TCSETS2 = global i32 0
define i32 @matrix_set_custom_baud(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.termios2
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @TCGETS2
%9 = call i32 @ioctl(i32 %7, i32 %8, %struct.termios2* %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
13: ; preds = %2
%14 = load i32, i32* @IGNBRK
%15 = load i32, i32* @IGNPAR
%16 = or i32 %14, %15
%17 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 0
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 4
store i64 0, i64* %18
%19 = load i32, i32* @BOTHER
%20 = load i32, i32* @CS8
%21 = or i32 %19, %20
%22 = load i32, i32* @CLOCAL
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* %5
%26 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 3
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.termios2, %struct.termios2* %6, i32 0, i32 2
store i32 %25, i32* %27
%28 = load i32, i32* %4
%29 = load i32, i32* @TCSETS2
%30 = call i32 @ioctl(i32 %28, i32 %29, %struct.termios2* %6)
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %13
%33 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
34: ; preds = %13
store i32 0, i32* %3
br label %35
35: ; preds = %34, %32, %11
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.termios2 = type { i32, i32, i32, i32, i64 }
@TCGETS2 = global i32 0
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@BOTHER = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@TCSETS2 = global i32 0
@str.1 = [24 x i8] c"Error on ioctl TCGETS2\0D\00"
define i32 @matrix_set_custom_baud(i32 %0, i32 %1) {
%3 = alloca %struct.termios2
%4 = bitcast %struct.termios2* %3 to i8*
%5 = load i32, i32* @TCGETS2
%6 = call i32 @ioctl(i32 %0, i32 %5, %struct.termios2* %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %25, label %8
8: ; preds = %2
%9 = load i32, i32* @IGNBRK
%10 = load i32, i32* @IGNPAR
%11 = or i32 %10, %9
%12 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 4
store i64 0, i64* %13
%14 = load i32, i32* @BOTHER
%15 = load i32, i32* @CS8
%16 = or i32 %15, %14
%17 = load i32, i32* @CLOCAL
%18 = or i32 %16, %17
%19 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 3
store i32 %1, i32* %20
%21 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 2
store i32 %1, i32* %21
%22 = load i32, i32* @TCSETS2
%23 = call i32 @ioctl(i32 %0, i32 %22, %struct.termios2* %3)
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %8, %2
%26 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.1, i64 0, i64 0), %8 ]
%27 = call i32 @puts(i8* dereferenceable(1) %26)
br label %28
28: ; preds = %25, %8
%29 = phi i32 [ 0, %8 ], [ -1, %25 ]
ret i32 %29
}