#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ i_connect(int, char *, int);
int i_socket();
int /*<<< orphan*/ printf(char *, ...);
int rand();
scalar_t__ read(int, char **, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, int *, int);
int main(int argc, char **argv) {
  if (argc < 3) {
    printf("Usage: client [SERVER_IP] [PORT]");
    printf("\n");
    printf("Server ip: IP adress of the server 127.0.0.1 if local\n\n");
    printf("Port: port on which to connect for the server\n\n");
    return 0;
  }
  char *server_ip = argv[1];
  srand(time(NULL));
  int tcp_port = atoi(argv[2]);
  int s = i_socket();
  printf("Wait for server...\n");
  while (i_connect(s, server_ip, tcp_port) < 0) {
    sleep(1);
  }
  printf("Wait for request...\n");
  while (1) {
    char request[10];
    while (read(s, &request, 10) > 0) {
      printf("Received request: ");
      printf("%s", request);
      printf("\n");
      int random_value = rand();
      printf("Sending %d\n", random_value);
      write(s, &random_value, sizeof(int));
    }
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1200 <main+0x60>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1080 <i_socket@plt>
mov    %eax,-0x20(%rbp)
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <i_connect@plt>
cmp    $0x0,%rax
jge    126c <main+0xcc>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1244 <main+0xa4>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    127f <main+0xdf>
mov    -0x20(%rbp),%edi
lea    -0x2a(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    12f9 <main+0x159>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x2a(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10c0 <rand@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
jmp    127f <main+0xdf>
jmp    127a <main+0xda>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jg     1214 <main+0x54>
lea    0xe26(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe78(%rip),%rdi
call   1050 <puts@plt>
lea    0xea3(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsi,%rbx
mov    0x8(%rsi),%r14
xor    %edi,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <i_socket@plt>
mov    %eax,%ebx
lea    0xdff(%rip),%rdi
call   1050 <puts@plt>
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <i_connect@plt>
test   %rax,%rax
jns    126d <main+0xad>
mov    $0x1,%edi
call   10d0 <sleep@plt>
jmp    1250 <main+0x90>
lea    0xde2(%rip),%rdi
call   1050 <puts@plt>
lea    0xe(%rsp),%r14
lea    0xd9c(%rip),%r15
lea    0xda8(%rip),%r12
lea    0xda4(%rip),%r13
lea    0x8(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    12a0 <main+0xe0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   10e0 <rand@plt>
mov    %eax,0x8(%rsp)
mov    %r13,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1060 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xa,%edx
call   1080 <read@plt>
test   %rax,%rax
jg     12c0 <main+0x100>
jmp    12a0 <main+0xe0>

LLVM IR (-O0):
@.str = [33 x i8] c"Usage: client [SERVER_IP] [PORT]\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [56 x i8] c"Server ip: IP adress of the server 127.0.0.1 if local\0A\0A\00"
@.str.3 = [48 x i8] c"Port: port on which to connect for the server\0A\0A\00"
@.str.4 = [20 x i8] c"Wait for server...\0A\00"
@.str.5 = [21 x i8] c"Wait for request...\0A\00"
@.str.6 = [19 x i8] c"Received request: \00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [12 x i8] c"Sending %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [10 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = call i32 @time(i32* null)
%23 = call i32 @srand(i32 %22)
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
store i32 %27, i32* %7
%28 = call i32 (...) @i_socket()
store i32 %28, i32* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %30
30: ; preds = %36, %18
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = call i64 @i_connect(i32 %31, i8* %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 @sleep(i32 1)
br label %30
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %56
br label %41
41: ; preds = %46, %40
%42 = load i32, i32* %8
%43 = bitcast [10 x i8]* %9 to i8**
%44 = call i64 @read(i32 %42, i8** %43, i32 10)
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%48 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 (...) @rand()
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %8
%55 = call i32 @write(i32 %54, i32* %10, i32 4)
br label %41
56: ; preds = %41
br label %40
}

LLVM IR (-O2):
@.str = [33 x i8] c"Usage: client [SERVER_IP] [PORT]\00"
@.str.6 = [19 x i8] c"Received request: \00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [12 x i8] c"Sending %d\0A\00"
@str = [19 x i8] c"Wait for server...\00"
@str.9 = [20 x i8] c"Wait for request...\00"
@str.10 = [55 x i8] c"Server ip: IP adress of the server 127.0.0.1 if local\0A\00"
@str.11 = [47 x i8] c"Port: port on which to connect for the server\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca i32
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @putchar(i32 10)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.10, i64 0, i64 0))
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.11, i64 0, i64 0))
ret i32 0
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @time(i32* null)
%15 = tail call i32 @srand(i32 %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = tail call i32 (...) @i_socket()
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%21 = tail call i64 @i_connect(i32 %19, i8* %13, i32 %18)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %11, %23
%24 = tail call i32 @sleep(i32 1)
%25 = tail call i64 @i_connect(i32 %19, i8* %13, i32 %18)
%26 = icmp slt i64 %25, 0
br i1 %26, label %23, label %27
27: ; preds = %23, %11
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%30 = bitcast [10 x i8]* %3 to i8**
%31 = bitcast i32* %4 to i8*
br label %32
32: ; preds = %44, %27
%33 = call i64 @read(i32 %19, i8** %30, i32 10)
%34 = icmp sgt i64 %33, 0
br i1 %34, label %35, label %44
35: ; preds = %32, %35
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %29)
%38 = call i32 @putchar(i32 10)
%39 = call i32 (...) @rand()
store i32 %39, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i32 %39)
%41 = call i32 @write(i32 %19, i32* %4, i32 4)
%42 = call i64 @read(i32 %19, i8** %30, i32 10)
%43 = icmp sgt i64 %42, 0
br i1 %43, label %35, label %44
44: ; preds = %35, %32
br label %32
}