#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, int);
int /*<<< orphan*/ perror(char *);
int lock_tty(int fd) {
  struct termios term;
  memset(&term, 0xff, sizeof(term));
  errno = 0;
  if (ioctl(fd, TIOCSLCKTRMIOS, &term) < 0) {
    perror("ioctl error");
  }
  return errno;
}


x86 assembly (clang, -O0):
<lock_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x8(%rbp),%rdi
mov    $0xff,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e83(%rip),%rax
movl   $0x0,(%rax)
mov    -0x4(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    1186 <lock_tty+0x56>
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_tty>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xff,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    0x2e7f(%rip),%r15
movl   $0x0,(%r15)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <ioctl@plt>
test   %rax,%rax
js     1185 <lock_tty+0x55>
mov    (%r15),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe74(%rip),%rdi
call   1050 <perror@plt>
jmp    1178 <lock_tty+0x48>

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 @lock_tty(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.termios
store i32 %0, i32* %2
%4 = call i32 @memset(%struct.termios* %3, i32 255, 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 @lock_tty(i32 %0) {
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %2 to i8*
%4 = call i32 @memset(%struct.termios* %2, i32 255, 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
}