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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(void) {
  int s, n;
  struct sockaddr_in sa;
  char buf[32];
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  sa.sin_family = AF_INET;
  sa.sin_port = htons(50000);
  if (!inet_aton("127.0.0.1", &sa.sin_addr)) {
    perror("inet_aton");
    exit(1);
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    perror("connect");
    exit(1);
  }
  if ((n = read(s, buf, 32)) < 0) {
    perror("read");
    exit(1);
  }
  close(s);
  write(STDOUT_FILENO, buf, n);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x4a>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0xc350,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xe09(%rip),%rdi
call   1070 <inet_aton@plt>
cmp    $0x0,%eax
jne    1222 <main+0x92>
lea    0xdfe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1253 <main+0xc3>
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1286 <main+0xf6>
lea    0xdac(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
jns    11bc <main+0x2c>
lea    0xe46(%rip),%rdi
jmp    122e <main+0x9e>
mov    %eax,%ebx
mov    0x2e1b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0xc350,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe27(%rip),%rdi
mov    %rsp,%rsi
call   1070 <inet_aton@plt>
test   %eax,%eax
jne    11f5 <main+0x65>
lea    0xe1e(%rip),%rdi
jmp    122e <main+0x9e>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jns    1212 <main+0x82>
lea    0xe0b(%rip),%rdi
jmp    122e <main+0x9e>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x20,%edx
call   1060 <read@plt>
test   %eax,%eax
jns    123d <main+0xad>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
mov    %ebp,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"inet_aton\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [5 x i8] c"read\00"
@STDOUT_FILENO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [32 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i32 @htons(i32 50000)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%19 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = load i32, i32* %2
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 32)
store i32 %35, i32* %3
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %32
%41 = load i32, i32* %2
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @STDOUT_FILENO
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%45 = load i32, i32* %3
%46 = call i32 @write(i32 %43, i8* %44, i32 %45)
%47 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [10 x i8] c"inet_aton\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [5 x i8] c"read\00"
@STDOUT_FILENO = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [32 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 50000)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%18 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %12
%24 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%25 = call i64 @connect(i32 %7, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = call i32 @read(i32 %7, i8* %4, i32 32)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = call i32 @close(i32 %7)
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i8* %4, i32 %31)
%40 = call i32 @exit(i32 0)
unreachable
}