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

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connection_handler(int);
void wait_for_work(int socket_fd) {
  int connection_fd;
  struct sockaddr_in client;
  socklen_t length;
  length = sizeof(client);
  while ((connection_fd =
              accept(socket_fd, (struct sockaddr *)&client, &length)) > -1) {
    connection_handler(connection_fd);
    close(connection_fd);
  }
}


x86 assembly (clang, -O0):
<wait_for_work>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1050 <accept@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jle    1173 <wait_for_work+0x43>
mov    -0x8(%rbp),%edi
call   1030 <connection_handler@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    1142 <wait_for_work+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_work>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1050 <accept@plt>
test   %eax,%eax
js     1191 <wait_for_work+0x61>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
call   1030 <connection_handler@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1170 <wait_for_work+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define void @wait_for_work(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %2
store i32 4, i32* %5
br label %6
6: ; preds = %11, %1
%7 = load i32, i32* %2
%8 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%9 = call i32 @accept(i32 %7, %struct.sockaddr* %8, i32* %5)
store i32 %9, i32* %3
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* %3
%13 = call i32 @connection_handler(i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @close(i32 %14)
br label %6
16: ; preds = %6
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define void @wait_for_work(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%6 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %3)
%8 = icmp sgt i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %1, %9
%10 = phi i32 [ %13, %9 ], [ %7, %1 ]
%11 = call i32 @connection_handler(i32 %10)
%12 = call i32 @close(i32 %10)
%13 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %3)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %9, label %15
15: ; preds = %9, %1
ret void
}