#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ logData(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ writeSock(int, char *, int);
void readArticle(int sock, FILE *logfile, char *action) {
  FILE *file;
  char buf[100];
  char path[100];
  logData(logfile, &action[1]);
  strcpy(path, "./articles/");
  strcat(path, &action[1]);
  logData(logfile, "user request to read article: %s", path);
  file = fopen(path, "r");
  if (!file) {
    writeSock(sock, "\x33\x31 FILE NOT AVAILABLE!",
              sizeof("\x33\x31 FILE NOT AVAILABLE!"));
    return;
  }
  while (fgets(buf, 1000, file)) {
    writeSock(sock, buf, strlen(buf));
  }
  fclose(file);
  return;
}


x86 assembly (clang, -O0):
<readArticle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x0,%al
call   1030 <logData@plt>
lea    -0x100(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x100(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   10a0 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x100(%rbp),%rdx
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1030 <logData@plt>
lea    -0x100(%rbp),%rdi
lea    0xe36(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1224 <readArticle+0xa4>
mov    -0x4(%rbp),%edi
lea    0xe1a(%rip),%rsi
mov    $0x17,%edx
call   1080 <writeSock@plt>
jmp    128d <readArticle+0x10d>
jmp    1229 <readArticle+0xa9>
lea    -0x90(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3e8,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1284 <readArticle+0x104>
mov    -0x4(%rbp),%eax
mov    %eax,-0x10c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x108(%rbp)
lea    -0x90(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rsi
mov    %eax,%edx
call   1080 <writeSock@plt>
jmp    1229 <readArticle+0xa9>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readArticle>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xd8,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
lea    0x1(%rdx),%rbp
mov    %rsi,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1030 <logData@plt>
lea    0xe55(%rip),%rsi
lea    0x70(%rsp),%r15
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
call   10a0 <strcat@plt>
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <logData@plt>
lea    0xe4f(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1244 <readArticle+0xc4>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     123a <readArticle+0xba>
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1080 <writeSock@plt>
mov    %rbp,%rdi
mov    $0x3e8,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
jne    1210 <readArticle+0x90>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1258 <readArticle+0xd8>
lea    0xde4(%rip),%rsi
mov    %r14d,%edi
mov    $0x17,%edx
call   1080 <writeSock@plt>
add    $0xd8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"./articles/\00"
@.str.1 = [33 x i8] c"user request to read article: %s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"31 FILE NOT AVAILABLE!\00"
define void @readArticle(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [100 x i8]
%9 = alloca [100 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
%10 = load i32*, i32** %5
%11 = load i8*, i8** %6
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = call i32 (i32*, i8*, ...) @logData(i32* %10, i8* %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call i32 @strcat(i8* %16, i8* %18)
%20 = load i32*, i32** %5
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%22 = call i32 (i32*, i8*, ...) @logData(i32* %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %7
%25 = load i32*, i32** %7
%26 = icmp ne i32* %25, null
br i1 %26, label %30, label %27
27: ; preds = %3
%28 = load i32, i32* %4
%29 = call i32 @writeSock(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 23)
br label %45
30: ; preds = %3
br label %31
31: ; preds = %36, %30
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%33 = load i32*, i32** %7
%34 = call i64 @fgets(i8* %32, i32 1000, i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %31
%37 = load i32, i32* %4
%38 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = call i32 @writeSock(i32 %37, i8* %38, i32 %40)
br label %31
42: ; preds = %31
%43 = load i32*, i32** %7
%44 = call i32 @fclose(i32* %43)
br label %45
45: ; preds = %42, %27
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"./articles/\00"
@.str.1 = [33 x i8] c"user request to read article: %s\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [23 x i8] c"31 FILE NOT AVAILABLE!\00"
define void @readArticle(i32 %0, i32* %1, i8* %2) {
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds i8, i8* %2, i64 1
%9 = tail call i32 (i32*, i8*, ...) @logData(i32* %1, i8* %8)
%10 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @strcat(i8* %7, i8* %8)
%12 = call i32 (i32*, i8*, ...) @logData(i32* %1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%13 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = call i64 @fgets(i8* %6, i32 1000, i32* %13)
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %20
18: ; preds = %3
%19 = call i32 @writeSock(i32 %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 23)
br label %27
20: ; preds = %15, %20
%21 = call i32 @strlen(i8* %6)
%22 = call i32 @writeSock(i32 %0, i8* %6, i32 %21)
%23 = call i64 @fgets(i8* %6, i32 1000, i32* %13)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %20
25: ; preds = %20, %15
%26 = call i32 @fclose(i32* %13)
br label %27
27: ; preds = %25, %18
ret void
}