#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*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int handle_request(int msgsock, char *dir) {
  if (fork() == 0) {
    dup2(msgsock, STDOUT_FILENO);
    dup2(msgsock, STDERR_FILENO);
    execl("/bin/ls", "ls", "-l", dir, NULL);
    close(msgsock);
    perror("execl");
    exit(1);
  } else {
    close(msgsock);
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11e3 <handle_request+0x83>
mov    -0x4(%rbp),%edi
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rcx
lea    0xe53(%rip),%rdi
lea    0xe51(%rip),%rsi
lea    0xe4d(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <execl@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_request>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     1186 <handle_request+0x26>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
lea    0xe53(%rip),%rdi
lea    0xe51(%rip),%rsi
lea    0xe4d(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1070 <execl@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@.str.3 = [6 x i8] c"execl\00"
define i32 @handle_request(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%5 = call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %2
%8 = load i32, i32* %3
%9 = load i32, i32* @STDOUT_FILENO
%10 = call i32 @dup2(i32 %8, i32 %9)
%11 = load i32, i32* %3
%12 = load i32, i32* @STDERR_FILENO
%13 = call i32 @dup2(i32 %11, i32 %12)
%14 = load i8*, i8** %4
%15 = call i32 @execl(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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32* null)
%16 = load i32, i32* %3
%17 = call i32 @close(i32 %16)
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %20
ret i32 0
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [8 x i8] c"/bin/ls\00"
@.str.1 = [3 x i8] c"ls\00"
@.str.2 = [3 x i8] c"-l\00"
@.str.3 = [6 x i8] c"execl\00"
define i32 @handle_request(i32 %0, i8* %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %14
5: ; preds = %2
%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 = tail call i32 @execl(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* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32* null)
%11 = tail call i32 @close(i32 %0)
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 @close(i32 %0)
ret i32 0
}