#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ parse(unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
unsigned char *realloc(unsigned char *, int);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strlen(char *);
int retrieve_random_bytes(void) {
  int size = 0;
  char ans[256], cmd[1024];
  unsigned char *result = NULL;
  int read_bytes = 0;
  int sock;
  struct sockaddr_in dest;
  struct hostent *he;
  char *hostname = "www.random.org";
  char *file = "/integers/?";
  char *options = "num=1024&min=0&max=65535&col=4&base=10&format=plain&md=new";
  if ((he = gethostbyname(hostname)) == NULL) {
    printf("[-] Could not resolve %s\n", hostname);
    return 1;
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "%s: socket() failed.\n", "GUChaos");
    return 1;
  }
  dest.sin_family = AF_INET;
  dest.sin_port = htons(80);
  dest.sin_addr = *((struct in_addr *)he->h_addr);
  if (connect(sock, (struct sockaddr *)&dest, sizeof(struct sockaddr)) < 0) {
    printf("[-] Connect() failed.\n");
    close(sock);
    return 1;
  }
  sprintf(cmd, "GET %s%s HTTP/1.1\nHost: www.random.org\r\n\r\n", file,
          options);
  send(sock, cmd, strlen(cmd), 0);
  while ((read_bytes = read(sock, ans, 256 - 1))) {
    ans[read_bytes] = '\0';
    size += read_bytes;
    result = realloc(result, size);
    strcat((char *)result, ans);
  }
  parse(result);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<retrieve_random_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x560,%rsp
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x518(%rbp)
movl   $0x0,-0x51c(%rbp)
lea    0xdf2(%rip),%rax
mov    %rax,-0x540(%rbp)
lea    0xdf3(%rip),%rax
mov    %rax,-0x548(%rbp)
lea    0xdf1(%rip),%rax
mov    %rax,-0x550(%rbp)
mov    -0x540(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x538(%rbp)
cmp    $0x0,%rax
jne    126f <retrieve_random_bytes+0x8f>
mov    -0x540(%rbp),%rsi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1420 <retrieve_random_bytes+0x240>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x520(%rbp)
cmp    $0x0,%eax
jge    12bf <retrieve_random_bytes+0xdf>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xdc9(%rip),%rsi
lea    0xdd8(%rip),%rdx
call   10a0 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1420 <retrieve_random_bytes+0x240>
mov    0x2d22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x528(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x52c(%rbp)
mov    -0x538(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x530(%rbp)
mov    -0x520(%rbp),%edi
lea    -0x530(%rbp),%rsi
mov    $0x4,%edx
call   10f0 <connect@plt>
cmp    $0x0,%rax
jge    1336 <retrieve_random_bytes+0x156>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x520(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1420 <retrieve_random_bytes+0x240>
lea    -0x510(%rbp),%rdi
mov    -0x548(%rbp),%rdx
mov    -0x550(%rbp),%rcx
lea    0xd54(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x520(%rbp),%eax
mov    %eax,-0x55c(%rbp)
lea    -0x510(%rbp),%rax
mov    %rax,-0x558(%rbp)
lea    -0x510(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x55c(%rbp),%edi
mov    -0x558(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0x520(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0x51c(%rbp)
cmp    $0x0,%eax
je     1402 <retrieve_random_bytes+0x222>
movslq -0x51c(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x51c(%rbp),%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x518(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   10b0 <realloc@plt>
mov    %rax,-0x518(%rbp)
mov    -0x518(%rbp),%rdi
lea    -0x110(%rbp),%rsi
call   10c0 <strcat@plt>
jmp    1393 <retrieve_random_bytes+0x1b3>
mov    -0x518(%rbp),%rdi
call   10d0 <parse@plt>
mov    -0x520(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x560,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<retrieve_random_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x518,%rsp
lea    0xdfc(%rip),%rdi
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     1307 <retrieve_random_bytes+0x117>
mov    %rax,%rbx
mov    0x2dcc(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     1323 <retrieve_random_bytes+0x133>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   1100 <connect@plt>
test   %rax,%rax
js     1346 <retrieve_random_bytes+0x156>
lea    0xe17(%rip),%rsi
lea    0xd91(%rip),%rdx
lea    0xd96(%rip),%rcx
lea    0x110(%rsp),%rbp
mov    %rbp,%rdi
call   10f0 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xff,%edx
call   1090 <read@plt>
test   %eax,%eax
je     1361 <retrieve_random_bytes+0x171>
lea    0x10(%rsp),%r15
xor    %ebp,%ebp
nop
cltq
movb   $0x0,0x10(%rsp,%rax,1)
add    %eax,%ebx
mov    %rbp,%rdi
mov    %ebx,%esi
call   10c0 <realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
call   10d0 <strcat@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xff,%edx
call   1090 <read@plt>
test   %eax,%eax
jne    12d0 <retrieve_random_bytes+0xe0>
jmp    1363 <retrieve_random_bytes+0x173>
lea    0xd48(%rip),%rdi
lea    0xceb(%rip),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
jmp    1375 <retrieve_random_bytes+0x185>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
lea    0xd3d(%rip),%rsi
lea    0xd4c(%rip),%rdx
call   10b0 <fprintf@plt>
mov    $0x1,%eax
jmp    1375 <retrieve_random_bytes+0x185>
lea    0xd6c(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0x1,%eax
jmp    1375 <retrieve_random_bytes+0x185>
xor    %ebp,%ebp
mov    %rbp,%rdi
call   10e0 <parse@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x518,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"www.random.org\00"
@.str.1 = [12 x i8] c"/integers/?\00"
@.str.2 = [59 x i8] c"num=1024&min=0&max=65535&col=4&base=10&format=plain&md=new\00"
@.str.3 = [26 x i8] c"[-] Could not resolve %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.4 = [22 x i8] c"%s: socket() failed.\0A\00"
@.str.5 = [8 x i8] c"GUChaos\00"
@.str.6 = [24 x i8] c"[-]\C2\A0Connect() failed.\0A\00"
@.str.7 = [43 x i8] c"GET %s%s HTTP/1.1\0AHost: www.random.org\0D\0A\0D\0A\00"
define i32 @retrieve_random_bytes() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %2
store i8* null, i8** %5
store i32 0, i32* %6
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %10
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8** %11
store i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8** %12
%13 = load i8*, i8** %10
%14 = call %struct.hostent* @gethostbyname(i8* %13)
store %struct.hostent* %14, %struct.hostent** %9
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load i8*, i8** %10
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %17)
store i32 1, i32* %1
br label %80
19: ; preds = %0
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %7
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %80
27: ; preds = %19
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %28, i32* %29
%30 = call i32 @htons(i32 80)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %30, i32* %31
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%33 = load %struct.hostent*, %struct.hostent** %9
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to %struct.in_addr*
%37 = bitcast %struct.in_addr* %32 to i8*
%38 = bitcast %struct.in_addr* %36 to i8*
%39 = load i32, i32* %7
%40 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%41 = call i64 @connect(i32 %39, %struct.sockaddr* %40, i32 4)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %27
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
store i32 1, i32* %1
br label %80
47: ; preds = %27
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%49 = load i8*, i8** %11
%50 = load i8*, i8** %12
%51 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %49, i8* %50)
%52 = load i32, i32* %7
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%55 = call i32 @strlen(i8* %54)
%56 = call i32 @send(i32 %52, i8* %53, i32 %55, i32 0)
br label %57
57: ; preds = %62, %47
%58 = load i32, i32* %7
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 255)
store i32 %60, i32* %6
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %75
62: ; preds = %57
%63 = load i32, i32* %6
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %64
store i8 0, i8* %65
%66 = load i32, i32* %6
%67 = load i32, i32* %2
%68 = add nsw i32 %67, %66
store i32 %68, i32* %2
%69 = load i8*, i8** %5
%70 = load i32, i32* %2
%71 = call i8* @realloc(i8* %69, i32 %70)
store i8* %71, i8** %5
%72 = load i8*, i8** %5
%73 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%74 = call i32 @strcat(i8* %72, i8* %73)
br label %57
75: ; preds = %57
%76 = load i8*, i8** %5
%77 = call i32 @parse(i8* %76)
%78 = load i32, i32* %7
%79 = call i32 @close(i32 %78)
store i32 0, i32* %1
br label %80
80: ; preds = %75, %43, %24, %16
%81 = load i32, i32* %1
ret i32 %81
}

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 = [15 x i8] c"www.random.org\00"
@.str.1 = [12 x i8] c"/integers/?\00"
@.str.2 = [59 x i8] c"num=1024&min=0&max=65535&col=4&base=10&format=plain&md=new\00"
@.str.3 = [26 x i8] c"[-] Could not resolve %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.4 = [22 x i8] c"%s: socket() failed.\0A\00"
@.str.5 = [8 x i8] c"GUChaos\00"
@.str.7 = [43 x i8] c"GET %s%s HTTP/1.1\0AHost: www.random.org\0D\0A\0D\0A\00"
@str = [23 x i8] c"[-]\C2\A0Connect() failed.\00"
define i32 @retrieve_random_bytes() {
%1 = alloca [256 x i8]
%2 = alloca [1024 x i8]
%3 = alloca %struct.sockaddr_in
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %56
11: ; preds = %0
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %56
19: ; preds = %11
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 80)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to i32*
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%28 = load i32, i32* %26
store i32 %28, i32* %27
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @connect(i32 %14, %struct.sockaddr* %29, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %19
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%34 = call i32 @close(i32 %14)
br label %56
35: ; preds = %19
%36 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @strlen(i8* %5)
%38 = call i32 @send(i32 %14, i8* %5, i32 %37, i32 0)
%39 = call i32 @read(i32 %14, i8* %4, i32 255)
%40 = icmp eq i32 %39, 0
br i1 %40, label %52, label %41
41: ; preds = %35, %41
%42 = phi i32 [ %50, %41 ], [ %39, %35 ]
%43 = phi i32 [ %47, %41 ], [ 0, %35 ]
%44 = phi i8* [ %48, %41 ], [ null, %35 ]
%45 = sext i32 %42 to i64
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %45
store i8 0, i8* %46
%47 = add nsw i32 %42, %43
%48 = call i8* @realloc(i8* %44, i32 %47)
%49 = call i32 @strcat(i8* %48, i8* %4)
%50 = call i32 @read(i32 %14, i8* %4, i32 255)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %41
52: ; preds = %41, %35
%53 = phi i8* [ null, %35 ], [ %48, %41 ]
%54 = call i32 @parse(i8* %53)
%55 = call i32 @close(i32 %14)
br label %56
56: ; preds = %52, %32, %16, %9
%57 = phi i32 [ 1, %9 ], [ 1, %16 ], [ 1, %32 ], [ 0, %52 ]
ret i32 %57
}