#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 dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  scalar_t__ ai_flags;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int sd;
  FILE *fp;
  int err;
  int len;
  struct sockaddr_in raddr;
  long long stamp;
  char rbuf[1024];
  struct addrinfo ai, *res;
  if (argc < 2) {
    fprintf(stderr, "Usage...\n");
    exit(1);
  }
  ai.ai_flags = 0;
  ai.ai_family = AF_INET;
  ai.ai_socktype = SOCK_STREAM;
  ai.ai_protocol = 0;
  err = getaddrinfo(argv[1], "http", &ai, &res);
  if (err) {
    fprintf(stderr, "getaddrinfo():%s\n", gai_strerror(err));
    exit(1);
  }
  sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (sd < 0) {
    perror("socket()");
    exit(1);
  }
  if (connect(sd, res->ai_addr, res->ai_addrlen) < 0) {
    perror("connect()");
    exit(1);
  }
  fp = fdopen(sd, "r+");
  if (fp == NULL) {
    perror("fdopen()");
    exit(1);
  }
  fprintf(fp, "GET /test.jpg\r\n\r\n");
  fflush(fp);
  while (1) {
    len = fread(rbuf, 1, 1024, fp);
    if (len <= 0)
      break;
    fwrite(rbuf, 1, len, stdout);
  }
  fclose(fp);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1205 <main+0x45>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdi
