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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int kaccept(int, struct sockaddr *, int *);
int /*<<< orphan*/ stderr;
int socket_server_wait(int sd) {
  int nsd, clientlen;
  struct sockaddr_in client;
  unsigned char *p;
  clientlen = sizeof(client);
  fprintf(stderr, "wait for client...\n");
  do {
    nsd = kaccept(sd, (struct sockaddr *)&client, &clientlen);
  } while (nsd <= 0);
  p = (unsigned char *)&client.sin_addr;
  return nsd;
}


x86 assembly (clang, -O0):
<socket_server_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
lea    0xebe(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1040 <kaccept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1147 <socket_server_wait+0x27>
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_server_wait>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x4,0x4(%rsp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%edi
lea    0xebd(%rip),%rsi
call   1030 <fprintf@plt>
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <kaccept@plt>
test   %eax,%eax
jle    1160 <socket_server_wait+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"wait for client...\0A\00"
define i32 @socket_server_wait(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i8*
store i32 %0, i32* %2
store i32 4, i32* %4
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %13, %1
%10 = load i32, i32* %2
%11 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%12 = call i32 @kaccept(i32 %10, %struct.sockaddr* %11, i32* %4)
store i32 %12, i32* %3
br label %13
13: ; preds = %9
%14 = load i32, i32* %3
%15 = icmp sle i32 %14, 0
br i1 %15, label %9, label %16
16: ; preds = %13
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%18 = bitcast i32* %17 to i8*
store i8* %18, i8** %6
%19 = load i32, i32* %3
ret i32 %19
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [20 x i8] c"wait for client...\0A\00"
define i32 @socket_server_wait(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
store i32 4, i32* %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%8 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %9
9: ; preds = %9, %1
%10 = call i32 @kaccept(i32 %0, %struct.sockaddr* %8, i32* %2)
%11 = icmp slt i32 %10, 1
br i1 %11, label %9, label %12
12: ; preds = %9
ret i32 %10
}