#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ header(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ perror(char *);
do_ls(char *dir, int fd) {
  FILE *fp;
  fp = fdopen(fd, "w");
  header(fp, "text/plain");
  fprintf(fp, "\r\n");
  fflush(fp);
  dup2(fd, 1);
  dup2(fd, 2);
  fclose(fp);
  close(fd);
  execlp("ls", "ls", "-1", dir, NULL);
  perror(dir);
  exit(1);
}


x86 assembly (clang, -O0):
<do_ls>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    0xe47(%rip),%rsi
call   1090 <fdopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1030 <header@plt>
mov    -0x18(%rbp),%rdi
lea    0xe30(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1080 <fflush@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%rcx
lea    0xdef(%rip),%rsi
lea    0xdeb(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10c0 <execlp@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<do_ls>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe50(%rip),%rsi
mov    %ebp,%edi
call   1080 <fdopen@plt>
mov    %rax,%rbx
lea    0xe41(%rip),%rsi
mov    %rax,%rdi
call   1030 <header@plt>
lea    0xe3d(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xdf8(%rip),%rdi
lea    0xdf4(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   10c0 <execlp@plt>
mov    %r14,%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [11 x i8] c"text/plain\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [3 x i8] c"-1\00"
define i32 @do_ls(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = call i32* @fdopen(i32 %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = call i32 @header(i32* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32*, i32** %5
%11 = call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = load i32*, i32** %5
%13 = call i32 @fflush(i32* %12)
%14 = load i32, i32* %4
%15 = call i32 @dup2(i32 %14, i32 1)
%16 = load i32, i32* %4
%17 = call i32 @dup2(i32 %16, i32 2)
%18 = load i32*, i32** %5
%19 = call i32 @fclose(i32* %18)
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = load i8*, i8** %3
%23 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %22, i32* null)
%24 = load i8*, i8** %3
%25 = call i32 @perror(i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [11 x i8] c"text/plain\00"
@.str.2 = [3 x i8] c"\0D\0A\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [3 x i8] c"-1\00"
define i32 @do_ls(i8* %0, i32 %1) {
%3 = tail call i32* @fdopen(i32 %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @header(i32* %3, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 2, i64 1, i32* %3)
%6 = tail call i32 @fflush(i32* %3)
%7 = tail call i32 @dup2(i32 %1, i32 1)
%8 = tail call i32 @dup2(i32 %1, i32 2)
%9 = tail call i32 @fclose(i32* %3)
%10 = tail call i32 @close(i32 %1)
%11 = tail call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %0, i32* null)
%12 = tail call i32 @perror(i8* %0)
%13 = tail call i32 @exit(i32 1)
unreachable
}