#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 termios {
  int dummy;
};

/* Variables and functions */
int EINVAL;
int /*<<< orphan*/ TCGETA;
int errno;
int ioctl(int, int /*<<< orphan*/, struct termios *);
int tcgetattr(int fd, struct termios *tio) {
  int error;
  if (tio == NULL) {
    errno = EINVAL;
    return -1;
  }
  error = ioctl(fd, TCGETA, tio);
  if (error < 0) {
    errno = -error;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcgetattr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1148 <tcgetattr+0x38>
mov    0x2e9f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9e(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    118b <tcgetattr+0x7b>
mov    -0x8(%rbp),%edi
mov    0x2e96(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1184 <tcgetattr+0x74>
xor    %ecx,%ecx
sub    -0x14(%rbp),%ecx
mov    0x2e62(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    118b <tcgetattr+0x7b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcgetattr>:
push   %rax
test   %rsi,%rsi
je     1131 <tcgetattr+0x21>
mov    %rsi,%rdx
mov    0x2ec8(%rip),%rax
mov    (%rax),%esi
call   1030 <ioctl@plt>
xor    %ecx,%ecx
test   %eax,%eax
jns    1148 <tcgetattr+0x38>
neg    %eax
jmp    113a <tcgetattr+0x2a>
mov    0x2e98(%rip),%rax
mov    (%rax),%eax
mov    0x2e97(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ecx
mov    %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@TCGETA = global i32 0
define i32 @tcgetattr(i32 %0, %struct.termios* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.termios*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.termios* %1, %struct.termios** %5
%7 = load %struct.termios*, %struct.termios** %5
%8 = icmp eq %struct.termios* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store i32 -1, i32* %3
br label %22
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i32, i32* @TCGETA
%14 = load %struct.termios*, %struct.termios** %5
%15 = call i32 @ioctl(i32 %12, i32 %13, %struct.termios* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* %6
%20 = sub nsw i32 0, %19
store i32 %20, i32* @errno
store i32 -1, i32* %3
br label %22
21: ; preds = %11
store i32 0, i32* %3
br label %22
22: ; preds = %21, %18, %9
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@TCGETA = global i32 0
define i32 @tcgetattr(i32 %0, %struct.termios* %1) {
%3 = icmp eq %struct.termios* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @EINVAL
br label %12
6: ; preds = %2
%7 = load i32, i32* @TCGETA
%8 = tail call i32 @ioctl(i32 %0, i32 %7, %struct.termios* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = sub nsw i32 0, %8
br label %12
12: ; preds = %4, %10
%13 = phi i32 [ %11, %10 ], [ %5, %4 ]
store i32 %13, i32* @errno
br label %14
14: ; preds = %12, %6
%15 = phi i32 [ 0, %6 ], [ -1, %12 ]
ret i32 %15
}