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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ display_image(char *, int, int);
int /*<<< orphan*/ exit(int);
scalar_t__ gethostbyname(char *);
int /*<<< orphan*/ htonl(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int receive_mouse(char *ip, int port) {
  struct sockaddr_in server_address;
  char *server_name;
  char buffer[256];
  socklen_t address_size;
  int socket_file_descriptor;
  socket_file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
  memset(&server_address, 0, sizeof(server_address));
  server_address.sin_family = AF_INET;
  server_address.sin_port = htonl(port);
  server_name = (char *)gethostbyname("localhost");
  inet_pton(AF_INET, server_name, &server_address.sin_addr);
  address_size = sizeof(server_address);
  if (connect(socket_file_descriptor, (struct sockaddr *)&server_address,
              address_size) == -1) {
    exit(21);
  }
  while (recv(socket_file_descriptor, buffer, sizeof(buffer), 0) != -1) {
    display_image(buffer, 500, 500);
  }
  return 1;
}


x86 assembly (clang, -O0):
<receive_mouse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x128(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htonl@plt>
mov    %eax,-0x14(%rbp)
lea    0xe11(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1070 <inet_pton@plt>
movl   $0xc,-0x124(%rbp)
mov    -0x128(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x124(%rbp),%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1240 <receive_mouse+0xb0>
mov    $0x15,%edi
call   1090 <exit@plt>
jmp    1245 <receive_mouse+0xb5>
mov    -0x128(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     127f <receive_mouse+0xef>
lea    -0x120(%rbp),%rdi
mov    $0x1f4,%edx
mov    %edx,%esi
call   1080 <display_image@plt>
jmp    1245 <receive_mouse+0xb5>
mov    $0x1,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receive_mouse>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %esi,%ebx
mov    0x2e42(%rip),%r15
mov    (%r15),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
lea    0xe1a(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    (%r15),%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1070 <inet_pton@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     126b <receive_mouse+0xdb>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1258 <receive_mouse+0xc8>
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1f4,%esi
mov    $0x1f4,%edx
call   1080 <display_image@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    1230 <receive_mouse+0xa0>
mov    $0x1,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x15,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
define i32 @receive_mouse(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i8*
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* %4
%17 = call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %17, i32* %18
%19 = call i64 @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %6
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %6
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%24 = call i32 @inet_pton(i32 %21, i8* %22, i32* %23)
store i32 12, i32* %8
%25 = load i32, i32* %9
%26 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%27 = load i32, i32* %8
%28 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = call i32 @exit(i32 21)
unreachable
32: ; preds = %2
br label %33
33: ; preds = %38, %32
%34 = load i32, i32* %9
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%36 = call i32 @recv(i32 %34, i8* %35, i32 256, i32 0)
%37 = icmp ne i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%40 = call i32 @display_image(i8* %39, i32 500, i32 500)
br label %33
41: ; preds = %33
ret i32 1
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"localhost\00"
define i32 @receive_mouse(i8* nocapture readnone %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htonl(i32 %1)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = call i64 @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = inttoptr i64 %15 to i8*
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%19 = call i32 @inet_pton(i32 %17, i8* %16, i32* %18)
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @connect(i32 %9, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %26, label %23
23: ; preds = %2
%24 = call i32 @recv(i32 %9, i8* %6, i32 256, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %32, label %28
26: ; preds = %2
%27 = call i32 @exit(i32 21)
unreachable
28: ; preds = %23, %28
%29 = call i32 @display_image(i8* %6, i32 500, i32 500)
%30 = call i32 @recv(i32 %9, i8* %6, i32 256, i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %28
32: ; preds = %28, %23
ret i32 1
}