#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 */
scalar_t__ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ stderr;
int main() {
  int fd[2];
  if (pipe(fd) != 0) {
    fprintf(stderr, "pipe failed\n");
    return 1;
  }
  printf("Pipe returned file descriptors: %d, %d\n", fd[0], fd[1]);
  if (close(fd[0]) != 0) {
    fprintf(stderr, "File descriptor %d is invalid\n", fd[0]);
    return 1;
  }
  if (close(fd[1]) != 0) {
    fprintf(stderr, "File descriptor %d is invalid\n", fd[1]);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     1185 <main+0x45>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xe8e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120e <main+0xce>
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     11cf <main+0x8f>
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120e <main+0xce>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1207 <main+0xc7>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    120e <main+0xce>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
je     1167 <main+0x27>
mov    0x2e8b(%rip),%rax
mov    (%rax),%edi
lea    0xea2(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    11be <main+0x7e>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe98(%rip),%rdi
call   1030 <printf@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
je     1195 <main+0x55>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%edx
jmp    11b0 <main+0x70>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
test   %rax,%rax
je     11c5 <main+0x85>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x4(%rsp),%edx
lea    0xe7e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"pipe failed\0A\00"
@.str.1 = [40 x i8] c"Pipe returned file descriptors: %d, %d\0A\00"
@.str.2 = [31 x i8] c"File descriptor %d is invalid\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
store i32 0, i32* %1
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%4 = call i64 @pipe(i32* %3)
%5 = icmp ne i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %35
9: ; preds = %0
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = load i32, i32* %10
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%16 = load i32, i32* %15
%17 = call i64 @close(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %9
%20 = load i32, i32* @stderr
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%22 = load i32, i32* %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 1, i32* %1
br label %35
24: ; preds = %9
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i64 @close(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %32)
store i32 1, i32* %1
br label %35
34: ; preds = %24
store i32 0, i32* %1
br label %35
35: ; preds = %34, %29, %19, %6
%36 = load i32, i32* %1
ret i32 %36
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"pipe failed\0A\00"
@.str.1 = [40 x i8] c"Pipe returned file descriptors: %d, %d\0A\00"
@.str.2 = [31 x i8] c"File descriptor %d is invalid\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%4 = call i64 @pipe(i32* %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %29
9: ; preds = %0
%10 = load i32, i32* %3
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %12)
%14 = load i32, i32* %3
%15 = call i64 @close(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %9
%18 = load i32, i32* @stderr
%19 = load i32, i32* %3
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %29
21: ; preds = %9
%22 = load i32, i32* %11
%23 = call i64 @close(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = load i32, i32* %11
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %21, %25, %17, %6
%30 = phi i32 [ 1, %6 ], [ 1, %17 ], [ 1, %25 ], [ 0, %21 ]
ret i32 %30
}