#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;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
int pipe_close(int fd, pid_t pid) {
  int i, status;
  pid_t p;
  int exit_code;
  i = close(fd);
  if (i == -1)
    err(1, "close(in pipe_close) failed");
  p = waitpid(pid, &status, 0);
  if (p == -1)
    err(1, "waitpid(in pipe_close) failed");
  if (!WIFEXITED(status))
    errx(1, "child process terminated abnormally (in pipe_close), status=%08x",
         status);
  exit_code = WEXITSTATUS(status);
  if (exit_code != 0)
    errx(1, "child process terminated with error code %d (in pipe_close)",
         exit_code);
  return 0;
}


x86 assembly (clang, -O0):
<pipe_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1194 <pipe_close+0x34>
mov    $0x1,%edi
lea    0xe71(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c0 <pipe_close+0x60>
mov    $0x1,%edi
lea    0xe61(%rip),%rsi
call   1050 <err@plt>
mov    -0x10(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    11e5 <pipe_close+0x85>
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
lea    0xe5a(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     120e <pipe_close+0xae>
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
lea    0xe72(%rip),%rsi
call   1030 <errx@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_close>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
call   1040 <close@plt>
cmp    $0xffffffff,%eax
jne    1182 <pipe_close+0x22>
lea    0xe88(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    11a6 <pipe_close+0x46>
lea    0xe80(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %eax,%eax
jne    11c8 <pipe_close+0x68>
mov    0xc(%rsp),%edx
lea    0xe7c(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     11e8 <pipe_close+0x88>
lea    0xe9f(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1030 <errx@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [28 x i8] c"close(in pipe_close) failed\00"
@.str.1 = [30 x i8] c"waitpid(in pipe_close) failed\00"
@.str.2 = [65 x i8] c"child process terminated abnormally (in pipe_close), status=%08x\00"
@.str.3 = [60 x i8] c"child process terminated with error code %d (in pipe_close)\00"
define i32 @pipe_close(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = call i32 @close(i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* %4
%17 = call i32 @waitpid(i32 %16, i32* %6, i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i32, i32* %6
%24 = call i32 @WIFEXITED(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = load i32, i32* %6
%28 = call i32 @errx(i32 1, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %26, %22
%30 = load i32, i32* %6
%31 = call i32 @WEXITSTATUS(i32 %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* %8
%36 = call i32 @errx(i32 1, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %29
ret i32 0
}

LLVM IR (-O2):
@.str = [28 x i8] c"close(in pipe_close) failed\00"
@.str.1 = [30 x i8] c"waitpid(in pipe_close) failed\00"
@.str.2 = [65 x i8] c"child process terminated abnormally (in pipe_close), status=%08x\00"
@.str.3 = [60 x i8] c"child process terminated with error code %d (in pipe_close)\00"
define i32 @pipe_close(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @close(i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = call i32 @waitpid(i32 %1, i32* %3, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @err(i32 1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = load i32, i32* %3
%16 = call i32 @WIFEXITED(i32 %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %3
%20 = call i32 @errx(i32 1, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %14
%22 = load i32, i32* %3
%23 = call i32 @WEXITSTATUS(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i32 @errx(i32 1, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %27
27: ; preds = %25, %21
ret i32 0
}