#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 */
typedef int /*<<< orphan*/ target;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
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);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, ...);
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;
  struct hostent *he;
  int sock, num;
  if (argc != 2) {
    puts("falta indicar o nome do servidor");
    exit(1);
  }
  he = gethostbyname(argv[1]);
  if (!he) {
    printf("Falhou a resolução do nome %s\n", argv[1]);
    exit(1);
  }
  sock = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&target, sizeof(target));
  target.sin_family = AF_INET;
  target.sin_addr = *(struct in_addr *)he->h_addr_list[0];
  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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    122b <main+0x6b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0xead0,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ad <main+0xed>
lea    0xda4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12b2 <main+0xf2>
jmp    12b7 <main+0xf7>
lea    0xd95(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   10a0 <scanf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmpl   $0x0,-0x30(%rbp)
jne    12b7 <main+0xf7>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    -0x30(%rbp),%esi
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x30(%rbp)
jne    12b2 <main+0xf2>
mov    -0x2c(%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    12c7 <main+0x107>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     12d0 <main+0x110>
mov    %rax,%rbx
mov    0x2df6(%rip),%r15
mov    (%r15),%edi
mov    0x2ddc(%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    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%eax
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     12ec <main+0x12c>
lea    0xdfa(%rip),%r14
lea    0x4(%rsp),%rbx
lea    0xdf1(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
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    1270 <main+0xb0>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0x4(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x0,0x4(%rsp)
jne    1270 <main+0xb0>
mov    %ebp,%edi
call   1070 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd32(%rip),%rdi
jmp    12f3 <main+0x133>
mov    0x8(%rbp),%rsi
lea    0xd46(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd4f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@.str = [33 x i8] c"falta indicar o nome do servidor\00"
@.str.1 = [33 x i8] c"Falhou a resolu\C3\A7\C3\A3o do nome %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"A ligacao falhou\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [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 %struct.hostent*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %6
%19 = load %struct.hostent*, %struct.hostent** %6
%20 = icmp ne %struct.hostent* %19, null
br i1 %20, label %27, label %21
21: ; preds = %14
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %7
%31 = bitcast %struct.sockaddr_in* %5 to i8*
%32 = call i32 @bzero(i8* %31, i32 12)
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%36 = load %struct.hostent*, %struct.hostent** %6
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 0
%38 = load i64*, i64** %37
%39 = getelementptr inbounds i64, i64* %38, i64 0
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to %struct.in_addr*
%42 = bitcast %struct.in_addr* %35 to i8*
%43 = bitcast %struct.in_addr* %41 to i8*
%44 = call i32 @htons(i32 60112)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %7
%47 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%48 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %27
%51 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %27
br label %54
54: ; preds = %67, %53
br label %55
55: ; preds = %59, %54
%56 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %8)
%57 = load i32, i32* %7
%58 = call i32 @write(i32 %57, i32* %8, i32 4)
br label %59
59: ; preds = %55
%60 = load i32, i32* %8
%61 = icmp ne i32 %60, 0
br i1 %61, label %55, label %62
62: ; preds = %59
%63 = load i32, i32* %7
%64 = call i32 @read(i32 %63, i32* %8, i32 4)
%65 = load i32, i32* %8
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %62
%68 = load i32, i32* %8
%69 = icmp ne i32 %68, 0
br i1 %69, label %54, label %70
70: ; preds = %67
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@.str = [33 x i8] c"falta indicar o nome do servidor\00"
@.str.1 = [33 x i8] c"Falhou a resolu\C3\A7\C3\A3o do nome %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [17 x i8] c"A ligacao falhou\00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [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 ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call %struct.hostent* @gethostbyname(i8* %13)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i8*, i8** %12
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = call i32 @bzero(i8* %5, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%28 = load i64*, i64** %27
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to i32*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%32 = load i32, i32* %30
store i32 %32, i32* %31
%33 = tail call i32 @htons(i32 60112)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i32 @connect(i32 %23, %struct.sockaddr* %35, i32 12)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %20
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %20, %46
%42 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %4)
%43 = call i32 @write(i32 %23, i32* %4, i32 4)
%44 = load i32, i32* %4
%45 = icmp eq i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %41, %47
br label %41
47: ; preds = %41
%48 = call i32 @read(i32 %23, i32* %4, i32 4)
%49 = load i32, i32* %4
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %4
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %46
53: ; preds = %47
%54 = call i32 @close(i32 %23)
ret void
}