#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_RDWR;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exec_elf(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
int /*<<< orphan*/ fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage : %s /dev/ttyX\n", argv[0]);
  }
  int cttyfd = open(argv[1], O_RDWR);
  if (cttyfd >= 0) {
    fclose(stdin);
    fclose(stdout);
    fclose(stderr);
    stdin = fopen(argv[1], "r");
    stdout = fopen(argv[1], "w");
    stderr = fopen(argv[1], "w");
    cttyfd = open(argv[1], O_RDWR);
    ioctl(cttyfd, TIOCSCTTY, argv[1]);
    close(cttyfd);
    exec_elf("/tacos/bin/mishell");
  } else {
    fprintf(stderr, "Erreur à l'ouverture de %s.\n", argv[1]);
  }
  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   $0x2,-0x8(%rbp)
je     11ac <main+0x3c>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     12a1 <main+0x131>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0e(%rip),%rsi
call   1090 <fopen@plt>
mov    %eax,%ecx
mov    0x2dca(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdf1(%rip),%rsi
call   1090 <fopen@plt>
mov    %eax,%ecx
mov    0x2d93(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdd2(%rip),%rsi
call   1090 <fopen@plt>
mov    %eax,%ecx
mov    0x2d7c(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
call   1050 <ioctl@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xd83(%rip),%rdi
call   1030 <exec_elf@plt>
jmp    12be <main+0x14e>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd74(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
je     119b <main+0x2b>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe6a(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x8(%rbx),%rdi
mov    0x2e42(%rip),%r15
mov    (%r15),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1256 <main+0xe6>
mov    0x2e23(%rip),%rbp
mov    0x0(%rbp),%edi
call   1040 <fclose@plt>
mov    0x2dfc(%rip),%r13
mov    0x0(%r13),%edi
call   1040 <fclose@plt>
mov    0x2df4(%rip),%r12
mov    (%r12),%edi
call   1040 <fclose@plt>
mov    0x8(%rbx),%rdi
lea    0xe26(%rip),%rsi
call   1090 <fopen@plt>
mov    %eax,0x0(%rbp)
mov    0x8(%rbx),%rdi
lea    0xe15(%rip),%r14
mov    %r14,%rsi
call   1090 <fopen@plt>
mov    %eax,0x0(%r13)
mov    0x8(%rbx),%rdi
mov    %r14,%rsi
call   1090 <fopen@plt>
mov    %eax,(%r12)
mov    0x8(%rbx),%rdi
mov    (%r15),%esi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rbx),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xdcb(%rip),%rdi
call   1030 <exec_elf@plt>
jmp    126f <main+0xff>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xdc3(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage : %s /dev/ttyX\0A\00"
@O_RDWR = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@TIOCSCTTY = global i32 0
@.str.3 = [19 x i8] c"/tacos/bin/mishell\00"
@.str.4 = [30 x i8] c"Erreur \C3\A0 l'ouverture de %s.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %15
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %9, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %56
23: ; preds = %15
%24 = load i32, i32* @stdin
%25 = call i32 @fclose(i32 %24)
%26 = load i32, i32* @stdout
%27 = call i32 @fclose(i32 %26)
%28 = load i32, i32* @stderr
%29 = call i32 @fclose(i32 %28)
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %33, i32* @stdin
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @fopen(i8* %36, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %37, i32* @stdout
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = call i32 @fopen(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %41, i32* @stderr
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = load i32, i32* @O_RDWR
%46 = call i32 @open(i8* %44, i32 %45)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = load i32, i32* @TIOCSCTTY
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call i32 @ioctl(i32 %47, i32 %48, i8* %51)
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
%55 = call i32 @exec_elf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %62
56: ; preds = %15
%57 = load i32, i32* @stderr
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 1
%60 = load i8*, i8** %59
%61 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %56, %23
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage : %s /dev/ttyX\0A\00"
@O_RDWR = global i32 0
@stdin = global i32 0
@stdout = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@TIOCSCTTY = global i32 0
@.str.3 = [19 x i8] c"/tacos/bin/mishell\00"
@.str.4 = [30 x i8] c"Erreur \C3\A0 l'ouverture de %s.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %4, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %35
14: ; preds = %8
%15 = load i32, i32* @stdin
%16 = tail call i32 @fclose(i32 %15)
%17 = load i32, i32* @stdout
%18 = tail call i32 @fclose(i32 %17)
%19 = load i32, i32* @stderr
%20 = tail call i32 @fclose(i32 %19)
%21 = load i8*, i8** %9
%22 = tail call i32 @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %22, i32* @stdin
%23 = load i8*, i8** %9
%24 = tail call i32 @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %24, i32* @stdout
%25 = load i8*, i8** %9
%26 = tail call i32 @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %26, i32* @stderr
%27 = load i8*, i8** %9
%28 = load i32, i32* @O_RDWR
%29 = tail call i32 @open(i8* %27, i32 %28)
%30 = load i32, i32* @TIOCSCTTY
%31 = load i8*, i8** %9
%32 = tail call i32 @ioctl(i32 %29, i32 %30, i8* %31)
%33 = tail call i32 @close(i32 %29)
%34 = tail call i32 @exec_elf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %39
35: ; preds = %8
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %9
%38 = tail call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %35, %14
ret i32 0
}