#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 */
struct cmd {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ exec_pipeline(struct cmd *);
int wait_pipeline(struct cmd *);
int invoke_commands(struct cmd *cmdhead) {
  int st;
  int original_stdin = dup(0);
  int original_stdout = dup(1);
  exec_pipeline(cmdhead);
  st = wait_pipeline(cmdhead);
  close(0);
  dup2(original_stdin, 0);
  close(original_stdin);
  close(1);
  dup2(original_stdout, 1);
  close(original_stdout);
  return st;
}


x86 assembly (clang, -O0):
<invoke_commands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,-0x10(%rbp)
mov    $0x1,%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <exec_pipeline@plt>
mov    -0x8(%rbp),%rdi
call   1070 <wait_pipeline@plt>
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invoke_commands>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
call   1040 <dup@plt>
mov    %eax,%ebp
mov    $0x1,%edi
call   1040 <dup@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1060 <exec_pipeline@plt>
mov    %rbx,%rdi
call   1070 <wait_pipeline@plt>
mov    %eax,%ebx
xor    %edi,%edi
call   1050 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    %r14d,%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.cmd = type { i32 }
define i32 @invoke_commands(%struct.cmd* %0) {
%2 = alloca %struct.cmd*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store %struct.cmd* %0, %struct.cmd** %2
%6 = call i32 @dup(i32 0)
store i32 %6, i32* %4
%7 = call i32 @dup(i32 1)
store i32 %7, i32* %5
%8 = load %struct.cmd*, %struct.cmd** %2
%9 = call i32 @exec_pipeline(%struct.cmd* %8)
%10 = load %struct.cmd*, %struct.cmd** %2
%11 = call i32 @wait_pipeline(%struct.cmd* %10)
store i32 %11, i32* %3
%12 = call i32 @close(i32 0)
%13 = load i32, i32* %4
%14 = call i32 @dup2(i32 %13, i32 0)
%15 = load i32, i32* %4
%16 = call i32 @close(i32 %15)
%17 = call i32 @close(i32 1)
%18 = load i32, i32* %5
%19 = call i32 @dup2(i32 %18, i32 1)
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
%struct.cmd = type { i32 }
define i32 @invoke_commands(%struct.cmd* %0) {
%2 = tail call i32 @dup(i32 0)
%3 = tail call i32 @dup(i32 1)
%4 = tail call i32 @exec_pipeline(%struct.cmd* %0)
%5 = tail call i32 @wait_pipeline(%struct.cmd* %0)
%6 = tail call i32 @close(i32 0)
%7 = tail call i32 @dup2(i32 %2, i32 0)
%8 = tail call i32 @close(i32 %2)
%9 = tail call i32 @close(i32 1)
%10 = tail call i32 @dup2(i32 %3, i32 1)
%11 = tail call i32 @close(i32 %3)
ret i32 %5
}