#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 socklen_t;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int, int);
int get_tcp_server_connection(int sock) {
  socklen_t sin_size;
  int connection = 0;
  struct sockaddr_in c_addr;
  sin_size = sizeof(struct sockaddr_in);
  connection = accept(sock, (struct sockaddr *)&c_addr, &sin_size);
  if (connection > 0) {
    printf("Got connection from (%s , %d) [sock = %d]\n",
           inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), sock);
  } else {
    connection = 0;
  }
  return connection;
}


x86 assembly (clang, -O0):
<get_tcp_server_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x8,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x8(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11a4 <get_tcp_server_connection+0x64>
mov    -0x14(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <ntohs@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
mov    -0x4(%rbp),%ecx
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
jmp    11ab <get_tcp_server_connection+0x6b>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_tcp_server_connection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
call   1060 <accept@plt>
test   %eax,%eax
jle    1191 <get_tcp_server_connection+0x51>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r14
mov    0x8(%rsp),%edi
call   1050 <ntohs@plt>
lea    0xe7d(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %ebx,%ecx
call   1040 <printf@plt>
jmp    1193 <get_tcp_server_connection+0x53>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [43 x i8] c"Got connection from (%s , %d) [sock = %d]\0A\00"
define i32 @get_tcp_server_connection(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 8, i32* %2
%6 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%7 = call i32 @accept(i32 %0, %struct.sockaddr* %6, i32* %2)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %1
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%11 = load i32, i32* %10
%12 = call i8* @inet_ntoa(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @ntohs(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %15, i32 %0)
br label %17
17: ; preds = %1, %9
%18 = phi i32 [ %7, %9 ], [ 0, %1 ]
ret i32 %18
}