#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int inet_addr(char *);
int /*<<< orphan*/ isspace(char);
int /*<<< orphan*/ perror(char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int get_dns_ip(char *buf, size_t n) {
  FILE *fp;
  char line[512];
  char *pos, *p;
  fp = NULL;
  pos = NULL;
  fp = fopen("/etc/resolv.conf", "r");
  if (!fp) {
    perror("fopen /etc/resolv.conf");
    return -1;
  }
  while (fgets(line, sizeof(line), fp)) {
    if (line[0] == '#')
      continue;
    if ((pos = strchr(line, '\n')))
      *pos = '\0';
    p = line;
    while (*p && !isspace(*p))
      p++;
    if (!isspace(*p))
      continue;
    *p = '\0';
    p++;
    if (!strcmp(line, "nameserver")) {
      if (!strcmp(p, "127.0.0.1"))
        continue;
    } else if (!strcmp(line, "search")) {
      if (inet_addr(p) == -1)
        continue;
    } else {
      continue;
    }
    strncpy(buf, p, n);
    if (n > 0)
      buf[n - 1] = '\0';
    fclose(fp);
    return 0;
  }
  fclose(fp);
  return -1;
}


x86 assembly (clang, -O0):
<get_dns_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x228(%rbp)
lea    0xe49(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f0 <get_dns_ip+0x60>
lea    0xe21(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13d9 <get_dns_ip+0x249>
jmp    11f5 <get_dns_ip+0x65>
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     13c9 <get_dns_ip+0x239>
movsbl -0x220(%rbp),%eax
cmp    $0x23,%eax
jne    1229 <get_dns_ip+0x99>
jmp    11f5 <get_dns_ip+0x65>
lea    -0x220(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x228(%rbp)
cmp    $0x0,%rax
je     1255 <get_dns_ip+0xc5>
mov    -0x228(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    -0x230(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x231(%rbp)
je     129b <get_dns_ip+0x10b>
mov    -0x230(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x231(%rbp)
mov    -0x231(%rbp),%al
test   $0x1,%al
jne    12ae <get_dns_ip+0x11e>
jmp    12c5 <get_dns_ip+0x135>
mov    -0x230(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x230(%rbp)
jmp    1263 <get_dns_ip+0xd3>
mov    -0x230(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isspace@plt>
cmp    $0x0,%eax
jne    12e2 <get_dns_ip+0x152>
jmp    11f5 <get_dns_ip+0x65>
mov    -0x230(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x230(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x230(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xd0b(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    1340 <get_dns_ip+0x1b0>
mov    -0x230(%rbp),%rdi
lea    0xcfa(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    133b <get_dns_ip+0x1ab>
jmp    11f5 <get_dns_ip+0x65>
jmp    1385 <get_dns_ip+0x1f5>
lea    -0x220(%rbp),%rdi
lea    0xcde(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    137b <get_dns_ip+0x1eb>
mov    -0x230(%rbp),%rdi
call   1080 <inet_addr@plt>
cmp    $0xffffffff,%eax
jne    1376 <get_dns_ip+0x1e6>
jmp    11f5 <get_dns_ip+0x65>
jmp    1380 <get_dns_ip+0x1f0>
jmp    11f5 <get_dns_ip+0x65>
jmp    1385 <get_dns_ip+0x1f5>
mov    -0x10(%rbp),%rdi
mov    -0x230(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x18(%rbp)
jbe    13b4 <get_dns_ip+0x224>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13d9 <get_dns_ip+0x249>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dns_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0xe58(%rip),%rdi
lea    0xe6b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1346 <get_dns_ip+0x1b6>
mov    %rax,%rbx
mov    %rbp,(%rsp)
mov    %r14,0x8(%rsp)
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1327 <get_dns_ip+0x197>
movabs $0x76726573656d616e,%r14
65 72 76
movabs $0x72657672657365,%r15
jmp    1239 <get_dns_ip+0xa9>
mov    %rbp,%rdi
lea    0xe15(%rip),%rsi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    12f9 <get_dns_ip+0x169>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1327 <get_dns_ip+0x197>
mov    0x10(%rsp),%r13b
cmp    $0x23,%r13b
je     1220 <get_dns_ip+0x90>
mov    %r12,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     125e <get_dns_ip+0xce>
movb   $0x0,(%rax)
mov    0x10(%rsp),%r13b
test   %r13b,%r13b
je     128f <get_dns_ip+0xff>
mov    %r12,%rbp
cs nopw 0x0(%rax,%rax,1)
movsbl %r13b,%edi
call   1060 <isspace@plt>
test   %eax,%eax
jne    1296 <get_dns_ip+0x106>
movzbl 0x1(%rbp),%r13d
add    $0x1,%rbp
test   %r13b,%r13b
jne    1270 <get_dns_ip+0xe0>
xor    %eax,%eax
jmp    1299 <get_dns_ip+0x109>
xor    %eax,%eax
mov    %r12,%rbp
jmp    1299 <get_dns_ip+0x109>
mov    0x0(%rbp),%al
movsbl %al,%edi
call   1060 <isspace@plt>
test   %eax,%eax
je     1220 <get_dns_ip+0x90>
movb   $0x0,0x0(%rbp)
add    $0x1,%rbp
mov    0x10(%rsp),%rax
xor    %r14,%rax
mov    0x13(%rsp),%rcx
xor    %r15,%rcx
or     %rax,%rcx
je     1203 <get_dns_ip+0x73>
mov    0x10(%rsp),%eax
mov    $0x72616573,%ecx
xor    %ecx,%eax
mov    0x13(%rsp),%ecx
mov    $0x686372,%edx
xor    %edx,%ecx
or     %eax,%ecx
jne    1220 <get_dns_ip+0x90>
mov    %rbp,%rdi
call   1080 <inet_addr@plt>
cmp    $0xffffffff,%eax
je     1220 <get_dns_ip+0x90>
mov    (%rsp),%r14
mov    %r14,%rdi
mov    %rbp,%rsi
mov    0x8(%rsp),%rbp
mov    %rbp,%rdx
call   1030 <strncpy@plt>
test   %rbp,%rbp
je     131b <get_dns_ip+0x18b>
movb   $0x0,-0x1(%rbp,%r14,1)
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1334 <get_dns_ip+0x1a4>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb3(%rip),%rdi
call   10b0 <perror@plt>
jmp    132f <get_dns_ip+0x19f>

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"fopen /etc/resolv.conf\00"
@.str.3 = [11 x i8] c"nameserver\00"
@.str.4 = [10 x i8] c"127.0.0.1\00"
@.str.5 = [7 x i8] c"search\00"
define i32 @get_dns_ip(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32* null, i32** %6
store i8* null, i8** %8
%10 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp ne i32* %11, null
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %100
15: ; preds = %2
br label %16
16: ; preds = %80, %78, %68, %56, %26, %15
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%18 = load i32*, i32** %6
%19 = call i64 @fgets(i8* %17, i32 512, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %97
21: ; preds = %16
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 35
br i1 %25, label %26, label %27
26: ; preds = %21
br label %16
27: ; preds = %21
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%29 = call i8* @strchr(i8* %28, i8 signext 10)
store i8* %29, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = load i8*, i8** %8
store i8 0, i8* %32
br label %33
33: ; preds = %31, %27
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
store i8* %34, i8** %9
br label %35
35: ; preds = %48, %33
%36 = load i8*, i8** %9
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i8*, i8** %9
%42 = load i8, i8* %41
%43 = call i32 @isspace(i8 signext %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br label %46
46: ; preds = %40, %35
%47 = phi i1 [ false, %35 ], [ %45, %40 ]
br i1 %47, label %48, label %51
48: ; preds = %46
%49 = load i8*, i8** %9
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %9
br label %35
51: ; preds = %46
%52 = load i8*, i8** %9
%53 = load i8, i8* %52
%54 = call i32 @isspace(i8 signext %53)
%55 = icmp ne i32 %54, 0
br i1 %55, label %57, label %56
56: ; preds = %51
br label %16
57: ; preds = %51
%58 = load i8*, i8** %9
store i8 0, i8* %58
%59 = load i8*, i8** %9
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %9
%61 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%62 = call i32 @strcmp(i8* %61, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%63 = icmp ne i32 %62, 0
br i1 %63, label %70, label %64
64: ; preds = %57
%65 = load i8*, i8** %9
%66 = call i32 @strcmp(i8* %65, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%67 = icmp ne i32 %66, 0
br i1 %67, label %69, label %68
68: ; preds = %64
br label %16
69: ; preds = %64
br label %82
70: ; preds = %57
%71 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%72 = call i32 @strcmp(i8* %71, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%73 = icmp ne i32 %72, 0
br i1 %73, label %80, label %74
74: ; preds = %70
%75 = load i8*, i8** %9
%76 = call i32 @inet_addr(i8* %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %79
78: ; preds = %74
br label %16
79: ; preds = %74
br label %81
80: ; preds = %70
br label %16
81: ; preds = %79
br label %82
82: ; preds = %81, %69
%83 = load i8*, i8** %4
%84 = load i8*, i8** %9
%85 = load i64, i64* %5
%86 = call i32 @strncpy(i8* %83, i8* %84, i64 %85)
%87 = load i64, i64* %5
%88 = icmp ugt i64 %87, 0
br i1 %88, label %89, label %94
89: ; preds = %82
%90 = load i8*, i8** %4
%91 = load i64, i64* %5
%92 = sub i64 %91, 1
%93 = getelementptr inbounds i8, i8* %90, i64 %92
store i8 0, i8* %93
br label %94
94: ; preds = %89, %82
%95 = load i32*, i32** %6
%96 = call i32 @fclose(i32* %95)
store i32 0, i32* %3
br label %100
97: ; preds = %16
%98 = load i32*, i32** %6
%99 = call i32 @fclose(i32* %98)
store i32 -1, i32* %3
br label %100
100: ; preds = %97, %94, %13
%101 = load i32, i32* %3
ret i32 %101
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"fopen /etc/resolv.conf\00"
@.str.3 = [11 x i8] c"nameserver\00"
@.str.4 = [10 x i8] c"127.0.0.1\00"
@.str.5 = [7 x i8] c"search\00"
define i32 @get_dns_ip(i8* %0, i64 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i64 @fgets(i8* %4, i32 512, i32* %5)
%9 = icmp eq i64 %8, 0
br i1 %9, label %63, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %65
12: ; preds = %7, %15
%13 = load i8, i8* %4
%14 = icmp eq i8 %13, 35
br i1 %14, label %15, label %18
15: ; preds = %12, %37, %46, %52, %49
%16 = call i64 @fgets(i8* %4, i32 512, i32* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %63, label %12
18: ; preds = %12
%19 = call i8* @strchr(i8* %4, i8 signext 10)
%20 = icmp eq i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
store i8 0, i8* %19
%22 = load i8, i8* %4
br label %23
23: ; preds = %21, %18
%24 = phi i8 [ %22, %21 ], [ %13, %18 ]
%25 = icmp eq i8 %24, 0
br i1 %25, label %37, label %26
26: ; preds = %23, %33
%27 = phi i8 [ %35, %33 ], [ %24, %23 ]
%28 = phi i8* [ %34, %33 ], [ %4, %23 ]
%29 = call i32 @isspace(i8 signext %27)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = load i8, i8* %28
br label %37
33: ; preds = %26
%34 = getelementptr inbounds i8, i8* %28, i64 1
%35 = load i8, i8* %34
%36 = icmp eq i8 %35, 0
br i1 %36, label %37, label %26
37: ; preds = %33, %31, %23
%38 = phi i8 [ 0, %23 ], [ %32, %31 ], [ 0, %33 ]
%39 = phi i8* [ %4, %23 ], [ %28, %31 ], [ %34, %33 ]
%40 = call i32 @isspace(i8 signext %38)
%41 = icmp eq i32 %40, 0
br i1 %41, label %15, label %42
42: ; preds = %37
store i8 0, i8* %39
%43 = getelementptr inbounds i8, i8* %39, i64 1
%44 = call i32 @bcmp(i8* dereferenceable(11) %4, i8* dereferenceable(11) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i64 11)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = call i32 @strcmp(i8* %43, i8* dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%48 = icmp eq i32 %47, 0
br i1 %48, label %15, label %55
49: ; preds = %42
%50 = call i32 @bcmp(i8* dereferenceable(7) %4, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i64 7)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %15
52: ; preds = %49
%53 = call i32 @inet_addr(i8* %43)
%54 = icmp eq i32 %53, -1
br i1 %54, label %15, label %55
55: ; preds = %52, %46
%56 = call i32 @strncpy(i8* %0, i8* %43, i64 %1)
%57 = icmp eq i64 %1, 0
br i1 %57, label %61, label %58
58: ; preds = %55
%59 = add i64 %1, -1
%60 = getelementptr inbounds i8, i8* %0, i64 %59
store i8 0, i8* %60
br label %61
61: ; preds = %58, %55
%62 = call i32 @fclose(i32* %5)
br label %65
63: ; preds = %15, %7
%64 = call i32 @fclose(i32* %5)
br label %65
65: ; preds = %63, %61, %10
%66 = phi i32 [ 0, %61 ], [ -1, %63 ], [ -1, %10 ]
ret i32 %66
}