#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 */
typedef int /*<<< orphan*/ command;

/* Variables and functions */
int EXIT_FAILURE;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int system(char *);
int tty_wait(int tty) {
  char command[255] = {'\0'};
  snprintf(command, sizeof(command),
           "cat /sys/class/tty/tty0/active | grep -E 'tty%d$' >/dev/null 2>&1",
           tty);
  int status = EXIT_FAILURE;
  for (int i = 0; i < 10; ++i) {
    status = system(command);
    status = ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                   : WEXITSTATUS(status));
    if (status == 0) {
      break;
    }
    sleep(1);
  }
  return status;
}


x86 assembly (clang, -O0):
<tty_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0xff,%edx
call   1050 <memset@plt>
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0xff,%esi
lea    0xe69(%rip),%rdx
call   1040 <snprintf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x114(%rbp)
movl   $0x0,-0x118(%rbp)
cmpl   $0xa,-0x118(%rbp)
jge    1256 <tty_wait+0xf6>
lea    -0x110(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
je     11f5 <tty_wait+0x95>
mov    -0x114(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1209 <tty_wait+0xa9>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x11c(%rbp)
jmp    121a <tty_wait+0xba>
mov    -0x114(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x11c(%rbp)
mov    -0x11c(%rbp),%eax
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jne    1238 <tty_wait+0xd8>
jmp    1256 <tty_wait+0xf6>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x118(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x118(%rbp)
jmp    11b5 <tty_wait+0x55>
mov    -0x114(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_wait>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %edi,%ecx
xorps  %xmm0,%xmm0
movups %xmm0,0xef(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xe30(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xff,%esi
call   1040 <snprintf@plt>
mov    $0xa,%ebx
mov    0x2df4(%rip),%r15
nopl   0x0(%rax)
mov    %r14,%rdi
call   1030 <system@plt>
cmp    $0xffffffff,%eax
je     1220 <tty_wait+0xd0>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
je     1220 <tty_wait+0xd0>
mov    %ebp,%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebp
test   %ebp,%ebp
jne    1227 <tty_wait+0xd7>
jmp    1236 <tty_wait+0xe6>
nopl   0x0(%rax)
mov    (%r15),%ebp
test   %ebp,%ebp
je     1236 <tty_wait+0xe6>
mov    $0x1,%edi
call   1070 <sleep@plt>
add    $0xffffffff,%ebx
jne    11f0 <tty_wait+0xa0>
mov    %ebp,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [66 x i8] c"cat /sys/class/tty/tty0/active | grep -E 'tty%d$' >/dev/null 2>&1\00"
@EXIT_FAILURE = global i32 0
define i32 @tty_wait(i32 %0) {
%2 = alloca i32
%3 = alloca [255 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = bitcast [255 x i8]* %3 to i8*
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i32 @snprintf(i8* %7, i32 255, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @EXIT_FAILURE
store i32 %10, i32* %4
store i32 0, i32* %5
br label %11
11: ; preds = %35, %1
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 10
br i1 %13, label %14, label %38
14: ; preds = %11
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%16 = call i32 @system(i8* %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %14
%20 = load i32, i32* %4
%21 = call i32 @WIFEXITED(i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %19, %14
%24 = load i32, i32* @EXIT_FAILURE
br label %28
25: ; preds = %19
%26 = load i32, i32* %4
%27 = call i32 @WEXITSTATUS(i32 %26)
br label %28
28: ; preds = %25, %23
%29 = phi i32 [ %24, %23 ], [ %27, %25 ]
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %38
33: ; preds = %28
%34 = call i32 @sleep(i32 1)
br label %35
35: ; preds = %33
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %11
38: ; preds = %32, %11
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
@.str = [66 x i8] c"cat /sys/class/tty/tty0/active | grep -E 'tty%d$' >/dev/null 2>&1\00"
@EXIT_FAILURE = global i32 0
define i32 @tty_wait(i32 %0) {
%2 = alloca [255 x i8]
%3 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 255, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %5
5: ; preds = %1, %19
%6 = phi i32 [ 0, %1 ], [ %21, %19 ]
%7 = call i32 @system(i8* %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = call i32 @WIFEXITED(i32 %7)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9, %5
%13 = load i32, i32* @EXIT_FAILURE
br label %16
14: ; preds = %9
%15 = call i32 @WEXITSTATUS(i32 %7)
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ %13, %12 ], [ %15, %14 ]
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = call i32 @sleep(i32 1)
%21 = add nuw nsw i32 %6, 1
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %5
23: ; preds = %16, %19
ret i32 %17
}