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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  pid_t my_id;
  char buffer[4096];
  my_id = getpid();
  fprintf(stderr, "pid: %d -- I am my-cat and I have started\n", my_id);
  memset(buffer, 0, sizeof(buffer));
  while (read(0, buffer, sizeof(buffer)) > 0) {
    fprintf(stderr, "pid: %d read some data\n", getpid());
    buffer[4095] = 0;
    write(1, buffer, strlen(buffer));
    fprintf(stderr, "pid: %d wrote some data\n", getpid());
    memset(buffer, 0, sizeof(buffer));
  }
  fprintf(stderr, "pid: %d -- I am my-cat and I am exiting\n", my_id);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe5a(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
lea    -0x1020(%rbp),%rsi
xor    %edi,%edi
mov    $0x1000,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1278 <main+0x108>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1034(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1034(%rbp),%edi
mov    %eax,%edx
lea    0xe2b(%rip),%rsi
call   1080 <fprintf@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1030(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1030(%rbp),%rsi
mov    %eax,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1024(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1024(%rbp),%edi
mov    %eax,%edx
lea    0xde8(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x1020(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
jmp    11be <main+0x4e>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdd1(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%r14d
mov    0x2e4e(%rip),%r13
mov    0x0(%r13),%edi
lea    0xe63(%rip),%rsi
mov    %eax,%edx
call   1080 <fprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1250 <main+0xe0>
lea    0xe56(%rip),%r15
mov    %rsp,%rbp
lea    0xe64(%rip),%r12
nop
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1080 <fprintf@plt>
movb   $0x0,0xfff(%rsp)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1080 <fprintf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     11e0 <main+0x70>
mov    0x0(%r13),%edi
lea    0xe01(%rip),%rsi
mov    %r14d,%edx
call   1080 <fprintf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"pid: %d -- I am my-cat and I have started\0A\00"
@.str.1 = [24 x i8] c"pid: %d read some data\0A\00"
@.str.2 = [25 x i8] c"pid: %d wrote some data\0A\00"
@.str.3 = [41 x i8] c"pid: %d -- I am my-cat and I am exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4096 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 (...) @getpid()
store i32 %8, i32* %6
%9 = load i32, i32* @stderr
%10 = load i32, i32* %6
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i32 0, i32 4096)
br label %14
14: ; preds = %18, %2
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%16 = call i64 @read(i32 0, i8* %15, i32 4096)
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 (...) @getpid()
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 4095
store i8 0, i8* %22
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 1, i8* %23, i32 %25)
%27 = load i32, i32* @stderr
%28 = call i32 (...) @getpid()
%29 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 4096)
br label %14
32: ; preds = %14
%33 = load i32, i32* @stderr
%34 = load i32, i32* %6
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %34)
%36 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"pid: %d -- I am my-cat and I have started\0A\00"
@.str.1 = [24 x i8] c"pid: %d read some data\0A\00"
@.str.2 = [25 x i8] c"pid: %d wrote some data\0A\00"
@.str.3 = [41 x i8] c"pid: %d -- I am my-cat and I am exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %5)
%8 = call i32 @memset(i8* %4, i32 0, i32 4096)
%9 = call i64 @read(i32 0, i8* %4, i32 4096)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %2
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 4095
br label %13
13: ; preds = %11, %13
%14 = load i32, i32* @stderr
%15 = call i32 (...) @getpid()
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i8 0, i8* %12
%17 = call i32 @strlen(i8* %4)
%18 = call i32 @write(i32 1, i8* %4, i32 %17)
%19 = load i32, i32* @stderr
%20 = call i32 (...) @getpid()
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = call i32 @memset(i8* %4, i32 0, i32 4096)
%23 = call i64 @read(i32 0, i8* %4, i32 4096)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %13, label %25
25: ; preds = %13, %2
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%28 = call i32 @exit(i32 0)
unreachable
}