#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int dup2(int, int);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ stderr;
int redirect_fd(const char *cmd, int newd) {
  FILE *stream;
  int fd;
  int res;
  stream = popen(cmd, "w");
  if (NULL == stream) {
    fprintf(stderr, "cannot start %s\n", cmd);
    return -1;
  }
  fd = fileno(stream);
  if (0 >= fd) {
    fprintf(stderr, "cannot get fd of %s\n", cmd);
    return -1;
  }
  res = dup2(fd, newd);
  if (-1 == res) {
    fprintf(stderr, "cannot replace fd %d\n", newd);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<redirect_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1196 <redirect_fd+0x56>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe7f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <redirect_fd+0xdd>
mov    -0x20(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jl     11d4 <redirect_fd+0x94>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <redirect_fd+0xdd>
mov    -0x24(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <dup2@plt>
mov    %eax,-0x28(%rbp)
mov    $0xffffffff,%eax
cmp    -0x28(%rbp),%eax
jne    1216 <redirect_fd+0xd6>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <redirect_fd+0xdd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<redirect_fd>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xeb1(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     1192 <redirect_fd+0x52>
mov    %rax,%rdi
call   1050 <fileno@plt>
test   %eax,%eax
jle    11a4 <redirect_fd+0x64>
mov    %eax,%edi
mov    %ebp,%esi
call   1030 <dup2@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11c3 <redirect_fd+0x83>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xea1(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11be <redirect_fd+0x7e>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
jmp    11b4 <redirect_fd+0x74>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"cannot start %s\0A\00"
@.str.2 = [21 x i8] c"cannot get fd of %s\0A\00"
@.str.3 = [22 x i8] c"cannot replace fd %d\0A\00"
define i32 @redirect_fd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* null, %11
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %37
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = call i32 @fileno(i32* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sge i32 0, %20
br i1 %21, label %22, label %26
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %4
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 -1, i32* %3
br label %37
26: ; preds = %17
%27 = load i32, i32* %7
%28 = load i32, i32* %5
%29 = call i32 @dup2(i32 %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 -1, %30
br i1 %31, label %32, label %36
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i32, i32* %5
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %34)
store i32 -1, i32* %3
br label %37
36: ; preds = %26
store i32 0, i32* %3
br label %37
37: ; preds = %36, %32, %22, %13
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"cannot start %s\0A\00"
@.str.2 = [21 x i8] c"cannot get fd of %s\0A\00"
@.str.3 = [22 x i8] c"cannot replace fd %d\0A\00"
define i32 @redirect_fd(i8* %0, i32 %1) {
%3 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
8: ; preds = %2
%9 = tail call i32 @fileno(i32* %3)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %20
14: ; preds = %8
%15 = tail call i32 @dup2(i32 %9, i32 %1)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %1)
br label %20
20: ; preds = %14, %17, %11, %5
%21 = phi i32 [ -1, %5 ], [ -1, %11 ], [ -1, %17 ], [ 0, %14 ]
ret i32 %21
}