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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ logData(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
size_t readSock(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ writeSock(int, char *, int);
void writeArticle(int sock, FILE *logfile, char *action) {
  FILE *file;
  char *p;
  size_t x, y;
  int complete = 0;
  char buf[1024];
  char path[1024];
  strcpy(path, "./articles/");
  strncat(path, &action[1], sizeof(path));
  logData(logfile, "user writing article: %s", path);
  file = fopen(&action[1], "w");
  if (!file) {
    writeSock(sock, "\x33\x31 FILE NOT AVAILABLE!",
              sizeof("\x33\x31 FILE NOT AVAILABLE!"));
    return;
  }
  writeSock(sock, "\x41\x41 BEGIN FILE: END WITH '!!!'",
            sizeof("\x41\x41 BEGIN FILE: END WITH '!!!'"));
  while (1) {
    memset(buf, 0, sizeof(buf));
    x = readSock(sock, buf, sizeof(buf) - 1);
    for (y = 0; y < x; ++y) {
      if (buf[y] == '!') {
        if (buf[y + 1] == '!' && buf[y + 2] == '!') {
          buf[y] = 0x0;
          complete = 1;
        }
      }
    }
    fputs(buf, file);
    if (complete) {
      break;
    }
  }
  writeSock(sock, "\x41\x42 ARTICLE HAS BEEN WRITTEN!",
            sizeof("\x41\x42 ARTICLE HAS BEEN WRITTEN!"));
  fclose(file);
}


x86 assembly (clang, -O0):
<writeArticle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x840(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1050 <strcpy@plt>
lea    -0x840(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x400,%edx
call   1090 <strncat@plt>
mov    -0x10(%rbp),%rdi
lea    -0x840(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1030 <logData@plt>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
lea    0xe26(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122c <writeArticle+0x9c>
mov    -0x4(%rbp),%edi
lea    0xe0a(%rip),%rsi
mov    $0x17,%edx
call   10a0 <writeSock@plt>
jmp    132f <writeArticle+0x19f>
mov    -0x4(%rbp),%edi
lea    0xe08(%rip),%rsi
mov    $0x1e,%edx
call   10a0 <writeSock@plt>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <readSock@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12ee <writeArticle+0x15e>
mov    -0x38(%rbp),%rax
movsbl -0x440(%rbp,%rax,1),%eax
ff
cmp    $0x21,%eax
jne    12d8 <writeArticle+0x148>
mov    -0x38(%rbp),%rax
movsbl -0x43f(%rbp,%rax,1),%eax
ff
cmp    $0x21,%eax
jne    12d3 <writeArticle+0x143>
mov    -0x38(%rbp),%rax
movsbl -0x43e(%rbp,%rax,1),%eax
ff
cmp    $0x21,%eax
jne    12d3 <writeArticle+0x143>
mov    -0x38(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
movl   $0x1,-0x3c(%rbp)
jmp    12d8 <writeArticle+0x148>
jmp    12dd <writeArticle+0x14d>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1273 <writeArticle+0xe3>
lea    -0x440(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <fputs@plt>
cmpl   $0x0,-0x3c(%rbp)
je     130d <writeArticle+0x17d>
jmp    1312 <writeArticle+0x182>
jmp    1240 <writeArticle+0xb0>
mov    -0x4(%rbp),%edi
lea    0xd40(%rip),%rsi
mov    $0x1d,%edx
call   10a0 <writeSock@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeArticle>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r14d
lea    0xe53(%rip),%rsi
lea    0x400(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strcpy@plt>
add    $0x1,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <strncat@plt>
lea    0xe34(%rip),%rsi
mov    %r15,%rdi
mov    %rbp,%rdx
call   1030 <logData@plt>
lea    0xe3b(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a9 <writeArticle+0x119>
mov    %rax,%r15
lea    0xe39(%rip),%rsi
mov    %r14d,%edi
mov    $0x1e,%edx
call   10a0 <writeSock@plt>
mov    %rsp,%rbp
jmp    122f <writeArticle+0x9f>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <fputs@plt>
test   %ebx,%ebx
jne    128b <writeArticle+0xfb>
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   1040 <readSock@plt>
test   %rax,%rax
je     1220 <writeArticle+0x90>
xor    %ebx,%ebx
xor    %ecx,%ecx
jmp    1265 <writeArticle+0xd5>
nopl   0x0(%rax,%rax,1)
cmp    %rcx,%rax
je     1220 <writeArticle+0x90>
mov    %rcx,%rdx
add    $0x1,%rcx
cmpb   $0x21,(%rsp,%rdx,1)
jne    1260 <writeArticle+0xd0>
cmpb   $0x21,0x1(%rsp,%rdx,1)
jne    1260 <writeArticle+0xd0>
cmpb   $0x21,0x2(%rsp,%rdx,1)
jne    1260 <writeArticle+0xd0>
movb   $0x0,(%rsp,%rdx,1)
mov    $0x1,%ebx
jmp    1260 <writeArticle+0xd0>
lea    0xdca(%rip),%rsi
mov    %r14d,%edi
mov    $0x1d,%edx
call   10a0 <writeSock@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
jmp    12bd <writeArticle+0x12d>
lea    0xd77(%rip),%rsi
mov    %r14d,%edi
mov    $0x17,%edx
call   10a0 <writeSock@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"./articles/\00"
@.str.1 = [25 x i8] c"user writing article: %s\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [23 x i8] c"31 FILE NOT AVAILABLE!\00"
@.str.4 = [30 x i8] c"AA BEGIN FILE: END WITH '!!!'\00"
@.str.5 = [29 x i8] c"AB ARTICLE HAS BEEN WRITTEN!\00"
define void @writeArticle(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca [1024 x i8]
%13 = alloca [1024 x i8]
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
store i32 0, i32* %11
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, 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 [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call i32 @strncat(i8* %16, i8* %18, i32 1024)
%20 = load i32*, i32** %5
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%22 = call i32 @logData(i32* %20, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %7
%26 = load i32*, i32** %7
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %3
%29 = load i32, i32* %4
%30 = call i32 @writeSock(i32 %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 23)
br label %85
31: ; preds = %3
%32 = load i32, i32* %4
%33 = call i32 @writeSock(i32 %32, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 30)
br label %34
34: ; preds = %31, %79
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%36 = call i32 @memset(i8* %35, i32 0, i32 1024)
%37 = load i32, i32* %4
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%39 = call i64 @readSock(i32 %37, i8* %38, i32 1023)
store i64 %39, i64* %9
store i64 0, i64* %10
br label %40
40: ; preds = %69, %34
%41 = load i64, i64* %10
%42 = load i64, i64* %9
%43 = icmp ult i64 %41, %42
br i1 %43, label %44, label %72
44: ; preds = %40
%45 = load i64, i64* %10
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 33
br i1 %49, label %50, label %68
50: ; preds = %44
%51 = load i64, i64* %10
%52 = add i64 %51, 1
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 33
br i1 %56, label %57, label %67
57: ; preds = %50
%58 = load i64, i64* %10
%59 = add i64 %58, 2
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp eq i32 %62, 33
br i1 %63, label %64, label %67
64: ; preds = %57
%65 = load i64, i64* %10
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %65
store i8 0, i8* %66
store i32 1, i32* %11
br label %67
67: ; preds = %64, %57, %50
br label %68
68: ; preds = %67, %44
br label %69
69: ; preds = %68
%70 = load i64, i64* %10
%71 = add i64 %70, 1
store i64 %71, i64* %10
br label %40
72: ; preds = %40
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%74 = load i32*, i32** %7
%75 = call i32 @fputs(i8* %73, i32* %74)
%76 = load i32, i32* %11
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %79
78: ; preds = %72
br label %80
79: ; preds = %72
br label %34
80: ; preds = %78
%81 = load i32, i32* %4
%82 = call i32 @writeSock(i32 %81, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 29)
%83 = load i32*, i32** %7
%84 = call i32 @fclose(i32* %83)
br label %85
85: ; preds = %80, %28
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"./articles/\00"
@.str.1 = [25 x i8] c"user writing article: %s\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [23 x i8] c"31 FILE NOT AVAILABLE!\00"
@.str.4 = [30 x i8] c"AA BEGIN FILE: END WITH '!!!'\00"
@.str.5 = [29 x i8] c"AB ARTICLE HAS BEEN WRITTEN!\00"
define void @writeArticle(i32 %0, i32* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%9 = getelementptr inbounds i8, i8* %2, i64 1
%10 = call i32 @strncat(i8* %7, i8* %9, i32 1024)
%11 = call i32 @logData(i32* %1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%12 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @writeSock(i32 %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 23)
br label %49
16: ; preds = %3
%17 = call i32 @writeSock(i32 %0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 30)
br label %18
18: ; preds = %42, %16
%19 = call i32 @memset(i8* %6, i32 0, i32 1024)
%20 = call i64 @readSock(i32 %0, i8* %6, i32 1023)
%21 = icmp eq i64 %20, 0
br i1 %21, label %42, label %22
22: ; preds = %18, %39
%23 = phi i32 [ %40, %39 ], [ 0, %18 ]
%24 = phi i64 [ %28, %39 ], [ 0, %18 ]
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = icmp eq i8 %26, 33
%28 = add nuw i64 %24, 1
br i1 %27, label %29, label %39
29: ; preds = %22
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %28
%31 = load i8, i8* %30
%32 = icmp eq i8 %31, 33
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = add i64 %24, 2
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = icmp eq i8 %36, 33
br i1 %37, label %38, label %39
38: ; preds = %33
store i8 0, i8* %25
br label %39
39: ; preds = %22, %38, %33, %29
%40 = phi i32 [ 1, %38 ], [ %23, %33 ], [ %23, %29 ], [ %23, %22 ]
%41 = icmp eq i64 %28, %20
br i1 %41, label %42, label %22
42: ; preds = %39, %18
%43 = phi i32 [ 0, %18 ], [ %40, %39 ]
%44 = call i32 @fputs(i8* %6, i32* %12)
%45 = icmp eq i32 %43, 0
br i1 %45, label %18, label %46
46: ; preds = %42
%47 = call i32 @writeSock(i32 %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i32 29)
%48 = call i32 @fclose(i32* %12)
br label %49
49: ; preds = %46, %14
ret void
}