lea    0xe0c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movq   $0x0,-0x448(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44c(%rbp)
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x450(%rbp)
movq   $0x0,-0x458(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdc2(%rip),%rsi
lea    -0x460(%rbp),%rdx
lea    -0x468(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12a3 <main+0xe3>
mov    0x2d69(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x470(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x470(%rbp),%rdi
mov    %rax,%rdx
lea    0xd7d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x468(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x468(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x468(%rbp),%rax
mov    0x8(%rax),%rdx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12ea <main+0x12a>
lea    0xd46(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x468(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x468(%rbp),%rax
mov    (%rax),%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1325 <main+0x165>
lea    0xd14(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    0xd05(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1359 <main+0x199>
lea    0xced(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xcdc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fflush@plt>
lea    -0x440(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     13a0 <main+0x1e0>
jmp    13c2 <main+0x202>
lea    -0x440(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    0x2c17(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
jmp    1374 <main+0x1b4>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x430,%rsp
cmp    $0x1,%edi
jg     11fb <main+0x3b>
mov    0x2e00(%rip),%rax
mov    (%rax),%rcx
lea    0xe1e(%rip),%rdi
mov    $0x9,%esi
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movq   $0x0,0x28(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x8(%rsi),%rdi
lea    0xdd8(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
jne    1266 <main+0xa6>
mov    0x8(%rsp),%rax
mov    0x14(%rax),%edi
mov    0x10(%rax),%esi
mov    0x8(%rax),%rdx
call   10e0 <socket@plt>
test   %eax,%eax
jns    1295 <main+0xd5>
lea    0xdbd(%rip),%rdi
jmp    12d0 <main+0x110>
mov    0x2d6b(%rip),%rcx
mov    (%rcx),%rbx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd91(%rip),%rsi
mov    %rbx,%rdi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   10b0 <connect@plt>
test   %rax,%rax
jns    12b6 <main+0xf6>
lea    0xd76(%rip),%rdi
jmp    12d0 <main+0x110>
lea    0xd77(%rip),%rsi
mov    %ebx,%edi
call   1080 <fdopen@plt>
test   %rax,%rax
jne    12df <main+0x11f>
lea    0xd67(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%r14
lea    0xd57(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
mov    %r14,%rdi
call   1070 <fflush@plt>
lea    0x30(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    135b <main+0x19b>
mov    0x2ca3(%rip),%r15
lea    0x30(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r15),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     1330 <main+0x170>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i32, i64, i32, i32, i64 }
@stderr = global i32* null
@.str = [10 x i8] c"Usage...\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [18 x i8] c"getaddrinfo():%s\0A\00"
@.str.3 = [9 x i8] c"socket()\00"
@.str.4 = [10 x i8] c"connect()\00"
@.str.5 = [3 x i8] c"r+\00"
@.str.6 = [9 x i8] c"fdopen()\00"
@.str.7 = [18 x i8] c"GET /test.jpg\0D\0A\0D\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i64
%12 = alloca [1024 x i8]
%13 = alloca %struct.addrinfo
%14 = alloca %struct.addrinfo*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32*, i32** @stderr
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 5
store i64 0, i64* %22
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 4
store i32 %23, i32* %24
%25 = load i32, i32* @SOCK_STREAM
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 3
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 2
store i64 0, i64* %27
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @getaddrinfo(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %13, %struct.addrinfo** %14)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %21
%35 = load i32*, i32** @stderr
%36 = load i32, i32* %8
%37 = call i8* @gai_strerror(i32 %36)
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %21
%41 = load %struct.addrinfo*, %struct.addrinfo** %14
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 4
%43 = load i32, i32* %42
%44 = load %struct.addrinfo*, %struct.addrinfo** %14
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 3
%46 = load i32, i32* %45
%47 = load %struct.addrinfo*, %struct.addrinfo** %14
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 2
%49 = load i64, i64* %48
%50 = call i32 @socket(i32 %43, i32 %46, i64 %49)
store i32 %50, i32* %6
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %40
%54 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %40
%57 = load i32, i32* %6
%58 = load %struct.addrinfo*, %struct.addrinfo** %14
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 1
%60 = load i32, i32* %59
%61 = load %struct.addrinfo*, %struct.addrinfo** %14
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @connect(i32 %57, i32 %60, i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %56
%67 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %56
%70 = load i32, i32* %6
%71 = call i32* @fdopen(i32 %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %71, i32** %7
%72 = load i32*, i32** %7
%73 = icmp eq i32* %72, null
br i1 %73, label %74, label %77
74: ; preds = %69
%75 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %69
%78 = load i32*, i32** %7
%79 = call i32 (i32*, i8*, ...) @fprintf(i32* %78, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
%80 = load i32*, i32** %7
%81 = call i32 @fflush(i32* %80)
br label %82
82: ; preds = %77, %89
%83 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%84 = load i32*, i32** %7
%85 = call i32 @fread(i8* %83, i32 1, i32 1024, i32* %84)
store i32 %85, i32* %9
%86 = load i32, i32* %9
%87 = icmp sle i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %82
br label %94
89: ; preds = %82
%90 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%91 = load i32, i32* %9
%92 = load i32, i32* @stdout
%93 = call i32 @fwrite(i8* %90, i32 1, i32 %91, i32 %92)
br label %82
94: ; preds = %88
%95 = load i32*, i32** %7
%96 = call i32 @fclose(i32* %95)
%97 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32, i32, i64 }
@stderr = global i32* null
@.str = [10 x i8] c"Usage...\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [5 x i8] c"http\00"
@.str.2 = [18 x i8] c"getaddrinfo():%s\0A\00"
@.str.3 = [9 x i8] c"socket()\00"
@.str.4 = [10 x i8] c"connect()\00"
@.str.5 = [3 x i8] c"r+\00"
@.str.6 = [9 x i8] c"fdopen()\00"
@.str.7 = [18 x i8] c"GET /test.jpg\0D\0A\0D\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32*, i32** @stderr
%12 = tail call i64 bitcast (i32 (i8*, i32, i32, i32)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i64 9, i64 1, i32* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i64 0, i64* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i64 0, i64* %20
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @getaddrinfo(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
%24 = icmp eq i32 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %14
%26 = load i32*, i32** @stderr
%27 = call i8* @gai_strerror(i32 %23)
%28 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %14
%31 = load %struct.addrinfo*, %struct.addrinfo** %5
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 4
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 3
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 2
%37 = load i64, i64* %36
%38 = call i32 @socket(i32 %33, i32 %35, i64 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %30
%41 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %30
%44 = load %struct.addrinfo*, %struct.addrinfo** %5
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i64 0, i32 1
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i64 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @connect(i32 %38, i32 %46, i32 %48)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %43
%52 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %43
%55 = call i32* @fdopen(i32 %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %54
%61 = call i64 bitcast (i32 (i8*, i32, i32, i32)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0), i64 17, i64 1, i32* %55)
%62 = call i32 @fflush(i32* %55)
%63 = call i32 @fread(i8* %6, i32 1, i32 1024, i32* %55)
%64 = icmp slt i32 %63, 1
br i1 %64, label %71, label %65
65: ; preds = %60, %65
%66 = phi i32 [ %69, %65 ], [ %63, %60 ]
%67 = load i32, i32* @stdout
%68 = call i32 @fwrite(i8* %6, i32 1, i32 %66, i32 %67)
%69 = call i32 @fread(i8* %6, i32 1, i32 1024, i32* %55)
%70 = icmp slt i32 %69, 1
br i1 %70, label %71, label %65
71: ; preds = %65, %60
%72 = call i32 @fclose(i32* %55)
%73 = call i32 @exit(i32 0)
unreachable
}