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

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int write_to_client(int, char *);
int write_OK(int clientsd, char *curr_time, FILE *file, char *file_length_buf) {
  int total_written = 0;
  int written = 0;
  char buffer[4096] = {0};
  write_to_client(clientsd, "HTTP/1.1 200 OK\n");
  write_to_client(clientsd, "Date: ");
  write_to_client(clientsd, curr_time);
  write_to_client(clientsd, "\nContent-Type: text/html\n");
  write_to_client(clientsd, "Content-Length: ");
  write_to_client(clientsd, file_length_buf);
  write_to_client(clientsd, "\n\n");
  while (fgets(buffer, sizeof(buffer), file) != NULL) {
    written = write_to_client(clientsd, buffer);
    if (written == -1)
      return total_written;
    else
      total_written += written;
  }
  return total_written;
}


x86 assembly (clang, -O0):
<write_OK>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x1030(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    0xe8b(%rip),%rsi
call   1030 <write_to_client@plt>
mov    -0x8(%rbp),%edi
lea    0xe8d(%rip),%rsi
call   1030 <write_to_client@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <write_to_client@plt>
mov    -0x8(%rbp),%edi
lea    0xe79(%rip),%rsi
call   1030 <write_to_client@plt>
mov    -0x8(%rbp),%edi
lea    0xe84(%rip),%rsi
call   1030 <write_to_client@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <write_to_client@plt>
mov    -0x8(%rbp),%edi
lea    0xe7a(%rip),%rsi
call   1030 <write_to_client@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x1000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1222 <write_OK+0xf2>
mov    -0x8(%rbp),%edi
lea    -0x1030(%rbp),%rsi
call   1030 <write_to_client@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1214 <write_OK+0xe4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1228 <write_OK+0xf8>
mov    -0x28(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
jmp    11ce <write_OK+0x9e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_OK>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %edi,%r13d
mov    %rsp,%r15
xor    %ebx,%ebx
mov    $0x1000,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe98(%rip),%rsi
mov    %r13d,%edi
call   1030 <write_to_client@plt>
lea    0xe9a(%rip),%rsi
mov    %r13d,%edi
call   1030 <write_to_client@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1030 <write_to_client@plt>
lea    0xe87(%rip),%rsi
mov    %r13d,%edi
call   1030 <write_to_client@plt>
lea    0xe92(%rip),%rsi
mov    %r13d,%edi
call   1030 <write_to_client@plt>
mov    %r13d,%edi
mov    %r12,%rsi
call   1030 <write_to_client@plt>
lea    0xe89(%rip),%rsi
mov    %r13d,%edi
call   1030 <write_to_client@plt>
mov    %r15,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1207 <write_OK+0xd7>
xor    %ebx,%ebx
mov    %rsp,%rbp
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbp,%rsi
call   1030 <write_to_client@plt>
cmp    $0xffffffff,%eax
je     1207 <write_OK+0xd7>
add    %eax,%ebx
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11e0 <write_OK+0xb0>
mov    %ebx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.1 = [7 x i8] c"Date: \00"
@.str.2 = [26 x i8] c"\0AContent-Type: text/html\0A\00"
@.str.3 = [17 x i8] c"Content-Length: \00"
@.str.4 = [3 x i8] c"\0A\0A\00"
define i32 @write_OK(i32 %0, i8* %1, i32* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [4096 x i8]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i8* %3, i8** %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = bitcast [4096 x i8]* %12 to i8*
%14 = load i32, i32* %6
%15 = call i32 @write_to_client(i32 %14, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* %6
%17 = call i32 @write_to_client(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* %6
%19 = load i8*, i8** %7
%20 = call i32 @write_to_client(i32 %18, i8* %19)
%21 = load i32, i32* %6
%22 = call i32 @write_to_client(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* %6
%24 = call i32 @write_to_client(i32 %23, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %6
%26 = load i8*, i8** %9
%27 = call i32 @write_to_client(i32 %25, i8* %26)
%28 = load i32, i32* %6
%29 = call i32 @write_to_client(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %47, %4
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%32 = load i32*, i32** %8
%33 = call i32* @fgets(i8* %31, i32 4096, i32* %32)
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %48
35: ; preds = %30
%36 = load i32, i32* %6
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%38 = call i32 @write_to_client(i32 %36, i8* %37)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = load i32, i32* %10
store i32 %42, i32* %5
br label %50
43: ; preds = %35
%44 = load i32, i32* %11
%45 = load i32, i32* %10
%46 = add nsw i32 %45, %44
store i32 %46, i32* %10
br label %47
47: ; preds = %43
br label %30
48: ; preds = %30
%49 = load i32, i32* %10
store i32 %49, i32* %5
br label %50
50: ; preds = %48, %41
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@.str = [17 x i8] c"HTTP/1.1 200 OK\0A\00"
@.str.1 = [7 x i8] c"Date: \00"
@.str.2 = [26 x i8] c"\0AContent-Type: text/html\0A\00"
@.str.3 = [17 x i8] c"Content-Length: \00"
@.str.4 = [3 x i8] c"\0A\0A\00"
define i32 @write_OK(i32 %0, i8* %1, i32* nocapture %2, i8* %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @write_to_client(i32 %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @write_to_client(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @write_to_client(i32 %0, i8* %1)
%10 = tail call i32 @write_to_client(i32 %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @write_to_client(i32 %0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @write_to_client(i32 %0, i8* %3)
%13 = tail call i32 @write_to_client(i32 %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%14 = call i32* @fgets(i8* %6, i32 4096, i32* %2)
%15 = icmp eq i32* %14, null
br i1 %15, label %24, label %19
16: ; preds = %19
%17 = call i32* @fgets(i8* %6, i32 4096, i32* %2)
%18 = icmp eq i32* %17, null
br i1 %18, label %24, label %19
19: ; preds = %4, %16
%20 = phi i32 [ %23, %16 ], [ 0, %4 ]
%21 = call i32 @write_to_client(i32 %0, i8* %6)
%22 = icmp eq i32 %21, -1
%23 = add nsw i32 %21, %20
br i1 %22, label %24, label %16
24: ; preds = %19, %16, %4
%25 = phi i32 [ 0, %4 ], [ %23, %16 ], [ %20, %19 ]
ret i32 %25
}