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

/* Variables and functions */
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
char *inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int wait_connection(int sockfd) {
  int new_sockfd = -1;
  if (sockfd != -1) {
    int clilen;
    struct sockaddr_in cli_addr;
    listen(sockfd, 10);
    clilen = sizeof(cli_addr);
    new_sockfd =
        accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
    if (new_sockfd != -1) {
      printf("New connection incoming: %s:%d\n", inet_ntoa(cli_addr.sin_addr),
             ntohs(cli_addr.sin_port));
    }
  }
  return new_sockfd;
}


x86 assembly (clang, -O0):
<wait_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     11c8 <wait_connection+0x78>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
movl   $0x8,-0xc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0xc(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     11c3 <wait_connection+0x73>
mov    -0x14(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <ntohs@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe42(%rip),%rdi
call   1040 <printf@plt>
jmp    11c8 <wait_connection+0x78>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_connection>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0xffffffff,%edi
je     11b0 <wait_connection+0x60>
mov    %edi,%ebx
mov    $0xa,%esi
call   1050 <listen@plt>
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1070 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11b5 <wait_connection+0x65>
mov    0x14(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x10(%rsp),%edi
call   1060 <ntohs@plt>
lea    0xe5c(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    11b5 <wait_connection+0x65>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"New connection incoming: %s:%d\0A\00"
define i32 @wait_connection(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
store i32 -1, i32* %3
%6 = load i32, i32* %2
%7 = icmp ne i32 %6, -1
br i1 %7, label %8, label %25
8: ; preds = %1
%9 = load i32, i32* %2
%10 = call i32 @listen(i32 %9, i32 10)
store i32 8, i32* %4
%11 = load i32, i32* %2
%12 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%13 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %4)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %24
16: ; preds = %8
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i8* @inet_ntoa(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @ntohs(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %22)
br label %24
24: ; preds = %16, %8
br label %25
25: ; preds = %24, %1
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"New connection incoming: %s:%d\0A\00"
define i32 @wait_connection(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = icmp eq i32 %0, -1
br i1 %4, label %21, label %5
5: ; preds = %1
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = tail call i32 @listen(i32 %0, i32 10)
store i32 8, i32* %2
%9 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%10 = call i32 @accept(i32 %0, %struct.sockaddr* %9, i32* %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %20, label %12
12: ; preds = %5
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%14 = load i32, i32* %13
%15 = call i8* @inet_ntoa(i32 %14)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @ntohs(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %15, i32 %18)
br label %20
20: ; preds = %12, %5
br label %21
21: ; preds = %20, %1
%22 = phi i32 [ %10, %20 ], [ -1, %1 ]
ret i32 %22
}