#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 */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ TIOCSCTTY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ fatal(char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ setsid();
void exec_cmd(int fd, char **argv) {
  setsid();
  dup2(fd, STDIN_FILENO);
  dup2(fd, STDOUT_FILENO);
  dup2(fd, STDERR_FILENO);
  if (ioctl(fd, TIOCSCTTY, NULL)) {
    fatal("failed to set the controlling terminal for the child process");
  }
  close(fd);
  execvp(argv[0], argv);
  fatal("execvp returned");
}


x86 assembly (clang, -O0):
<exec_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <setsid@plt>
mov    -0x4(%rbp),%edi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
je     11d4 <exec_cmd+0x74>
lea    0xe31(%rip),%rdi
call   1080 <fatal@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <execvp@plt>
lea    0xe4a(%rip),%rdi
call   1080 <fatal@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %eax,%eax
call   1060 <setsid@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <ioctl@plt>
test   %rax,%rax
je     11c2 <exec_cmd+0x62>
lea    0xe43(%rip),%rdi
call   1080 <fatal@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
lea    0xe62(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1080 <fatal@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@TIOCSCTTY = global i32 0
@.str = [61 x i8] c"failed to set the controlling terminal for the child process\00"
@.str.1 = [16 x i8] c"execvp returned\00"
define void @exec_cmd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
store i32 %0, i32* %3
store i8** %1, i8*** %4
%5 = call i32 (...) @setsid()
%6 = load i32, i32* %3
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @dup2(i32 %6, i32 %7)
%9 = load i32, i32* %3
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @dup2(i32 %9, i32 %10)
%12 = load i32, i32* %3
%13 = load i32, i32* @STDERR_FILENO
%14 = call i32 @dup2(i32 %12, i32 %13)
%15 = load i32, i32* %3
%16 = load i32, i32* @TIOCSCTTY
%17 = call i64 @ioctl(i32 %15, i32 %16, i32* null)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @fatal(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = load i32, i32* %3
%23 = call i32 @close(i32 %22)
%24 = load i8**, i8*** %4
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = load i8**, i8*** %4
%28 = call i32 @execvp(i8* %26, i8** %27)
%29 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@TIOCSCTTY = global i32 0
@.str = [61 x i8] c"failed to set the controlling terminal for the child process\00"
@.str.1 = [16 x i8] c"execvp returned\00"
define void @exec_cmd(i32 %0, i8** %1) {
%3 = tail call i32 (...) @setsid()
%4 = load i32, i32* @STDIN_FILENO
%5 = tail call i32 @dup2(i32 %0, i32 %4)
%6 = load i32, i32* @STDOUT_FILENO
%7 = tail call i32 @dup2(i32 %0, i32 %6)
%8 = load i32, i32* @STDERR_FILENO
%9 = tail call i32 @dup2(i32 %0, i32 %8)
%10 = load i32, i32* @TIOCSCTTY
%11 = tail call i64 @ioctl(i32 %0, i32 %10, i32* null)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = tail call i32 @fatal(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = tail call i32 @close(i32 %0)
%17 = load i8*, i8** %1
%18 = tail call i32 @execvp(i8* %17, i8** %1)
%19 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
ret void
}