#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 /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int client_mode(int sock, int port) {
  struct sockaddr_in address;
  int valread;
  struct sockaddr_in serv_addr;
  char *hello = "Hello from client";
  char buffer[1024] = {0};
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("\n Socket creation error \n");
    return -1;
  }
  memset(&serv_addr, '0', sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
    perror("ERROR: failed to do something I guess ://");
    exit(EXIT_FAILURE);
  }
  if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR: connection failed");
    exit(EXIT_FAILURE);
  }
  send(sock, hello, strlen(hello), 0);
  printf("Hello message sent\n");
  valread = read(sock, buffer, 1024);
  printf("%s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<client_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe38(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    121e <client_mode+0x6e>
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132e <client_mode+0x17e>
lea    -0x28(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rdx
lea    0xdd0(%rip),%rsi
call   1090 <inet_pton@plt>
cmp    $0x0,%rax
jg     1285 <client_mode+0xd5>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12ba <client_mode+0x10a>
lea    0xdb9(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x430(%rbp),%rsi
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_mode>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %esi,%r15d
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2e10(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1291 <client_mode+0xe1>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
lea    0xdeb(%rip),%rsi
mov    %r14,%rdx
call   1090 <inet_pton@plt>
test   %rax,%rax
jle    12b2 <client_mode+0x102>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     12bb <client_mode+0x10b>
lea    0xdb1(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    0xdf1(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    12a2 <client_mode+0xf2>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd63(%rip),%rdi
jmp    12c2 <client_mode+0x112>
lea    0xd84(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Hello from client\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [26 x i8] c"\0A Socket creation error \0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [42 x i8] c"ERROR: failed to do something I guess ://\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [25 x i8] c"ERROR: connection failed\00"
@.str.5 = [20 x i8] c"Hello message sent\0A\00"
@.str.6 = [4 x i8] c"%s\0A\00"
define i32 @client_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i8*
%10 = alloca [1024 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8** %9
%11 = bitcast [1024 x i8]* %10 to i8*
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
18: ; preds = %2
%19 = call i32 @memset(%struct.sockaddr_in* %8, i32 48, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %5
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%27 = call i64 @inet_pton(i32 %25, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32* %26)
%28 = icmp sle i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %18
%34 = load i32, i32* %4
%35 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%36 = call i64 @connect(i32 %34, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %33
%43 = load i32, i32* %4
%44 = load i8*, i8** %9
%45 = load i8*, i8** %9
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @send(i32 %43, i8* %44, i32 %46, i32 0)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32, i32* %4
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 1024)
store i32 %51, i32* %7
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %52)
store i32 0, i32* %3
br label %54
54: ; preds = %42, %16
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"Hello from client\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [42 x i8] c"ERROR: failed to do something I guess ://\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [25 x i8] c"ERROR: connection failed\00"
@str = [19 x i8] c"Hello message sent\00"
@str.7 = [25 x i8] c"\0A Socket creation error \00"
define i32 @client_mode(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 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 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
br label %41
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 48, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%21 = call i64 @inet_pton(i32 %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32* %20)
%22 = icmp slt i64 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %13
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @connect(i32 %9, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %27
%36 = call i32 @strlen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @send(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %36, i32 0)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%39 = call i32 @read(i32 %9, i8* %6, i32 1024)
%40 = call i32 @puts(i8* %6)
br label %41
41: ; preds = %35, %11
%42 = phi i32 [ -1, %11 ], [ 0, %35 ]
ret i32 %42
}