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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ cctools_debug(long long, char *, char const *, ...);
int /*<<< orphan*/ close(int);
int dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char const *, int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int sort_uniq_namelist(const char *filename, int *fd) {
  int fds[2];
  pid_t pid;
  if (pipe(fds)) {
    cctools_debug((1LL << 3), "pipe fails: %s\n", strerror(errno));
    return -1;
  }
  pid = fork();
  if (pid == 0) {
    int input = open(filename, O_RDONLY);
    if (input == -1) {
      cctools_debug((1LL << 3),
                    "sort_uniq_namelist: open(`%s`) func fails: %s\n", filename,
                    strerror(errno));
      exit(EXIT_FAILURE);
    }
    if (dup2(input, STDIN_FILENO) == -1) {
      cctools_debug((1LL << 3), "sort_uniq_namelist: dup2 fails: %s\n",
                    strerror(errno));
      exit(EXIT_FAILURE);
    }
    close(input);
    close(fds[0]);
    if (dup2(fds[1], STDOUT_FILENO) == -1) {
      cctools_debug((1LL << 3), "sort_uniq_namelist: dup2 fails: %s\n",
                    strerror(errno));
      exit(EXIT_FAILURE);
    }
    close(fds[1]);
    if (execlp("sort", "sort", "-u", NULL) == -1) {
      cctools_debug((1LL << 3), "sort_uniq_namelist: execlp fails: %s\n",
                    strerror(errno));
      exit(EXIT_FAILURE);
    }
  } else if (pid > 0) {
    close(fds[1]);
    *fd = fds[0];
  } else {
    cctools_debug((1LL << 3), "fork fails: %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sort_uniq_namelist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11e3 <sort_uniq_namelist+0x53>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
mov    $0x8,%edi
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1060 <cctools_debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bc <sort_uniq_namelist+0x22c>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    135f <sort_uniq_namelist+0x1cf>
mov    -0x10(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1255 <sort_uniq_namelist+0xc5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
mov    $0x8,%edi
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1060 <cctools_debug@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d61(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12a1 <sort_uniq_namelist+0x111>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
mov    $0x8,%edi
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1060 <cctools_debug@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12fd <sort_uniq_namelist+0x16d>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
mov    $0x8,%edi
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1060 <cctools_debug@plt>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xd57(%rip),%rsi
lea    0xd55(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1090 <execlp@plt>
cmp    $0xffffffff,%eax
jne    135a <sort_uniq_namelist+0x1ca>
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
mov    $0x8,%edi
lea    0xd26(%rip),%rsi
mov    $0x0,%al
call   1060 <cctools_debug@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    13b5 <sort_uniq_namelist+0x225>
cmpq   $0x0,-0x28(%rbp)
jle    1380 <sort_uniq_namelist+0x1f0>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    13b0 <sort_uniq_namelist+0x220>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
mov    $0x8,%edi
lea    0xcf4(%rip),%rsi
mov    $0x0,%al
call   1060 <cctools_debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bc <sort_uniq_namelist+0x22c>
jmp    13b5 <sort_uniq_namelist+0x225>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_uniq_namelist>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %rax,%rax
je     11da <sort_uniq_namelist+0x4a>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
lea    0xe41(%rip),%rsi
mov    $0x8,%edi
xor    %eax,%eax
call   1060 <cctools_debug@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11fe <sort_uniq_namelist+0x6e>
jle    129f <sort_uniq_namelist+0x10f>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    (%rsp),%eax
mov    %eax,(%rbx)
xor    %ebx,%ebx
jmp    11d0 <sort_uniq_namelist+0x40>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12e2 <sort_uniq_namelist+0x152>
mov    %eax,%ebx
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     12bc <sort_uniq_namelist+0x12c>
mov    %ebx,%edi
call   1040 <close@plt>
mov    (%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     12bc <sort_uniq_namelist+0x12c>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
lea    0xdfa(%rip),%rdi
lea    0xdf8(%rip),%rdx
xor    %ebx,%ebx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1090 <execlp@plt>
cmp    $0xffffffff,%eax
jne    11d0 <sort_uniq_namelist+0x40>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
lea    0xdce(%rip),%rsi
jmp    12d4 <sort_uniq_namelist+0x144>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
lea    0xdda(%rip),%rsi
jmp    11bf <sort_uniq_namelist+0x2f>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
movslq %eax,%rdx
lea    0xd6b(%rip),%rsi
mov    $0x8,%edi
xor    %eax,%eax
call   1060 <cctools_debug@plt>
jmp    1308 <sort_uniq_namelist+0x178>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd19(%rip),%rsi
mov    $0x8,%edi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <cctools_debug@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"pipe fails: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [47 x i8] c"sort_uniq_namelist: open(`%s`) func fails: %s\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [36 x i8] c"sort_uniq_namelist: dup2 fails: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [3 x i8] c"-u\00"
@.str.5 = [38 x i8] c"sort_uniq_namelist: execlp fails: %s\0A\00"
@.str.6 = [16 x i8] c"fork fails: %s\0A\00"
define i32 @sort_uniq_namelist(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [2 x i32]
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = sext i32 %14 to i64
%16 = inttoptr i64 %15 to i8*
%17 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %100
18: ; preds = %2
%19 = call i64 (...) @fork()
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %82
22: ; preds = %18
%23 = load i8*, i8** %4
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %35
28: ; preds = %22
%29 = load i8*, i8** %4
%30 = load i32, i32* @errno
%31 = call i32 @strerror(i32 %30)
%32 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %31)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %22
%36 = load i32, i32* %8
%37 = load i32, i32* @STDIN_FILENO
%38 = call i32 @dup2(i32 %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %48
40: ; preds = %35
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i8*
%45 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %35
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%55 = load i32, i32* %54
%56 = load i32, i32* @STDOUT_FILENO
%57 = call i32 @dup2(i32 %55, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %67
59: ; preds = %48
%60 = load i32, i32* @errno
%61 = call i32 @strerror(i32 %60)
%62 = sext i32 %61 to i64
%63 = inttoptr i64 %62 to i8*
%64 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %63)
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %48
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%69 = load i32, i32* %68
%70 = call i32 @close(i32 %69)
%71 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* null)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %81
73: ; preds = %67
%74 = load i32, i32* @errno
%75 = call i32 @strerror(i32 %74)
%76 = sext i32 %75 to i64
%77 = inttoptr i64 %76 to i8*
%78 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %77)
%79 = load i32, i32* @EXIT_FAILURE
%80 = call i32 @exit(i32 %79)
unreachable
81: ; preds = %67
br label %99
82: ; preds = %18
%83 = load i64, i64* %7
%84 = icmp sgt i64 %83, 0
br i1 %84, label %85, label %92
85: ; preds = %82
%86 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%87 = load i32, i32* %86
%88 = call i32 @close(i32 %87)
%89 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%90 = load i32, i32* %89
%91 = load i32*, i32** %5
store i32 %90, i32* %91
br label %98
92: ; preds = %82
%93 = load i32, i32* @errno
%94 = call i32 @strerror(i32 %93)
%95 = sext i32 %94 to i64
%96 = inttoptr i64 %95 to i8*
%97 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %96)
store i32 -1, i32* %3
br label %100
98: ; preds = %85
br label %99
99: ; preds = %98, %81
store i32 0, i32* %3
br label %100
100: ; preds = %99, %92, %12
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@.str = [16 x i8] c"pipe fails: %s\0A\00"
@errno = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [47 x i8] c"sort_uniq_namelist: open(`%s`) func fails: %s\0A\00"
@EXIT_FAILURE = global i32 0
@STDIN_FILENO = global i32 0
@.str.2 = [36 x i8] c"sort_uniq_namelist: dup2 fails: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [5 x i8] c"sort\00"
@.str.4 = [3 x i8] c"-u\00"
@.str.5 = [38 x i8] c"sort_uniq_namelist: execlp fails: %s\0A\00"
@.str.6 = [16 x i8] c"fork fails: %s\0A\00"
define i32 @sort_uniq_namelist(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = sext i32 %10 to i64
%12 = inttoptr i64 %11 to i8*
%13 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %82
14: ; preds = %2
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %69
17: ; preds = %14
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %0, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32, i32* @errno
%23 = call i32 @strerror(i32 %22)
%24 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %17
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @dup2(i32 %19, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = sext i32 %33 to i64
%35 = inttoptr i64 %34 to i8*
%36 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %27
%40 = call i32 @close(i32 %19)
%41 = load i32, i32* %5
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @STDOUT_FILENO
%46 = call i32 @dup2(i32 %44, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %56
48: ; preds = %39
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = sext i32 %50 to i64
%52 = inttoptr i64 %51 to i8*
%53 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %39
%57 = load i32, i32* %43
%58 = call i32 @close(i32 %57)
%59 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* null)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %82
61: ; preds = %56
%62 = load i32, i32* @errno
%63 = call i32 @strerror(i32 %62)
%64 = sext i32 %63 to i64
%65 = inttoptr i64 %64 to i8*
%66 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %65)
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %14
%70 = icmp sgt i64 %15, 0
br i1 %70, label %71, label %76
71: ; preds = %69
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %5
store i32 %75, i32* %1
br label %82
76: ; preds = %69
%77 = load i32, i32* @errno
%78 = call i32 @strerror(i32 %77)
%79 = sext i32 %78 to i64
%80 = inttoptr i64 %79 to i8*
%81 = call i32 (i64, i8*, i8*, ...) @cctools_debug(i64 8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %71, %56, %76, %8
%83 = phi i32 [ -1, %8 ], [ -1, %76 ], [ 0, %56 ], [ 0, %71 ]
ret i32 %83
}