#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 EXIT_SUCCESS;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int makeServerSocket(int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ serveBMP(int);
int waitForConnection(int);
int main(int argc, char *argv[]) {
  printf("************************************\n");
  printf("Starting simple server %f\n", 1.0);
  printf("Serving bmps since 2012\n");
  int serverSocket = makeServerSocket(1917);
  printf("Access this server at http://localhost:%d/\n", 1917);
  printf("************************************\n");
  char request[1000];
  int numberServed = 0;
  while (numberServed < 10) {
    printf("*** So far served %d pages ***\n", numberServed);
    int connectionSocket = waitForConnection(serverSocket);
    int bytesRead;
    bytesRead = read(connectionSocket, request, (sizeof request) - 1);
    assert(bytesRead >= 0);
    printf(" *** Received http request ***\n %s\n", request);
    printf(" *** Sending http response ***\n");
    serveBMP(connectionSocket);
    close(connectionSocket);
    numberServed++;
  }
  printf("** shutting down the server **\n");
  close(serverSocket);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe90(%rip),%rdi
movsd  0xe5a(%rip),%xmm0
mov    $0x1,%al
call   1030 <printf@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x77d,%edi
call   1080 <makeServerSocket@plt>
mov    %eax,-0x14(%rbp)
lea    0xe93(%rip),%rdi
mov    $0x77d,%esi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x404(%rbp)
cmpl   $0xa,-0x404(%rbp)
jge    12a0 <main+0x130>
mov    -0x404(%rbp),%esi
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <waitForConnection@plt>
mov    %eax,-0x408(%rbp)
mov    -0x408(%rbp),%edi
lea    -0x400(%rbp),%rsi
mov    $0x3e7,%edx
call   1050 <read@plt>
mov    %eax,-0x40c(%rbp)
cmpl   $0x0,-0x40c(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x400(%rbp),%rsi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x408(%rbp),%edi
call   1070 <serveBMP@plt>
mov    -0x408(%rbp),%edi
call   1040 <close@plt>
mov    -0x404(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x404(%rbp)
jmp    11f3 <main+0x83>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3e8,%rsp
lea    0xf13(%rip),%rbx
mov    %rbx,%rdi
call   1030 <puts@plt>
lea    0xe61(%rip),%rdi
movsd  0xe51(%rip),%xmm0
mov    $0x1,%al
call   1040 <printf@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x77d,%edi
call   1090 <makeServerSocket@plt>
mov    %eax,%r13d
lea    0xe4d(%rip),%rdi
mov    $0x77d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
lea    0xe7b(%rip),%r15
lea    0xef4(%rip),%r12
xor    %ebp,%ebp
nopl   (%rax)
lea    0xe48(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
call   10a0 <waitForConnection@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x3e7,%edx
call   1060 <read@plt>
not    %eax
shr    $0x1f,%eax
mov    %eax,%edi
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <serveBMP@plt>
mov    %r14d,%edi
call   1050 <close@plt>
add    $0x1,%ebp
cmp    $0xa,%ebp
jne    1200 <main+0x80>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [38 x i8] c"************************************\0A\00"
@.str.1 = [27 x i8] c"Starting simple server %f\0A\00"
@.str.2 = [25 x i8] c"Serving bmps since 2012\0A\00"
@.str.3 = [44 x i8] c"Access this server at http://localhost:%d/\0A\00"
@.str.4 = [32 x i8] c"*** So far served %d pages ***\0A\00"
@.str.5 = [36 x i8] c" *** Received http request ***\0A %s\0A\00"
@.str.6 = [32 x i8] c" *** Sending http response ***\0A\00"
@.str.7 = [32 x i8] c"** shutting down the server **\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1000 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double 1.000000e+00)
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @makeServerSocket(i32 1917)
store i32 %14, i32* %6
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 1917)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %8
br label %17
17: ; preds = %20, %2
%18 = load i32, i32* %8
%19 = icmp slt i32 %18, 10
br i1 %19, label %20, label %41
20: ; preds = %17
%21 = load i32, i32* %8
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
%24 = call i32 @waitForConnection(i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 999)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp sge i32 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%35 = load i32, i32* %9
%36 = call i32 @serveBMP(i32 %35)
%37 = load i32, i32* %9
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %8
%40 = add nsw i32 %39, 1
store i32 %40, i32* %8
br label %17
41: ; preds = %17
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* @EXIT_SUCCESS
ret i32 %45
}

LLVM IR (-O2):
@.str.1 = [27 x i8] c"Starting simple server %f\0A\00"
@.str.3 = [44 x i8] c"Access this server at http://localhost:%d/\0A\00"
@.str.4 = [32 x i8] c"*** So far served %d pages ***\0A\00"
@.str.5 = [36 x i8] c" *** Received http request ***\0A %s\0A\00"
@EXIT_SUCCESS = global i32 0
@str.8 = [24 x i8] c"Serving bmps since 2012\00"
@str.9 = [37 x i8] c"************************************\00"
@str.10 = [31 x i8] c"** shutting down the server **\00"
@str.11 = [31 x i8] c" *** Sending http response ***\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1000 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), double 1.000000e+00)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.8, i64 0, i64 0))
%7 = tail call i32 @makeServerSocket(i32 1917)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 1917)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
%10 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
br label %11
11: ; preds = %2, %11
%12 = phi i32 [ 0, %2 ], [ %23, %11 ]
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%14 = call i32 @waitForConnection(i32 %7)
%15 = call i32 @read(i32 %14, i8* %10, i32 999)
%16 = xor i32 %15, -1
%17 = lshr i32 %16, 31
%18 = call i32 @assert(i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %10)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
%21 = call i32 @serveBMP(i32 %14)
%22 = call i32 @close(i32 %14)
%23 = add nuw nsw i32 %12, 1
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %11
25: ; preds = %11
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%27 = call i32 @close(i32 %7)
%28 = load i32, i32* @EXIT_SUCCESS
ret i32 %28
}