#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*/ AF_UNIX;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int socketpair(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int *);
char toupper(char);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int sv[2];
  char buf;
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) {
    perror("socketpair");
    exit(EXIT_FAILURE);
  }
  if (!fork()) {
    read(sv[1], &buf, 1);
    printf("child: read '%c'\n", buf);
    buf = toupper(buf);
    write(sv[1], &buf, 1);
    printf("child: sent '%c'\n", buf);
  } else {
    write(sv[0], "b", 1);
    printf("parent: sent 'b'\n");
    read(sv[0], &buf, 1);
    printf("parent: read '%c'\n", buf);
    wait(NULL);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0xffffffff,%eax
jne    11df <main+0x4f>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%eax
jne    1246 <main+0xb6>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0xd(%rbp),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movsbl -0xd(%rbp),%edi
call   1030 <toupper@plt>
mov    %al,-0xd(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
movsbl -0xd(%rbp),%esi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1294 <main+0x104>
mov    -0xc(%rbp),%edi
lea    0xddf(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0xd(%rbp),%esi
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
cmp    $0xffffffff,%eax
je     128f <main+0xef>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     122a <main+0x8a>
mov    0x8(%rsp),%edi
lea    0xe4a(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
lea    0xe4e(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0x7(%rsp),%esi
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    1284 <main+0xe4>
mov    0xc(%rsp),%edi
lea    0x7(%rsp),%r14
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0x7(%rsp),%ebp
lea    0xdbf(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1030 <toupper@plt>
mov    %eax,%ebx
mov    %al,0x7(%rsp)
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
movsbl %bl,%esi
lea    0xda0(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"child: read '%c'\0A\00"
@.str.2 = [18 x i8] c"child: sent '%c'\0A\00"
@.str.3 = [2 x i8] c"b\00"
@.str.4 = [18 x i8] c"parent: sent 'b'\0A\00"
@.str.5 = [19 x i8] c"parent: read '%c'\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i8
store i32 0, i32* %1
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
%15 = icmp ne i32 %14, 0
br i1 %15, label %31, label %16
16: ; preds = %13
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @read(i32 %18, i8* %3, i32 1)
%20 = load i8, i8* %3
%21 = sext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i8, i8* %3
%24 = call signext i8 @toupper(i8 signext %23)
store i8 %24, i8* %3
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @write(i32 %26, i8* %3, i32 1)
%28 = load i8, i8* %3
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %29)
br label %43
31: ; preds = %13
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%33 = load i32, i32* %32
%34 = call i32 @write(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @read(i32 %37, i8* %3, i32 1)
%39 = load i8, i8* %3
%40 = sext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = call i32 @wait(i32* null)
br label %43
43: ; preds = %31, %16
ret i32 0
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [18 x i8] c"child: read '%c'\0A\00"
@.str.2 = [18 x i8] c"child: sent '%c'\0A\00"
@.str.3 = [2 x i8] c"b\00"
@.str.5 = [19 x i8] c"parent: read '%c'\0A\00"
@str = [17 x i8] c"parent: sent 'b'\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = bitcast [2 x i32]* %1 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i32 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %28
16: ; preds = %13
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @read(i32 %18, i8* %2, i32 1)
%20 = load i8, i8* %2
%21 = sext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call signext i8 @toupper(i8 signext %20)
store i8 %23, i8* %2
%24 = load i32, i32* %17
%25 = call i32 @write(i32 %24, i8* %2, i32 1)
%26 = sext i8 %23 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %38
28: ; preds = %13
%29 = load i32, i32* %6
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = call i32 @read(i32 %32, i8* %2, i32 1)
%34 = load i8, i8* %2
%35 = sext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = call i32 @wait(i32* null)
br label %38
38: ; preds = %28, %16
ret i32 0
}