#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 */
typedef int /*<<< orphan*/ term;
struct termios {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ TIOCSLCKTRMIOS;
int errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int unlock_tty(int fd) {
  struct termios term;
  memset(&term, 0, sizeof(term));
  errno = 0;
  if (ioctl(fd, TIOCSLCKTRMIOS, &term) < 0) {
    perror("ioctl error");
  }
  return errno;
}


x86 assembly (clang, -O0):
<unlock_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e86(%rip),%rax
movl   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1183 <unlock_tty+0x53>
lea    0xe82(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlock_tty>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e82(%rip),%r15
movl   $0x0,(%r15)
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1182 <unlock_tty+0x52>
mov    (%r15),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
jmp    1175 <unlock_tty+0x45>

LLVM IR (-O0):
%struct.termios = type { i32 }
@errno = global i32 0
@TIOCSLCKTRMIOS = global i32 0
@.str = [12 x i8] c"ioctl error\00"
define i32 @unlock_tty(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = call i32 @memset(%struct.termios* %3, i32 0, i32 4)
store i32 0, i32* @errno
%5 = load i32, i32* %2
%6 = load i32, i32* @TIOCSLCKTRMIOS
%7 = call i64 @ioctl(i32 %5, i32 %6, %struct.termios* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i32, i32* @errno
ret i32 %12
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@errno = global i32 0
@TIOCSLCKTRMIOS = global i32 0
@.str = [12 x i8] c"ioctl error\00"
define i32 @unlock_tty(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @memset(%struct.termios* %2, i32 0, i32 4)
store i32 0, i32* @errno
%5 = load i32, i32* @TIOCSLCKTRMIOS
%6 = call i64 @ioctl(i32 %0, i32 %5, %struct.termios* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* @errno
ret i32 %11
}