#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ target;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ num;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ scanf(char *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
void main(int argc, char **argv) {
  struct sockaddr_in target;
  int sock, num;
  if (argc != 2) {
    puts("falta indicar o endereco IP do servidor");
    exit(1);
  }
  sock = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&target, sizeof(target));
  target.sin_family = AF_INET;
  target.sin_addr.s_addr = inet_addr(argv[1]);
  target.sin_port = htons(60112);
  if (connect(sock, (struct sockaddr *)&target, sizeof(target)) == -1) {
    puts("A ligacao falhou");
    exit(1);
  }
  do {
    do {
      scanf("%d", &num);
      write(sock, &num, sizeof(num));
    } while (num);
    read(sock, &num, sizeof(num));
    printf("SOMA=%d\n", num);
  } while (num);
  close(sock);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11ef <main+0x2f>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x24(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xead0,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1272 <main+0xb2>
lea    0xdc5(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1277 <main+0xb7>
jmp    127c <main+0xbc>
lea    0xdb6(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   10a0 <scanf@plt>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmpl   $0x0,-0x28(%rbp)
jne    127c <main+0xbc>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    -0x28(%rbp),%esi
lea    0xd7a(%rip),%rdi
call   1060 <printf@plt>
cmpl   $0x0,-0x28(%rbp)
jne    1277 <main+0xb7>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    12a5 <main+0xe5>
mov    %rsi,%rbx
mov    0x2e0b(%rip),%r15
mov    (%r15),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbx),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0xead0,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     12ae <main+0xee>
lea    0xdf8(%rip),%r14
lea    0x4(%rsp),%rbx
lea    0xdef(%rip),%r15
nopl   (%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmpl   $0x0,0x4(%rsp)
jne    1250 <main+0x90>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
call   1060 <printf@plt>
cmpl   $0x0,0x4(%rsp)
jne    1250 <main+0x90>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd54(%rip),%rdi
jmp    12b5 <main+0xf5>
lea    0xd73(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [40 x i8] c"falta indicar o endereco IP do servidor\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"A ligacao falhou\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [9 x i8] c"SOMA=%d\0A\00"
define void @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 @puts(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = bitcast %struct.sockaddr_in* %5 to i8*
%18 = call i32 @bzero(i8* %17, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i8**, i8*** %4
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @inet_addr(i8* %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = call i32 @htons(i32 60112)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %27, i32* %28
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%31 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %13
%34 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %13
br label %37
37: ; preds = %50, %36
br label %38
38: ; preds = %42, %37
%39 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %7)
%40 = load i32, i32* %6
%41 = call i32 @write(i32 %40, i32* %7, i32 4)
br label %42
42: ; preds = %38
%43 = load i32, i32* %7
%44 = icmp ne i32 %43, 0
br i1 %44, label %38, label %45
45: ; preds = %42
%46 = load i32, i32* %6
%47 = call i32 @read(i32 %46, i32* %7, i32 4)
%48 = load i32, i32* %7
%49 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %45
%51 = load i32, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %37, label %53
53: ; preds = %50
%54 = load i32, i32* %6
%55 = call i32 @close(i32 %54)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [40 x i8] c"falta indicar o endereco IP do servidor\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"A ligacao falhou\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [9 x i8] c"SOMA=%d\0A\00"
define void @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = call i32 @bzero(i8* %5, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call i32 @inet_addr(i8* %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 60112)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %14, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %11
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %11, %35
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%32 = call i32 @write(i32 %14, i32* %4, i32 4)
%33 = load i32, i32* %4
%34 = icmp eq i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %30, %36
br label %30
36: ; preds = %30
%37 = call i32 @read(i32 %14, i32* %4, i32 4)
%38 = load i32, i32* %4
%39 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %35
42: ; preds = %36
%43 = call i32 @close(i32 %14)
ret void
}