#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 */
typedef int /*<<< orphan*/ request;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDWR;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, size;
  char buffer[1024], request[1024];
  if (argc < 2) {
    printf("Usage: %s HOSTNAME\n", argv[0]);
    return EXIT_FAILURE;
  }
  fd = open("/net/dns", O_RDWR);
  if (fd < 0) {
    perror("open");
    return EXIT_FAILURE;
  }
  snprintf(request, sizeof(request), "resolve %s", argv[1]);
  size = write(fd, request, strlen(request));
  if (size < 0) {
    perror("write");
    return EXIT_FAILURE;
  }
  for (;;) {
    size = read(fd, buffer, sizeof(buffer));
    if (size <= 0)
      break;
    write(STDOUT_FILENO, buffer, size);
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c7 <main+0x47>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe4f(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e3 <main+0x163>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1206 <main+0x86>
lea    0xe2d(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e3 <main+0x163>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x400,%esi
lea    0xe01(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x820(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1287 <main+0x107>
lea    0xdbc(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12e3 <main+0x163>
jmp    128c <main+0x10c>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     12b2 <main+0x132>
jmp    12cf <main+0x14f>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
jmp    128c <main+0x10c>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11ae <main+0x2e>
mov    (%rbx),%rsi
lea    0xe63(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e27(%rip),%rax
jmp    125e <main+0xde>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe56(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     126c <main+0xec>
mov    %eax,%ebp
mov    0x8(%rbx),%rcx
lea    0xe4a(%rip),%rdx
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1275 <main+0xf5>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1250 <main+0xd0>
mov    0x2dd1(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1230 <main+0xb0>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdaa(%rip),%rdi
jmp    127c <main+0xfc>
lea    0xdb1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d48(%rip),%rax
jmp    125e <main+0xde>

LLVM IR (-O0):
@.str = [20 x i8] c"Usage: %s HOSTNAME\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/net/dns\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [11 x i8] c"resolve %s\00"
@.str.4 = [6 x i8] c"write\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = 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 [1024 x i8]
%9 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %3
br label %59
18: ; preds = %2
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
store i32 %25, i32* %3
br label %59
26: ; preds = %18
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @snprintf(i8* %27, i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %6
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @write(i32 %32, i8* %33, i32 %35)
store i32 %36, i32* %7
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %26
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
store i32 %41, i32* %3
br label %59
42: ; preds = %26
br label %43
43: ; preds = %50, %42
%44 = load i32, i32* %6
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%46 = call i32 @read(i32 %44, i8* %45, i32 1024)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = icmp sle i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
br label %55
50: ; preds = %43
%51 = load i32, i32* @STDOUT_FILENO
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%53 = load i32, i32* %7
%54 = call i32 @write(i32 %51, i8* %52, i32 %53)
br label %43
55: ; preds = %49
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* @EXIT_SUCCESS
store i32 %58, i32* %3
br label %59
59: ; preds = %55, %39, %23, %12
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [20 x i8] c"Usage: %s HOSTNAME\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [9 x i8] c"/net/dns\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [11 x i8] c"resolve %s\00"
@.str.4 = [6 x i8] c"write\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %37
11: ; preds = %2
%12 = load i32, i32* @O_RDWR
%13 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %37
17: ; preds = %11
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %19)
%21 = call i32 @strlen(i8* %6)
%22 = call i32 @write(i32 %13, i8* %6, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %17
%25 = call i32 @read(i32 %13, i8* %5, i32 1024)
%26 = icmp slt i32 %25, 1
br i1 %26, label %35, label %29
27: ; preds = %17
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %37
29: ; preds = %24, %29
%30 = phi i32 [ %33, %29 ], [ %25, %24 ]
%31 = load i32, i32* @STDOUT_FILENO
%32 = call i32 @write(i32 %31, i8* %5, i32 %30)
%33 = call i32 @read(i32 %13, i8* %5, i32 1024)
%34 = icmp slt i32 %33, 1
br i1 %34, label %35, label %29
35: ; preds = %29, %24
%36 = call i32 @close(i32 %13)
br label %37
37: ; preds = %35, %27, %15, %8
%38 = phi i32* [ @EXIT_FAILURE, %8 ], [ @EXIT_FAILURE, %15 ], [ @EXIT_FAILURE, %27 ], [ @EXIT_SUCCESS, %35 ]
%39 = load i32, i32* %38
ret i32 %39
}