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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ errno;
int execl(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ pipe(int *);
int lsopen(char *nombre) {
  int pdes[2];
  int childpid;
  int rc;
  DIR *d;
  if ((d = opendir(nombre)) == NULL)
    return -1;
  closedir(d);
  errno = 0;
  pipe(pdes);
  childpid = fork();
  switch (childpid) {
  case 0:
    close(1);
    dup2(pdes[1], 1);
    close(pdes[0]);
    close(pdes[1]);
    rc = execl("/bin/ls", "/bin/ls", "-l", nombre, (char *)NULL);
    exit(rc);
  case -1:
    return -1;
  default:
    break;
  }
  close(pdes[1]);
  return pdes[0];
}


x86 assembly (clang, -O0):
<lsopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11af <lsopen+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <lsopen+0xed>
mov    -0x28(%rbp),%rdi
call   1070 <closedir@plt>
mov    0x2e21(%rip),%rax
movq   $0x0,(%rax)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     124e <lsopen+0xce>
jmp    11ed <lsopen+0x6d>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jne    125a <lsopen+0xda>
jmp    11fd <lsopen+0x7d>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xdd1(%rip),%rsi
lea    0xdd2(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   1090 <execl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1080 <exit@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <lsopen+0xed>
jmp    125f <lsopen+0xdf>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsopen>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <opendir@plt>
test   %rax,%rax
je     11d1 <lsopen+0x51>
mov    %rax,%rdi
call   1070 <closedir@plt>
mov    0x2e3f(%rip),%rax
movq   $0x0,(%rax)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffff,%eax
je     11d6 <lsopen+0x56>
test   %eax,%eax
je     11dc <lsopen+0x5c>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%eax
jmp    11d6 <lsopen+0x56>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    $0x1,%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xdf3(%rip),%rdi
lea    0xdf4(%rip),%rdx
mov    %rdi,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1090 <execl@plt>
mov    %eax,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"-l\00"
define i32 @lsopen(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %42
12: ; preds = %1
%13 = load i32*, i32** %7
%14 = call i32 @closedir(i32* %13)
store i64 0, i64* @errno
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = call i32 (...) @fork()
store i32 %17, i32* %5
%18 = load i32, i32* %5
switch i32 %18, label %35 [
i32 0, label %19
i32 -1, label %34
]
19: ; preds = %12
%20 = call i32 @close(i32 1)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @dup2(i32 %22, i32 1)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = load i8*, i8** %3
%31 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %30, i8* null)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %12
store i32 -1, i32* %2
br label %42
35: ; preds = %12
br label %36
36: ; preds = %35
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%41 = load i32, i32* %40
store i32 %41, i32* %2
br label %42
42: ; preds = %36, %34, %11
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@errno = global i64 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"-l\00"
define i32 @lsopen(i8* %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = tail call i32* @opendir(i8* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %27, label %6
6: ; preds = %1
%7 = tail call i32 @closedir(i32* %4)
store i64 0, i64* @errno
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = call i32 (...) @fork()
switch i32 %10, label %22 [
i32 0, label %11
i32 -1, label %27
]
11: ; preds = %6
%12 = call i32 @close(i32 1)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @dup2(i32 %14, i32 1)
%16 = load i32, i32* %8
%17 = call i32 @close(i32 %16)
%18 = load i32, i32* %13
%19 = call i32 @close(i32 %18)
%20 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* null)
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %6
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %8
br label %27
27: ; preds = %6, %1, %22
%28 = phi i32 [ %26, %22 ], [ -1, %1 ], [ %10, %6 ]
ret i32 %28
}