#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ me;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ process(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void *cgi_server(void *x_void_ptr) {
  struct sockaddr_in me, from;
  int newSock, sock, adl;
  adl = sizeof(me);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&me, adl);
  me.sin_family = AF_INET;
  me.sin_addr.s_addr = htonl(INADDR_ANY);
  me.sin_port = htons(30473);
  if (-1 == bind(sock, (struct sockaddr *)&me, adl)) {
    puts("Porta ocupada");
    close(sock);
    exit(1);
  }
  listen(sock, SOMAXCONN);
  printf("HTTP server listening on port %d\n", 30473);
  while (1) {
    int s;
    FILE *f;
    s = accept(sock, NULL, NULL);
    if (s < 0)
      break;
    f = fdopen(s, "a+");
    process(f);
    fclose(f);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<cgi_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xc,-0x34(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x30(%rbp)
lea    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10d0 <bzero@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x7709,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x30(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   10c0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1281 <cgi_server+0xa1>
lea    0xd96(%rip),%rdi
call   1030 <puts@plt>
mov    -0x30(%rbp),%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x30(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
call   10a0 <listen@plt>
lea    0xd75(%rip),%rdi
mov    $0x7709,%esi
call   1060 <printf@plt>
mov    -0x30(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10e0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12c4 <cgi_server+0xe4>
jmp    12ee <cgi_server+0x10e>
mov    -0x38(%rbp),%edi
lea    0xd62(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
call   1080 <process@plt>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
jmp    12a3 <cgi_server+0xc3>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cgi_server>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2df9(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    $0x7709,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
je     12c7 <cgi_server+0xe7>
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   10a0 <listen@plt>
lea    0xdab(%rip),%rdi
mov    $0x7709,%esi
call   1060 <printf@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
test   %eax,%eax
js     12bc <cgi_server+0xdc>
lea    0xdad(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%edi
mov    %r14,%rsi
call   10b0 <fdopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <process@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10e0 <accept@plt>
test   %eax,%eax
jns    1290 <cgi_server+0xb0>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd32(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [14 x i8] c"Porta ocupada\00"
@SOMAXCONN = global i32 0
@.str.1 = [34 x i8] c"HTTP server listening on port %d\0A\00"
@.str.2 = [3 x i8] c"a+\00"
define i8* @cgi_server(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i8* %0, i8** %2
store i32 12, i32* %7
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %6
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = load i32, i32* %7
%15 = call i32 @bzero(i8* %13, i32 %14)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 30473)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %6
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = load i32, i32* %7
%27 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 %26)
%28 = icmp eq i32 -1, %27
br i1 %28, label %29, label %34
29: ; preds = %1
%30 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %1
%35 = load i32, i32* %6
%36 = load i32, i32* @SOMAXCONN
%37 = call i32 @listen(i32 %35, i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 30473)
br label %39
39: ; preds = %34, %45
%40 = load i32, i32* %6
%41 = call i32 @accept(i32 %40, i32* null, i32* null)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %39
br label %52
45: ; preds = %39
%46 = load i32, i32* %8
%47 = call i32* @fdopen(i32 %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %47, i32** %9
%48 = load i32*, i32** %9
%49 = call i32 @process(i32* %48)
%50 = load i32*, i32** %9
%51 = call i32 @fclose(i32* %50)
br label %39
52: ; preds = %44
ret i8* null
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [14 x i8] c"Porta ocupada\00"
@SOMAXCONN = global i32 0
@.str.1 = [34 x i8] c"HTTP server listening on port %d\0A\00"
@.str.2 = [3 x i8] c"a+\00"
define noalias i8* @cgi_server(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = call i32 @bzero(i8* %3, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = tail call i32 @htonl(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 30473)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%16 = call i32 @bind(i32 %6, %struct.sockaddr* %15, i32 12)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %1
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @close(i32 %6)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = load i32, i32* @SOMAXCONN
%24 = call i32 @listen(i32 %6, i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 30473)
%26 = call i32 @accept(i32 %6, i32* null, i32* null)
%27 = icmp slt i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %22, %28
%29 = phi i32 [ %33, %28 ], [ %26, %22 ]
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @process(i32* %30)
%32 = call i32 @fclose(i32* %30)
%33 = call i32 @accept(i32 %6, i32* null, i32* null)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %28
35: ; preds = %28, %22
ret i8* null
}