#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ puts(char *);
char *ttyname(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  bool sflag = true;
  if (argc > 1 && (argv[1][0] == '-'))
    switch (argv[1][1]) {
    case 's':
      sflag = false;
      break;
    default:
      printf("tty: invalid option -- '%c'\n"
             "usage: tty [-s]\n",
             argv[1][1]);
      exit(EXIT_FAILURE);
    }
  char *tty = ttyname(STDIN_FILENO);
  if (!tty)
    exit(EXIT_FAILURE);
  if (sflag)
    puts(tty);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11ca <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    11ca <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x1(%rax),%al
sub    $0x73,%al
jne    119f <main+0x5f>
jmp    1193 <main+0x53>
movl   $0x0,-0x14(%rbp)
jmp    11c5 <main+0x85>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
lea    0xe52(%rip),%rdi
movsbl 0x1(%rax),%esi
call   1040 <printf@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    11ca <main+0x8a>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1050 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <main+0xb5>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
je     1208 <main+0xc8>
mov    -0x20(%rbp),%rdi
call   1030 <puts@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rax
cmp    $0x2,%edi
jl     116b <main+0x2b>
mov    0x8(%rsi),%rax
cmpb   $0x2d,(%rax)
jne    116b <main+0x2b>
mov    0x1(%rax),%al
cmp    $0x73,%al
jne    1194 <main+0x54>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
call   1050 <ttyname@plt>
test   %rax,%rax
jne    1186 <main+0x46>
jmp    11a3 <main+0x63>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1050 <ttyname@plt>
test   %rax,%rax
je     11a3 <main+0x63>
mov    %rax,%rdi
call   1030 <puts@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
movsbl %al,%esi
lea    0xe62(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"tty: invalid option -- '%c'\0Ausage: tty [-s]\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %36
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 45
br i1 %17, label %18, label %36
18: ; preds = %10
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 1
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
switch i32 %24, label %26 [
i32 115, label %25
]
25: ; preds = %18
store i32 0, i32* %6
br label %35
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8, i8* %29, i64 1
%31 = load i8, i8* %30
%32 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8 signext %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %25
br label %36
36: ; preds = %35, %10, %2
%37 = load i32, i32* @STDIN_FILENO
%38 = call i8* @ttyname(i32 %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %44, label %41
41: ; preds = %36
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i8*, i8** %7
%49 = call i32 @puts(i8* %48)
br label %50
50: ; preds = %47, %44
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
}

LLVM IR (-O2):
@.str = [45 x i8] c"tty: invalid option -- '%c'\0Ausage: tty [-s]\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %17
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 45
br i1 %8, label %9, label %17
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 115
br i1 %12, label %21, label %13
13: ; preds = %9
%14 = tail call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8 signext %11)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %4, %2
%18 = load i32, i32* @STDIN_FILENO
%19 = tail call i8* @ttyname(i32 %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %25, label %28
21: ; preds = %9
%22 = load i32, i32* @STDIN_FILENO
%23 = tail call i8* @ttyname(i32 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %21, %17
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
%29 = tail call i32 @puts(i8* %19)
br label %30
30: ; preds = %21, %28
%31 = load i32, i32* @EXIT_SUCCESS
%32 = tail call i32 @exit(i32 %31)
unreachable
}