#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*/ snprintf(char *, int, char *, int);
int system(char *);
int tty_ready(int tty) {
  char command[255] = {'\0'};
  snprintf(
      command, sizeof(command),
      "ps aux | grep tty%d | grep -v '\\(agetty\\|grep\\)' >/dev/null 2>&1",
      tty);
  int status = system(command);
  status = ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                 : WEXITSTATUS(status));
  return status;
}


x86 assembly (clang, -O0):
<tty_ready>:
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    0xe79(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1030 <system@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0xffffffff,-0x114(%rbp)
je     11bf <tty_ready+0x6f>
mov    -0x114(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    11d3 <tty_ready+0x83>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x118(%rbp)
jmp    11e4 <tty_ready+0x94>
mov    -0x114(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x118(%rbp)
mov    -0x118(%rbp),%eax
mov    %eax,-0x114(%rbp)
mov    -0x114(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_ready>:
push   %rbx
sub    $0x100,%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    0xe45(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <system@plt>
cmp    $0xffffffff,%eax
je     11ee <tty_ready+0xae>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
je     11ee <tty_ready+0xae>
mov    %ebx,%edi
call   1050 <WEXITSTATUS@plt>
jmp    11f7 <tty_ready+0xb7>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [65 x i8] c"ps aux | grep tty%d | grep -v '\\(agetty\\|grep\\)' >/dev/null 2>&1\00"
@EXIT_FAILURE = global i32 0
define i32 @tty_ready(i32 %0) {
%2 = alloca i32
%3 = alloca [255 x i8]
%4 = alloca i32
store i32 %0, i32* %2
%5 = bitcast [255 x i8]* %3 to i8*
%6 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @snprintf(i8* %6, i32 255, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%10 = call i32 @system(i8* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i32, i32* %4
%15 = call i32 @WIFEXITED(i32 %14)
%16 = icmp ne i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13, %1
%18 = load i32, i32* @EXIT_FAILURE
br label %22
19: ; preds = %13
%20 = load i32, i32* %4
%21 = call i32 @WEXITSTATUS(i32 %20)
br label %22
22: ; preds = %19, %17
%23 = phi i32 [ %18, %17 ], [ %21, %19 ]
store i32 %23, i32* %4
%24 = load i32, i32* %4
ret i32 %24
}

LLVM IR (-O2):
@.str = [65 x i8] c"ps aux | grep tty%d | grep -v '\\(agetty\\|grep\\)' >/dev/null 2>&1\00"
@EXIT_FAILURE = global i32 0
define i32 @tty_ready(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 ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32 @system(i8* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %10, label %7
7: ; preds = %1
%8 = call i32 @WIFEXITED(i32 %5)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %1
%11 = load i32, i32* @EXIT_FAILURE
br label %14
12: ; preds = %7
%13 = call i32 @WEXITSTATUS(i32 %5)
br label %14
14: ; preds = %12, %10
%15 = phi i32 [ %11, %10 ], [ %13, %12 ]
ret i32 %15
}