#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 BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s ttyname\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  int tty_fd = open(argv[1], O_WRONLY);
  if (tty_fd == -1) {
    fprintf(stderr, "Can not open \n");
    perror(argv[1]);
    exit(EXIT_FAILURE);
  }
  char buf[BUFSIZ];
  int size;
  while ((size = read(1, buf, BUFSIZ)) > 0) {
    if (size == -1) {
      fprintf(stderr, "Can not read from stdin\n");
      exit(EXIT_FAILURE);
    }
    if (size != write(tty_fd, buf, size)) {
      fprintf(stderr, "Can not write to %s\n", argv[1]);
      exit(EXIT_FAILURE);
    }
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ac <main+0x4c>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1201 <main+0xa1>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rsi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%edi
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    12d3 <main+0x173>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    127f <main+0x11f>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
je     12ce <main+0x16e>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    122d <main+0xcd>
mov    0x2cee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1239 <main+0xd9>
mov    0x8(%r14),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     124e <main+0xee>
mov    %eax,%r15d
mov    0x2e39(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%r12
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    $0x1,%edi
mov    %r12,%rsi
call   1040 <read@plt>
test   %eax,%eax
jle    11f8 <main+0x98>
mov    %eax,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %eax,%ebx
jne    1210 <main+0xb0>
mov    0x0(%r13),%edx
mov    $0x1,%edi
mov    %r12,%rsi
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jg     11d0 <main+0x70>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xdfe(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdb4(%rip),%rsi
jmp    1224 <main+0xc4>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%r14),%rdi
call   1070 <perror@plt>
jmp    122b <main+0xcb>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Usage: %s ttyname\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"Can not open \0A\00"
@BUFSIZ = global i32 0
@.str.2 = [25 x i8] c"Can not read from stdin\0A\00"
@.str.3 = [21 x i8] c"Can not write to %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_WRONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %37
28: ; preds = %20
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @perror(i8* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %20
%38 = load i32, i32* @BUFSIZ
%39 = zext i32 %38 to i64
%40 = call i8* @llvm.stacksave()
store i8* %40, i8** %7
%41 = alloca i8, i64 %39
store i64 %39, i64* %8
br label %42
42: ; preds = %68, %37
%43 = load i32, i32* @BUFSIZ
%44 = call i32 @read(i32 1, i8* %41, i32 %43)
store i32 %44, i32* %9
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %69
46: ; preds = %42
%47 = load i32, i32* %9
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %54
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %46
%55 = load i32, i32* %9
%56 = load i32, i32* %6
%57 = load i32, i32* %9
%58 = call i32 @write(i32 %56, i8* %41, i32 %57)
%59 = icmp ne i32 %55, %58
br i1 %59, label %60, label %68
60: ; preds = %54
%61 = load i32, i32* @stderr
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 1
%64 = load i8*, i8** %63
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %54
br label %42
69: ; preds = %42
%70 = load i32, i32* @EXIT_SUCCESS
store i32 %70, i32* %3
%71 = load i8*, i8** %7
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Usage: %s ttyname\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [15 x i8] c"Can not open \0A\00"
@BUFSIZ = global i32 0
@.str.3 = [21 x i8] c"Can not write to %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %10
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%19 = load i8*, i8** %11
%20 = tail call i32 @perror(i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %10
%24 = load i32, i32* @BUFSIZ
%25 = zext i32 %24 to i64
%26 = alloca i8, i64 %25
%27 = call i32 @read(i32 1, i8* %26, i32 %24)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %43
29: ; preds = %23, %33
%30 = phi i32 [ %35, %33 ], [ %27, %23 ]
%31 = call i32 @write(i32 %14, i8* %26, i32 %30)
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @BUFSIZ
%35 = call i32 @read(i32 1, i8* %26, i32 %34)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %29, label %43
37: ; preds = %29
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %11
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %39)
%41 = load i32, i32* @EXIT_FAILURE
%42 = tail call i32 @exit(i32 %41)
unreachable
43: ; preds = %33, %23
%44 = load i32, i32* @EXIT_SUCCESS
ret i32 %44
}