#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 atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *ttyname(int);
int main(int argc, char *argv[]) {
  int fd;
  char *p;
  fd = 0;
  if (argc >= 2)
    fd = atoi(argv[1]);
  p = ttyname(fd);
  if (!p) {
    fprintf(stderr, "ttyname: %s\n", strerror(errno));
    return 0;
  }
  printf("%s\n", p);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1187 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <main+0x86>
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11ed <main+0x9d>
mov    -0x20(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    %edi,%eax
xor    %edi,%edi
cmp    $0x2,%eax
jl     1165 <main+0x15>
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%edi
call   1050 <ttyname@plt>
test   %rax,%rax
je     1185 <main+0x35>
lea    0xe93(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0x1,%eax
pop    %rbx
ret
mov    0x2e54(%rip),%rax
mov    (%rax),%ebx
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe5d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"ttyname: %s\0A\00"
@errno = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
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 0, i32* %6
%8 = load i32, i32* %4
%9 = icmp sge i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @atoi(i8* %13)
store i32 %14, i32* %6
br label %15
15: ; preds = %10, %2
%16 = load i32, i32* %6
%17 = call i8* @ttyname(i32 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %23)
store i32 0, i32* %3
br label %28
25: ; preds = %15
%26 = load i8*, i8** %7
%27 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %26)
store i32 1, i32* %3
br label %28
28: ; preds = %25, %20
%29 = load i32, i32* %3
ret i32 %29
}

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