#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 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ get_address(char *, char *, struct sockaddr *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int strnlen(char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  if (argc != 3 || strnlen(argv[1], 50) == 50) {
    printf("Curl called but HOST or PORT not given or invalid\n");
    return 1;
  }
  struct sockaddr addr;
  int sock;
  if (strnlen(argv[2], 6) == 6) {
    printf("Curl called but PORT malformed\n");
    return 1;
  }
  if (get_address(argv[1], argv[2], &addr) != 0) {
    printf("Curl could not resolve hostname\n");
    return 1;
  }
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(sock, &addr, 16) == -1) {
    perror("Curl could not establish connection");
    return 1;
  }
  char str[512];
  snprintf(str, 512,
           "GET / HTTP/1.1\r\nHost: %s:%s\r\nConnection: close\r\n\r\n",
           argv[1], argv[2]);
  int len = strlen(str);
  if (write(sock, str, len) != len) {
    printf("Write error\n");
    return 1;
  }
  char buf[4096] = {0};
  int rlen = 0;
  while ((rlen = read(sock, buf, 4096)) > 0) {
    printf("%s", buf);
  }
  if (rlen == -1) {
    perror("Curl read error");
    return 1;
  }
  close(sock);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11fe <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    $0x32,%esi
call   1090 <strnlen@plt>
cmp    $0x32,%eax
jne    1218 <main+0x58>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x214>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    $0x6,%esi
call   1090 <strnlen@plt>
cmp    $0x6,%eax
jne    124d <main+0x8d>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x214>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x18(%rbp),%rdx
call   1030 <get_address@plt>
cmp    $0x0,%rax
je     128a <main+0xca>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x214>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12d8 <main+0x118>
lea    0xdad(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x214>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%r8
mov    $0x200,%esi
lea    0xd9d(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x220(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x224(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1040 <write@plt>
cmp    -0x224(%rbp),%eax
je     134d <main+0x18d>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x214>
lea    -0x1230(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
movl   $0x0,-0x1234(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x1230(%rbp),%rsi
mov    $0x1000,%edx
call   10b0 <read@plt>
mov    %eax,-0x1234(%rbp)
cmp    $0x0,%eax
jle    13a7 <main+0x1e7>
lea    -0x1230(%rbp),%rsi
lea    0xd3d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    136a <main+0x1aa>
cmpl   $0xffffffff,-0x1234(%rbp)
jne    13cc <main+0x20c>
lea    0xd20(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x214>
mov    -0x1c(%rbp),%edi
call   10a0 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x1240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1210,%rsp
cmp    $0x3,%edi
jne    11f6 <main+0x26>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    $0x32,%esi
call   10a0 <strnlen@plt>
cmp    $0x32,%eax
jne    1213 <main+0x43>
lea    0xeb8(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x1210,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x10(%rbx),%rdi
mov    $0x6,%esi
call   10a0 <strnlen@plt>
cmp    $0x6,%eax
jne    122f <main+0x5f>
lea    0xe69(%rip),%rdi
jmp    11fd <main+0x2d>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
lea    0x8(%rsp),%rdx
call   1030 <get_address@plt>
test   %rax,%rax
je     124f <main+0x7f>
lea    0xe29(%rip),%rdi
jmp    11fd <main+0x2d>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     133f <main+0x16f>
mov    0x8(%rbx),%rcx
mov    0x10(%rbx),%r8
lea    0xd91(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x200,%esi
call   1080 <snprintf@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
cmp    %ebx,%eax
jne    1333 <main+0x163>
lea    0x210(%rsp),%rbx
mov    $0x1000,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10c0 <read@plt>
test   %eax,%eax
jle    1320 <main+0x150>
lea    0xd67(%rip),%r14
lea    0x210(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   10c0 <read@plt>
test   %eax,%eax
jg     1300 <main+0x130>
cmp    $0xffffffff,%eax
je     1348 <main+0x178>
mov    %ebp,%edi
call   10b0 <close@plt>
xor    %eax,%eax
jmp    1207 <main+0x37>
lea    0xd30(%rip),%rdi
jmp    11fd <main+0x2d>
lea    0xcba(%rip),%rdi
jmp    134f <main+0x17f>
lea    0xd0b(%rip),%rdi
call   10d0 <perror@plt>
jmp    1202 <main+0x32>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [51 x i8] c"Curl called but HOST or PORT not given or invalid\0A\00"
@.str.1 = [32 x i8] c"Curl called but PORT malformed\0A\00"
@.str.2 = [33 x i8] c"Curl could not resolve hostname\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [36 x i8] c"Curl could not establish connection\00"
@.str.4 = [51 x i8] c"GET / HTTP/1.1\0D\0AHost: %s:%s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.5 = [13 x i8] c"Write error\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [16 x i8] c"Curl read error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca [4096 x i8]
%11 = alloca i32
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, 3
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strnlen(i8* %17, i32 50)
%19 = icmp eq i32 %18, 50
br i1 %19, label %20, label %22
20: ; preds = %14, %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %87
22: ; preds = %14
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @strnlen(i8* %25, i32 6)
%27 = icmp eq i32 %26, 6
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %87
30: ; preds = %22
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i64 @get_address(i8* %33, i8* %36, %struct.sockaddr* %6)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %87
41: ; preds = %30
%42 = load i32, i32* @AF_INET
%43 = load i32, i32* @SOCK_STREAM
%44 = call i32 @socket(i32 %42, i32 %43, i32 0)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = call i32 @connect(i32 %45, %struct.sockaddr* %6, i32 16)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %87
50: ; preds = %41
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 1
%54 = load i8*, i8** %53
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @snprintf(i8* %51, i32 512, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i8* %54, i8* %57)
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%60 = call i32 @strlen(i8* %59)
store i32 %60, i32* %9
%61 = load i32, i32* %7
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%63 = load i32, i32* %9
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
%65 = load i32, i32* %9
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %69
67: ; preds = %50
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %87
69: ; preds = %50
%70 = bitcast [4096 x i8]* %10 to i8*
store i32 0, i32* %11
br label %71
71: ; preds = %76, %69
%72 = load i32, i32* %7
%73 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%74 = call i32 @read(i32 %72, i8* %73, i32 4096)
store i32 %74, i32* %11
%75 = icmp sgt i32 %74, 0
br i1 %75, label %76, label %79
76: ; preds = %71
%77 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %77)
br label %71
79: ; preds = %71
%80 = load i32, i32* %11
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %87
84: ; preds = %79
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %84, %82, %67, %48, %39, %28, %20
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [36 x i8] c"Curl could not establish connection\00"
@.str.4 = [51 x i8] c"GET / HTTP/1.1\0D\0AHost: %s:%s\0D\0AConnection: close\0D\0A\0D\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [16 x i8] c"Curl read error\00"
@str = [12 x i8] c"Write error\00"
@str.8 = [32 x i8] c"Curl could not resolve hostname\00"
@str.9 = [31 x i8] c"Curl called but PORT malformed\00"
@str.10 = [50 x i8] c"Curl called but HOST or PORT not given or invalid\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr
%4 = alloca [512 x i8]
%5 = alloca [4096 x i8]
%6 = icmp eq i32 %0, 3
br i1 %6, label %7, label %12
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strnlen(i8* %9, i32 50)
%11 = icmp eq i32 %10, 50
br i1 %11, label %12, label %14
12: ; preds = %7, %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.10, i64 0, i64 0))
br label %68
14: ; preds = %7
%15 = bitcast %struct.sockaddr* %3 to i8*
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @strnlen(i8* %17, i32 6)
%19 = icmp eq i32 %18, 6
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.9, i64 0, i64 0))
br label %66
22: ; preds = %14
%23 = load i8*, i8** %8
%24 = load i8*, i8** %16
%25 = call i64 @get_address(i8* %23, i8* %24, %struct.sockaddr* %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %22
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.8, i64 0, i64 0))
br label %66
29: ; preds = %22
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = call i32 @connect(i32 %32, %struct.sockaddr* %3, i32 16)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0))
br label %66
37: ; preds = %29
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%39 = load i8*, i8** %8
%40 = load i8*, i8** %16
%41 = call i32 @snprintf(i8* %38, i32 512, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i8* %39, i8* %40)
%42 = call i32 @strlen(i8* %38)
%43 = call i32 @write(i32 %32, i8* %38, i32 %42)
%44 = icmp eq i32 %43, %42
br i1 %44, label %47, label %45
45: ; preds = %37
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %64
47: ; preds = %37
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%49 = call i32 @read(i32 %32, i8* %48, i32 4096)
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47, %51
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %48)
%53 = call i32 @read(i32 %32, i8* %48, i32 4096)
%54 = icmp sgt i32 %53, 0
br i1 %54, label %51, label %55
55: ; preds = %51, %47
%56 = phi i32 [ %49, %47 ], [ %53, %51 ]
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
br label %62
60: ; preds = %55
%61 = call i32 @close(i32 %32)
br label %62
62: ; preds = %60, %58
%63 = phi i32 [ 1, %58 ], [ 0, %60 ]
br label %64
64: ; preds = %62, %45
%65 = phi i32 [ 1, %45 ], [ %63, %62 ]
br label %66
66: ; preds = %64, %35, %27, %20
%67 = phi i32 [ 1, %20 ], [ 1, %27 ], [ 1, %35 ], [ %65, %64 ]
br label %68
68: ; preds = %66, %12
%69 = phi i32 [ 1, %12 ], [ %67, %66 ]
ret i32 %69
}