#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ cmd_parse(int, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main() {
  int ioctl_fd;
  char cmd[512];
  char *device_path = "/dev/ksh";
  if ((ioctl_fd = open(device_path, O_RDWR)) < 0) {
    printf("Error opening character device at %s\n", device_path);
    perror("");
    exit(EXIT_FAILURE);
  }
  while (1) {
    printf("%s", "ksh> ");
    fflush(stdout);
    cmd_parse(ioctl_fd, fgets(cmd, 512, stdin));
  }
  close(ioctl_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x218(%rbp)
mov    -0x218(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11de <main+0x6e>
mov    -0x218(%rbp),%rsi
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    11e3 <main+0x73>
lea    0xe45(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1030 <printf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x21c(%rbp)
lea    -0x210(%rbp),%rdi
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1040 <fgets@plt>
mov    -0x21c(%rbp),%edi
mov    %eax,%esi
call   1060 <cmd_parse@plt>
jmp    11e3 <main+0x73>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11ef <main+0x7f>
mov    %eax,%ebx
lea    0xe8c(%rip),%r14
lea    0xe88(%rip),%r15
mov    0x2e17(%rip),%r13
mov    0x2e28(%rip),%rbp
mov    %rsp,%r12
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <printf@plt>
mov    0x0(%r13),%edi
call   1050 <fflush@plt>
mov    0x0(%rbp),%edx
mov    %r12,%rdi
mov    $0x200,%esi
call   1040 <fgets@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <cmd_parse@plt>
jmp    11c0 <main+0x50>
lea    0xe13(%rip),%rdi
lea    0xe03(%rip),%rsi
call   1030 <printf@plt>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/ksh\00"
@O_RDWR = global i32 0
@.str.1 = [38 x i8] c"Error opening character device at %s\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [6 x i8] c"ksh> \00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [512 x i8]
%4 = alloca i8*
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %0
%10 = load i8*, i8** %4
%11 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
br label %16
16: ; preds = %15, %16
%17 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = load i32, i32* %2
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* @stdin
%23 = call i32 @fgets(i8* %21, i32 512, i32 %22)
%24 = call i32 @cmd_parse(i32 %20, i32 %23)
br label %16
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/ksh\00"
@O_RDWR = global i32 0
@.str.1 = [38 x i8] c"Error opening character device at %s\0A\00"
@.str.2 = [1 x i8] zeroinitializer
@EXIT_FAILURE = global i32 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [6 x i8] c"ksh> \00"
@stdout = global i32 0
@stdin = global i32 0
define i32 @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %0, %11
%12 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%13 = load i32, i32* @stdout
%14 = call i32 @fflush(i32 %13)
%15 = load i32, i32* @stdin
%16 = call i32 @fgets(i8* %2, i32 512, i32 %15)
%17 = call i32 @cmd_parse(i32 %4, i32 %16)
br label %11
}