#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ log_info(int /*<<< orphan*/);
int /*<<< orphan*/ process_request(int /*<<< orphan*/ *, char *);
int loop(int sock, char *root) {
  int s;
  FILE *f;
  struct sockaddr_in client;
  client.sin_family = AF_INET;
  socklen_t client_len = sizeof(client);
  s = accept(sock, (struct sockaddr *)&client, &client_len);
  if (s < 0)
    return 1;
  f = fdopen(s, "a+");
  log_info(inet_ntoa(client.sin_addr));
  process_request(f, root);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movl   $0x8,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ab <loop+0x4b>
movl   $0x1,-0x4(%rbp)
jmp    11ea <loop+0x8a>
mov    -0x14(%rbp),%edi
lea    0xe4b(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    %eax,%edi
call   1030 <log_info@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <process_request@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loop>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1080 <accept@plt>
test   %eax,%eax
js     11ca <loop+0x6a>
lea    0xe67(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
mov    %rax,%rbx
mov    0x10(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %eax,%edi
call   1030 <log_info@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <process_request@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    11cf <loop+0x6f>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [3 x i8] c"a+\00"
define i32 @loop(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %10, i32* %11
store i32 8, i32* %9
%12 = load i32, i32* %4
%13 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%14 = call i32 @accept(i32 %12, %struct.sockaddr* %13, i32* %9)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 1, i32* %3
br label %30
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %20, i32** %7
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @inet_ntoa(i32 %22)
%24 = call i32 @log_info(i32 %23)
%25 = load i32*, i32** %7
%26 = load i8*, i8** %5
%27 = call i32 @process_request(i32* %25, i8* %26)
%28 = load i32*, i32** %7
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %3
br label %30
30: ; preds = %18, %17
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [3 x i8] c"a+\00"
define i32 @loop(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %6, i32* %7
%8 = bitcast i32* %4 to i8*
store i32 8, i32* %4
%9 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%10 = call i32 @accept(i32 %0, %struct.sockaddr* %9, i32* %4)
%11 = icmp slt i32 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %2
%13 = call i32* @fdopen(i32 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @inet_ntoa(i32 %15)
%17 = call i32 @log_info(i32 %16)
%18 = call i32 @process_request(i32* %13, i8* %1)
%19 = call i32 @fclose(i32* %13)
br label %20
20: ; preds = %2, %12
%21 = phi i32 [ 0, %12 ], [ 1, %2 ]
ret i32 %21
}