#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*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
char *ttyname(int);
int main(int argc, char *argv[]) {
  int i;
  char *name;
  for (i = 0; i < 3; ++i) {
    name = ttyname(i);
    if (name) {
      printf("ttyname%d = %s\n", i, name);
    } else {
      perror("ttyname");
    }
  }
  return 0;
}


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   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    11a5 <main+0x75>
mov    -0x14(%rbp),%edi
call   1040 <ttyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1186 <main+0x56>
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
jmp    1192 <main+0x62>
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
jmp    1197 <main+0x67>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    114d <main+0x1d>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %edi,%edi
call   1040 <ttyname@plt>
test   %rax,%rax
je     1198 <main+0x68>
lea    0xebc(%rip),%rdi
xor    %esi,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    $0x1,%edi
call   1040 <ttyname@plt>
test   %rax,%rax
je     11a6 <main+0x76>
lea    0xe9c(%rip),%rdi
mov    $0x1,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    $0x2,%edi
call   1040 <ttyname@plt>
test   %rax,%rax
je     11b4 <main+0x84>
lea    0xe79(%rip),%rdi
mov    $0x2,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xe71(%rip),%rdi
call   1050 <perror@plt>
jmp    114e <main+0x1e>
lea    0xe63(%rip),%rdi
call   1050 <perror@plt>
jmp    1171 <main+0x41>
lea    0xe55(%rip),%rdi
call   1050 <perror@plt>
jmp    1194 <main+0x64>

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

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