#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 */
struct copy_buffs {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free_resources();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ reader(struct copy_buffs *, char *);
scalar_t__ setup_semid();
struct copy_buffs *setup_shared_mem();
int wait(int *);
int /*<<< orphan*/ writer(struct copy_buffs *, char *);
int main(int argc, char *argv[]) {
  struct copy_buffs *shared_stuff;
  int i, pid_reader, pid_writer, pid, status;
  if (argc < 3) {
    printf("usage: %s infile outfile\n");
    exit(0);
  }
  shared_stuff = setup_shared_mem();
  if (setup_semid()) {
    free_resources();
  }
  pid_reader = fork();
  if (pid_reader == 0) {
    reader(shared_stuff, argv[1]);
    exit(0);
  }
  if (pid_reader < 0) {
    free_resources();
    perror("reader fork error");
    exit(-1);
  }
  printf("Created reader task %d\n", pid_reader);
  pid_writer = fork();
  if (pid_writer == 0) {
    writer(shared_stuff, argv[2]);
    exit(0);
  }
  if (pid_writer < 0) {
    free_resources();
    perror("reader fork error");
    exit(-1);
  }
  printf("Created writer task %d\n", pid_writer);
  for (i = 0; i < 2; i++) {
    pid = wait(&status);
    if (pid == pid_reader)
      printf("reader terminated\n");
    else if (pid == pid_writer)
      printf("writer terminated\n");
    else
      printf(" Some pid terminated %d\n");
  }
  printf("Everything done\n");
  free_resources();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d5 <main+0x35>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1060 <setup_shared_mem@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <setup_semid@plt>
cmp    $0x0,%rax
je     11f8 <main+0x58>
mov    $0x0,%al
call   1050 <free_resources@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1224 <main+0x84>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <reader@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jge    124b <main+0xab>
mov    $0x0,%al
call   1050 <free_resources@plt>
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1288 <main+0xe8>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1070 <writer@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jge    12af <main+0x10f>
mov    $0x0,%al
call   1050 <free_resources@plt>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%esi
lea    0xd8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
jge    1341 <main+0x1a1>
lea    -0x2c(%rbp),%rdi
call   10b0 <wait@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jne    12fc <main+0x15c>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132e <main+0x18e>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    131b <main+0x17b>
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1329 <main+0x189>
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132e <main+0x18e>
jmp    1333 <main+0x193>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12c7 <main+0x127>
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <free_resources@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jle    12de <main+0x12e>
mov    %rsi,%r14
xor    %eax,%eax
call   1070 <setup_shared_mem@plt>
mov    %rax,%r15
xor    %eax,%eax
call   10a0 <setup_semid@plt>
test   %rax,%rax
je     11e0 <main+0x30>
xor    %eax,%eax
call   1060 <free_resources@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     12f3 <main+0x143>
js     12c1 <main+0x111>
mov    %eax,%ebx
lea    0xe2e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     1306 <main+0x156>
js     12c1 <main+0x111>
mov    %eax,%ebp
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
cmp    %ebx,%eax
jne    124a <main+0x9a>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
jmp    126a <main+0xba>
cmp    %ebp,%eax
jne    125c <main+0xac>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
jmp    126a <main+0xba>
lea    0xdf9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x4(%rsp),%rdi
call   10c0 <wait@plt>
cmp    %ebx,%eax
jne    1281 <main+0xd1>
lea    0xe18(%rip),%rdi
jmp    128c <main+0xdc>
cmp    %ebp,%eax
jne    1293 <main+0xe3>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
jmp    12a1 <main+0xf1>
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdcd(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1060 <free_resources@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1060 <free_resources@plt>
lea    0xd4b(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd1b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
call   1040 <reader@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x10(%r14),%rsi
mov    %r15,%rdi
call   1080 <writer@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.copy_buffs = type { i32 }
@.str = [26 x i8] c"usage: %s infile outfile\0A\00"
@.str.1 = [18 x i8] c"reader fork error\00"
@.str.2 = [24 x i8] c"Created reader task %d\0A\00"
@.str.3 = [24 x i8] c"Created writer task %d\0A\00"
@.str.4 = [19 x i8] c"reader terminated\0A\00"
@.str.5 = [19 x i8] c"writer terminated\0A\00"
@.str.6 = [25 x i8] c" Some pid terminated %d\0A\00"
@.str.7 = [17 x i8] c"Everything done\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.copy_buffs*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = call %struct.copy_buffs* (...) @setup_shared_mem()
store %struct.copy_buffs* %18, %struct.copy_buffs** %6
%19 = call i64 (...) @setup_semid()
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (...) @free_resources()
br label %23
23: ; preds = %21, %17
%24 = call i32 (...) @fork()
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %23
%28 = load %struct.copy_buffs*, %struct.copy_buffs** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @reader(%struct.copy_buffs* %28, i8* %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %23
%35 = load i32, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = call i32 (...) @free_resources()
%39 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %34
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = call i32 (...) @fork()
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %41
%48 = load %struct.copy_buffs*, %struct.copy_buffs** %6
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = call i32 @writer(%struct.copy_buffs* %48, i8* %51)
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %41
%55 = load i32, i32* %9
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = call i32 (...) @free_resources()
%59 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%60 = call i32 @exit(i32 -1)
unreachable
61: ; preds = %54
%62 = load i32, i32* %9
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %62)
store i32 0, i32* %7
br label %64
64: ; preds = %84, %61
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 2
br i1 %66, label %67, label %87
67: ; preds = %64
%68 = call i32 @wait(i32* %11)
store i32 %68, i32* %10
%69 = load i32, i32* %10
%70 = load i32, i32* %8
%71 = icmp eq i32 %69, %70
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %83
74: ; preds = %67
%75 = load i32, i32* %10
%76 = load i32, i32* %9
%77 = icmp eq i32 %75, %76
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %82
80: ; preds = %74
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %82
82: ; preds = %80, %78
br label %83
83: ; preds = %82, %72
br label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
br label %64
87: ; preds = %64
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0))
%89 = call i32 (...) @free_resources()
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.copy_buffs = type { i32 }
@.str = [26 x i8] c"usage: %s infile outfile\0A\00"
@.str.1 = [18 x i8] c"reader fork error\00"
@.str.2 = [24 x i8] c"Created reader task %d\0A\00"
@.str.3 = [24 x i8] c"Created writer task %d\0A\00"
@.str.6 = [25 x i8] c" Some pid terminated %d\0A\00"
@str = [16 x i8] c"Everything done\00"
@str.8 = [18 x i8] c"writer terminated\00"
@str.9 = [18 x i8] c"reader terminated\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = tail call %struct.copy_buffs* (...) @setup_shared_mem()
%11 = tail call i64 (...) @setup_semid()
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 (...) @free_resources()
br label %15
15: ; preds = %13, %9
%16 = tail call i32 (...) @fork()
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @reader(%struct.copy_buffs* %10, i8* %20)
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %15
%24 = icmp slt i32 %16, 0
br i1 %24, label %25, label %29
25: ; preds = %23
%26 = tail call i32 (...) @free_resources()
%27 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -1)
unreachable
29: ; preds = %23
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%31 = tail call i32 (...) @fork()
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = getelementptr inbounds i8*, i8** %1, i64 2
%35 = load i8*, i8** %34
%36 = tail call i32 @writer(%struct.copy_buffs* %10, i8* %35)
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %29
%39 = icmp slt i32 %31, 0
br i1 %39, label %40, label %44
40: ; preds = %38
%41 = tail call i32 (...) @free_resources()
%42 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%43 = tail call i32 @exit(i32 -1)
unreachable
44: ; preds = %38
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%46 = call i32 @wait(i32* %3)
%47 = icmp eq i32 %46, %16
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
br label %56
50: ; preds = %44
%51 = icmp eq i32 %46, %31
br i1 %51, label %52, label %54
52: ; preds = %50
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %56
54: ; preds = %50
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %48, %54, %52
%57 = call i32 @wait(i32* %3)
%58 = icmp eq i32 %57, %16
br i1 %58, label %65, label %59
59: ; preds = %56
%60 = icmp eq i32 %57, %31
br i1 %60, label %63, label %61
61: ; preds = %59
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
br label %67
63: ; preds = %59
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %67
65: ; preds = %56
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
br label %67
67: ; preds = %65, %63, %61
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%69 = call i32 (...) @free_resources()
ret i32 0
}