#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ logData(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ writeSock(int, char *, int /*<<< orphan*/);
void listArticles(int sock, FILE *logfile, char *action) {
  char buf[100];
  FILE *list;
  logData(logfile, "user has requested a list of articles");
  memset(buf, 0, sizeof(buf));
  system("ls ./articles/ > list.txt");
  list = fopen("list.txt", "r");
  while (fgets(buf, sizeof(buf) - 1, list)) {
    writeSock(sock, buf, strlen(buf));
  }
  fclose(list);
  return;
}


x86 assembly (clang, -O0):
<listArticles>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe5f(%rip),%rsi
call   1030 <logData@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
lea    0xe69(%rip),%rdi
call   1060 <system@plt>
lea    0xe6e(%rip),%rdi
lea    0xe70(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x88(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x88(%rbp),%rdx
mov    $0x63,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1231 <listArticles+0xb1>
mov    -0x4(%rbp),%eax
mov    %eax,-0x94(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x90(%rbp)
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x94(%rbp),%edi
mov    -0x90(%rbp),%rsi
mov    %eax,%edx
call   1090 <writeSock@plt>
jmp    11dc <listArticles+0x5c>
mov    -0x88(%rbp),%rdi
call   1040 <fclose@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listArticles>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %rsi,%rax
mov    %edi,%r14d
lea    0xe6b(%rip),%rsi
mov    %rax,%rdi
call   1030 <logData@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
lea    0xe70(%rip),%rdi
call   1060 <system@plt>
lea    0xe75(%rip),%rdi
lea    0xe77(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x63,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     121a <listArticles+0x9a>
mov    %rsp,%rbp
nopl   0x0(%rax)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1090 <writeSock@plt>
mov    %rbp,%rdi
mov    $0x63,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    11f0 <listArticles+0x70>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"user has requested a list of articles\00"
@.str.1 = [26 x i8] c"ls ./articles/ > list.txt\00"
@.str.2 = [9 x i8] c"list.txt\00"
@.str.3 = [2 x i8] c"r\00"
define void @listArticles(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca [100 x i8]
%8 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%9 = load i32*, i32** %5
%10 = call i32 @logData(i32* %9, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%12 = call i32 @memset(i8* %11, i32 0, i32 100)
%13 = call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %14, i32** %8
br label %15
15: ; preds = %20, %3
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%17 = load i32*, i32** %8
%18 = call i64 @fgets(i8* %16, i32 99, i32* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* %4
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = call i32 @writeSock(i32 %21, i8* %22, i32 %24)
br label %15
26: ; preds = %15
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
ret void
}

LLVM IR (-O2):
@.str = [38 x i8] c"user has requested a list of articles\00"
@.str.1 = [26 x i8] c"ls ./articles/ > list.txt\00"
@.str.2 = [9 x i8] c"list.txt\00"
@.str.3 = [2 x i8] c"r\00"
define void @listArticles(i32 %0, i32* %1, i8* nocapture readnone %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @logData(i32* %1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @memset(i8* %5, i32 0, i32 100)
%8 = call i32 @system(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%10 = call i64 @fgets(i8* %5, i32 99, i32* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %3, %12
%13 = call i32 @strlen(i8* %5)
%14 = call i32 @writeSock(i32 %0, i8* %5, i32 %13)
%15 = call i64 @fgets(i8* %5, i32 99, i32* %9)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %12
17: ; preds = %12, %3
%18 = call i32 @fclose(i32* %9)
ret void
}