#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*/ O_RDONLY;
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *ttyname(int);
int main() {
  int fd = open("ttyname", O_RDONLY);
  if (fd == -1) {
    perror("open");
    return errno;
  }
  char *name = ttyname(fd);
  if (name != NULL) {
    printf("ttyname %s\n", name);
  } else {
    perror("ttyname");
  }
  name = ttyname(open("/dev/tty", O_RDONLY));
  if (name != NULL) {
    printf("ttyname %s\n", name);
  } else {
    perror("ttyname");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118e <main+0x4e>
lea    0xe90(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1219 <main+0xd9>
mov    -0x8(%rbp),%edi
call   1040 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ba <main+0x7a>
mov    -0x10(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1030 <printf@plt>
jmp    11c6 <main+0x86>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe43(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%edi
call   1040 <ttyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1206 <main+0xc6>
mov    -0x10(%rbp),%rsi
lea    0xe11(%rip),%rdi
call   1030 <printf@plt>
jmp    1212 <main+0xd2>
lea    0xdf3(%rip),%rdi
call   1060 <perror@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rbx
mov    (%rbx),%esi
lea    0xeaf(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11a3 <main+0x63>
mov    %eax,%edi
call   1040 <ttyname@plt>
test   %rax,%rax
je     11ba <main+0x7a>
lea    0xe9f(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    (%rbx),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%edi
call   1040 <ttyname@plt>
test   %rax,%rax
je     11c8 <main+0x88>
lea    0xe76(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
jmp    1176 <main+0x36>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
jmp    119f <main+0x5f>

LLVM IR (-O0):
@.str = [8 x i8] c"ttyname\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"ttyname %s\0A\00"
@.str.3 = [9 x i8] c"/dev/tty\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @errno
store i32 %10, i32* %1
br label %33
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i8* @ttyname(i32 %12)
store i8* %13, i8** %3
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %21
19: ; preds = %11
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call i8* @ttyname(i32 %23)
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i8*, i8** %3
%29 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %32
30: ; preds = %21
%31 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
store i32 0, i32* %1
br label %33
33: ; preds = %32, %8
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [8 x i8] c"ttyname\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"ttyname %s\0A\00"
@.str.3 = [9 x i8] c"/dev/tty\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @errno
br label %23
7: ; preds = %0
%8 = tail call i8* @ttyname(i32 %2)
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %10
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = tail call i8* @ttyname(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %14
%20 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %17)
br label %23
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %19, %21, %4
%24 = phi i32 [ %6, %4 ], [ 0, %21 ], [ 0, %19 ]
ret i32 %24
}