#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*/ tty;

/* Variables and functions */
int getopt(int, char **, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ tty_func();
scalar_t__ ttyname_r(int /*<<< orphan*/, char *, int);
int main(int argv, char *argc[]) {
  int opt;
  while ((opt = getopt(argv, argc, "s")) != -1) {
    switch (opt) {
    case 's':
      tty_func();
      break;
    }
  }
  char tty[1024];
  if (ttyname_r(0, tty, sizeof(tty)))
    tty[0] = '\0';
  printf("tty:%s\n", tty);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe99(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1195 <main+0x55>
mov    -0x14(%rbp),%eax
sub    $0x73,%eax
jne    1190 <main+0x50>
jmp    1189 <main+0x49>
mov    $0x0,%al
call   1030 <tty_func@plt>
jmp    1159 <main+0x19>
lea    -0x420(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1050 <ttyname_r@plt>
cmp    $0x0,%rax
je     11b9 <main+0x79>
movb   $0x0,-0x420(%rbp)
lea    -0x420(%rbp),%rsi
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xea9(%rip),%r14
jmp    1167 <main+0x27>
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <tty_func@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <getopt@plt>
cmp    $0x73,%eax
je     1160 <main+0x20>
cmp    $0xffffffff,%eax
jne    1167 <main+0x27>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1050 <ttyname_r@plt>
test   %rax,%rax
je     1196 <main+0x56>
movb   $0x0,(%rsp)
lea    0xe65(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"s\00"
@.str.1 = [8 x i8] c"tty:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
br label %8
8: ; preds = %17, %2
%9 = load i32, i32* %4
%10 = load i8**, i8*** %5
%11 = call i32 @getopt(i32 %9, i8** %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %6
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %8
%14 = load i32, i32* %6
switch i32 %14, label %17 [
i32 115, label %15
]
15: ; preds = %13
%16 = call i32 (...) @tty_func()
br label %17
17: ; preds = %13, %15
br label %8
18: ; preds = %8
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%20 = call i64 @ttyname_r(i32 0, i8* %19, i32 1024)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
store i8 0, i8* %23
br label %24
24: ; preds = %22, %18
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %25)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"s\00"
@.str.1 = [8 x i8] c"tty:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [1024 x i8]
br label %4
4: ; preds = %6, %2
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
switch i32 %5, label %6 [
i32 -1, label %9
i32 115, label %7
]
6: ; preds = %4, %7
br label %4
7: ; preds = %4
%8 = tail call i32 (...) @tty_func()
br label %6
9: ; preds = %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%11 = call i64 @ttyname_r(i32 0, i8* %10, i32 1024)
%12 = icmp eq i64 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %9
store i8 0, i8* %10
br label %14
14: ; preds = %13, %9
%15 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %10)
ret i32 0
}