#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ str;
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, 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 recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, num;
  char buf[100];
  struct hostent *he;
  struct sockaddr_in server;
  if (argc != 2) {
    printf("Usage: %s <IP Address>\n", argv[0]);
    exit(1);
  }
  if ((he = gethostbyname(argv[1])) == NULL) {
    printf("gethostbyname() error\n");
    exit(1);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("socket() error\n");
    exit(1);
  }
  bzero(&server, sizeof(server));
  server.sin_family = AF_INET;
  server.sin_port = htons(4321);
  server.sin_addr = *((struct in_addr *)he->h_addr);
  if (connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1) {
    printf("connect() error\n");
    exit(1);
  }
  char str[] = "horst\n";
  if ((num = send(sockfd, str, sizeof(str), 0)) == -1) {
    printf("send() error\n");
    exit(1);
  }
  if ((num = recv(sockfd, buf, 100, 0)) == -1) {
    printf("recv() error\n");
    exit(1);
  }
  buf[num - 1] = '\0';
  printf("server message: %s\n", buf);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1218 <main+0x78>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1255 <main+0xb5>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x98(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x90(%rbp)
mov    $0x10e1,%edi
call   1040 <htons@plt>
mov    %eax,-0x94(%rbp)
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x98(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12cc <main+0x12c>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0xd7e(%rip),%eax
mov    %eax,-0x9f(%rbp)
mov    0xd75(%rip),%ax
mov    %ax,-0x9b(%rbp)
mov    0xd6a(%rip),%al
mov    %al,-0x99(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x9f(%rbp),%rsi
mov    $0x7,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    132c <main+0x18c>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1363 <main+0x1c3>
lea    0xd13(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xcf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x88,%rsp
cmp    $0x2,%edi
jne    12c3 <main+0x113>
mov    0x8(%rsi),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     12de <main+0x12e>
mov    %rax,%r14
mov    0x2e05(%rip),%r12
mov    (%r12),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x137>
mov    %eax,%ebx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    $0x10e1,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     12f0 <main+0x140>
movl   $0xa7473,0xb(%rsp)
movl   $0x73726f68,0x8(%rsp)
73
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     12f9 <main+0x149>
lea    0x20(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     1302 <main+0x152>
cltq
movb   $0x0,-0x1(%rax,%r14,1)
lea    0xd7e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsi),%rsi
lea    0xd33(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd87(%rip),%rdi
jmp    1309 <main+0x159>
lea    0xd6f(%rip),%rdi
jmp    1309 <main+0x159>
lea    0xd56(%rip),%rdi
jmp    1309 <main+0x159>
lea    0xd40(%rip),%rdi
jmp    1309 <main+0x159>
lea    0xd2a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage: %s <IP Address>\0A\00"
@.str.1 = [23 x i8] c"gethostbyname() error\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [16 x i8] c"socket() error\0A\00"
@.str.3 = [17 x i8] c"connect() error\0A\00"
@__const.main.str = [7 x i8] c"horst\0A\00"
@.str.4 = [14 x i8] c"send() error\0A\00"
@.str.5 = [14 x i8] c"recv() error\0A\00"
@.str.6 = [20 x i8] c"server message: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca %struct.hostent*
%10 = alloca %struct.sockaddr_in
%11 = alloca [7 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call %struct.hostent* @gethostbyname(i8* %23)
store %struct.hostent* %24, %struct.hostent** %9
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %6
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29
%38 = call i32 @bzero(%struct.sockaddr_in* %10, i32 12)
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %39, i32* %40
%41 = call i32 @htons(i32 4321)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%44 = load %struct.hostent*, %struct.hostent** %9
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = inttoptr i64 %46 to %struct.in_addr*
%48 = bitcast %struct.in_addr* %43 to i8*
%49 = bitcast %struct.in_addr* %47 to i8*
%50 = load i32, i32* %6
%51 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%52 = call i32 @connect(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %37
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %37
%58 = bitcast [7 x i8]* %11 to i8*
%59 = load i32, i32* %6
%60 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%61 = call i32 @send(i32 %59, i8* %60, i32 7, i32 0)
store i32 %61, i32* %7
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %66
63: ; preds = %57
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %57
%67 = load i32, i32* %6
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%69 = call i32 @recv(i32 %67, i8* %68, i32 100, i32 0)
store i32 %69, i32* %7
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %74
71: ; preds = %66
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %66
%75 = load i32, i32* %7
%76 = sub nsw i32 %75, 1
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 %77
store i8 0, i8* %78
%79 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %79)
%81 = load i32, i32* %6
%82 = call i32 @close(i32 %81)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage: %s <IP Address>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@__const.main.str = [7 x i8] c"horst\0A\00"
@.str.6 = [20 x i8] c"server message: %s\0A\00"
@str = [13 x i8] c"recv() error\00"
@str.7 = [13 x i8] c"send() error\00"
@str.8 = [16 x i8] c"connect() error\00"
@str.9 = [15 x i8] c"socket() error\00"
@str.10 = [22 x i8] c"gethostbyname() error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca [7 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call %struct.hostent* @gethostbyname(i8* %15)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = tail call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.9, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %31, i32* %32
%33 = tail call i32 @htons(i32 4321)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %33, i32* %34
%35 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%36 = load i64, i64* %35
%37 = inttoptr i64 %36 to i32*
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%39 = load i32, i32* %37
store i32 %39, i32* %38
%40 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%41 = call i32 @connect(i32 %24, %struct.sockaddr* %40, i32 12)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %46
43: ; preds = %29
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %29
%47 = getelementptr inbounds [7 x i8], [7 x i8]* %5, i64 0, i64 0
%48 = call i32 @send(i32 %24, i8* %47, i32 7, i32 0)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.7, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = call i32 @recv(i32 %24, i8* %6, i32 100, i32 0)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %53
%60 = add nsw i32 %54, -1
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %61
store i8 0, i8* %62
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%64 = call i32 @close(i32 %24)
ret i32 0
}