#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int fd;
  int status;
  bool stop = false;
  fd = open("/proc/self/cmdline", O_RDONLY);
  if (fd < 0) {
    perror("open()");
    exit(EXIT_FAILURE);
  }
  do {
    char buffer;
    status = read(fd, &buffer, 1);
    if (status < 0) {
      perror("read()");
      exit(EXIT_FAILURE);
    }
    stop = (status == 0);
    status = write(1, &buffer, 1);
    if (status < 0) {
      perror("write()");
      exit(EXIT_FAILURE);
    }
  } while (!stop);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a2 <main+0x52>
lea    0xe84(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    11a7 <main+0x57>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11df <main+0x8f>
lea    0xe4e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
mov    $0x1,%edx
lea    -0x11(%rbp),%rsi
mov    %edx,%edi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1225 <main+0xd5>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    122a <main+0xda>
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a7 <main+0x57>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    117a <main+0x2a>
lea    0xe9b(%rip),%rdi
jmp    11de <main+0x8e>
mov    %eax,%r14d
lea    0xf(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11ce <main+0x7e>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11d7 <main+0x87>
test   %ebp,%ebp
jne    1190 <main+0x40>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe45(%rip),%rdi
jmp    11de <main+0x8e>
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [8 x i8] c"write()\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %4
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
br label %15
15: ; preds = %36, %14
%16 = load i32, i32* %2
%17 = call i32 @read(i32 %16, i8* %5, i32 1)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %15
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
store i32 %27, i32* %4
%28 = call i32 @write(i32 1, i8* %5, i32 1)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %24
br label %36
36: ; preds = %35
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
%39 = xor i1 %38, true
br i1 %39, label %15, label %40
40: ; preds = %36
%41 = load i32, i32* @EXIT_SUCCESS
%42 = call i32 @exit(i32 %41)
unreachable
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/cmdline\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [7 x i8] c"read()\00"
@.str.3 = [8 x i8] c"write()\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i8
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0, %23
%10 = call i32 @read(i32 %3, i8* %1, i32 1)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %9
%17 = call i32 @write(i32 1, i8* %1, i32 1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
%24 = icmp eq i32 %10, 0
br i1 %24, label %25, label %9
25: ; preds = %23
%26 = load i32, i32* @EXIT_SUCCESS
%27 = tail call i32 @exit(i32 %26)
unreachable
}