#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;
scalar_t__ isatty(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
char *ttyname(int);
int opentty(char *tty, int fd) {
  fd = open(tty, O_RDONLY);
  printf("%s", tty);
  if (isatty(fd)) {
    printf("is a tty.\n");
    printf("ttyname = %s \n", ttyname(fd));
    return 1;
  } else {
    printf("is not a tty\n");
    return 0;
  }
}


x86 assembly (clang, -O0):
<opentty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <isatty@plt>
cmp    $0x0,%rax
je     11bb <opentty+0x7b>
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <ttyname@plt>
mov    %rax,%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11d0 <opentty+0x90>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentty>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe92(%rip),%rdi
xor    %r14d,%r14d
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1040 <isatty@plt>
test   %rax,%rax
je     11b3 <opentty+0x63>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <ttyname@plt>
lea    0xe62(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r14d
jmp    11bf <opentty+0x6f>
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.1 = [11 x i8] c"is a tty.\0A\00"
@.str.2 = [15 x i8] c"ttyname = %s \0A\00"
@.str.3 = [14 x i8] c"is not a tty\0A\00"
define i32 @opentty(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%6 = load i8*, i8** %4
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %5
%9 = load i8*, i8** %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* %5
%12 = call i64 @isatty(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %5
%17 = call i8* @ttyname(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %21
21: ; preds = %19, %14
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%s\00"
@.str.2 = [15 x i8] c"ttyname = %s \0A\00"
@str = [13 x i8] c"is not a tty\00"
@str.4 = [10 x i8] c"is a tty.\00"
define i32 @opentty(i8* %0, i32 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call i64 @isatty(i32 %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.4, i64 0, i64 0))
%10 = tail call i8* @ttyname(i32 %4)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = phi i32 [ 1, %8 ], [ 0, %12 ]
ret i32 %15
}