#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 /*<<< orphan*/ TIOCSTI;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd;
  char *s;
  if (argc != 3) {
    fprintf(stderr, "call: sti tty text\n");
    exit(1);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror(argv[1]);
    fprintf(stderr, "sti: could not open tty\n");
    exit(1);
  }
  s = argv[2];
  while (*s) {
    if (ioctl(fd, TIOCSTI, s)) {
      perror("TIOCSTI");
      fprintf(stderr, "sti: TIOCSTI ioctl failed\n");
      exit(1);
    }
    s++;
  }
  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)
cmpl   $0x3,-0x8(%rbp)
je     118f <main+0x3f>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11de <main+0x8e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <perror@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1252 <main+0x102>
mov    -0x14(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1241 <main+0xf1>
lea    0xe10(%rip),%rdi
call   1060 <perror@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11ea <main+0x9a>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x3,%edi
jne    11de <main+0x8e>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11f0 <main+0xa0>
mov    0x10(%rbx),%rbx
cmpb   $0x0,(%rbx)
je     11ac <main+0x5c>
mov    %eax,%ebp
mov    0x2e60(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r14),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11b3 <main+0x63>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1190 <main+0x40>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
jmp    11cf <main+0x7f>
mov    0x8(%rbx),%rdi
call   1060 <perror@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
jmp    11cf <main+0x7f>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"call: sti tty text\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"sti: could not open tty\0A\00"
@TIOCSTI = global i32 0
@.str.2 = [8 x i8] c"TIOCSTI\00"
@.str.3 = [27 x i8] c"sti: TIOCSTI ioctl failed\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
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 3
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %14
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @perror(i8* %25)
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %14
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
store i8* %33, i8** %7
br label %34
34: ; preds = %49, %30
%35 = load i8*, i8** %7
%36 = load i8, i8* %35
%37 = icmp ne i8 %36, 0
br i1 %37, label %38, label %52
38: ; preds = %34
%39 = load i32, i32* %6
%40 = load i32, i32* @TIOCSTI
%41 = load i8*, i8** %7
%42 = call i64 @ioctl(i32 %39, i32 %40, i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %38
%50 = load i8*, i8** %7
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %7
br label %34
52: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"call: sti tty text\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [25 x i8] c"sti: could not open tty\0A\00"
@TIOCSTI = global i32 0
@.str.2 = [8 x i8] c"TIOCSTI\00"
@.str.3 = [27 x i8] c"sti: TIOCSTI ioctl failed\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = load i8*, i8** %9
%16 = tail call i32 @perror(i8* %15)
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %8
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %39, label %28
25: ; preds = %28
%26 = load i8, i8* %33
%27 = icmp eq i8 %26, 0
br i1 %27, label %39, label %28
28: ; preds = %20, %25
%29 = phi i8* [ %33, %25 ], [ %22, %20 ]
%30 = load i32, i32* @TIOCSTI
%31 = tail call i64 @ioctl(i32 %12, i32 %30, i8* %29)
%32 = icmp eq i64 %31, 0
%33 = getelementptr inbounds i8, i8* %29, i64 1
br i1 %32, label %25, label %34
34: ; preds = %28
%35 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%36 = load i32, i32* @stderr
%37 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @exit(i32 1)
unreachable
39: ; preds = %25, %20
ret i32 0
}