#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*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
int sscanf(char *, char *, char *);
int mg_get_ip_address_of_nameserver(char *name, size_t name_len) {
  int ret = -1;
  FILE *fp;
  char line[512];
  if ((fp = fopen("/etc/resolv.conf", "r")) == NULL) {
    ret = -1;
  } else {
    for (ret = -1; fgets(line, sizeof(line), fp) != NULL;) {
      char buf[256];
      if (sscanf(line, "nameserver %255[^\n\t #]s", buf) == 1) {
        snprintf(name, name_len, "udp://%s:53", buf);
        ret = 0;
        break;
      }
    }
    (void)fclose(fp);
  }
  return ret;
}


x86 assembly (clang, -O0):
<mg_get_ip_address_of_nameserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x320,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    0xe8f(%rip),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1197 <mg_get_ip_address_of_nameserver+0x47>
movl   $0xffffffff,-0x14(%rbp)
jmp    1215 <mg_get_ip_address_of_nameserver+0xc5>
movl   $0xffffffff,-0x14(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     120c <mg_get_ip_address_of_nameserver+0xbc>
lea    -0x220(%rbp),%rdi
lea    -0x320(%rbp),%rdx
lea    0xe41(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1207 <mg_get_ip_address_of_nameserver+0xb7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x320(%rbp),%rcx
lea    0xe35(%rip),%rdx
call   1040 <snprintf@plt>
movl   $0x0,-0x14(%rbp)
jmp    120c <mg_get_ip_address_of_nameserver+0xbc>
jmp    119e <mg_get_ip_address_of_nameserver+0x4e>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x320,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mg_get_ip_address_of_nameserver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rsi,%rbp
mov    %rdi,%r15
lea    0xe92(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ea <mg_get_ip_address_of_nameserver+0x9a>
mov    %rax,%rbx
lea    0x110(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     120e <mg_get_ip_address_of_nameserver+0xbe>
mov    %rbp,0x8(%rsp)
lea    0xe65(%rip),%r13
lea    0x110(%rsp),%rbp
lea    0x10(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     11f2 <mg_get_ip_address_of_nameserver+0xa2>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11c0 <mg_get_ip_address_of_nameserver+0x70>
jmp    120e <mg_get_ip_address_of_nameserver+0xbe>
mov    $0xffffffff,%r12d
jmp    1216 <mg_get_ip_address_of_nameserver+0xc6>
lea    0xe32(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
call   1040 <snprintf@plt>
xor    %r12d,%r12d
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"nameserver %255[^\0A\09 #]s\00"
@.str.3 = [12 x i8] c"udp://%s:53\00"
define i32 @mg_get_ip_address_of_nameserver(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca [256 x i8]
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 -1, i32* %5
%9 = 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* %9, i32** %6
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 -1, i32* %5
br label %32
12: ; preds = %2
store i32 -1, i32* %5
br label %13
13: ; preds = %28, %12
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i32* @fgets(i8* %14, i32 512, i32* %15)
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %29
18: ; preds = %13
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = load i64, i64* %4
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%27 = call i32 @snprintf(i8* %24, i64 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %26)
store i32 0, i32* %5
br label %29
28: ; preds = %18
br label %13
29: ; preds = %23, %13
%30 = load i32*, i32** %6
%31 = call i32 @fclose(i32* %30)
br label %32
32: ; preds = %29, %11
%33 = load i32, i32* %5
ret i32 %33
}

LLVM IR (-O2):
@.str = [17 x i8] c"/etc/resolv.conf\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"nameserver %255[^\0A\09 #]s\00"
@.str.3 = [12 x i8] c"udp://%s:53\00"
define i32 @mg_get_ip_address_of_nameserver(i8* %0, i64 %1) {
%3 = alloca [512 x i8]
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = 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))
%7 = icmp eq i32* %6, null
br i1 %7, label %24, label %8
8: ; preds = %2
%9 = call i32* @fgets(i8* %5, i32 512, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %21, label %11
11: ; preds = %8
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
br label %13
13: ; preds = %11, %18
%14 = call i32 @sscanf(i8* %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %12)
br label %21
18: ; preds = %13
%19 = call i32* @fgets(i8* %5, i32 512, i32* %6)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %13
21: ; preds = %18, %8, %16
%22 = phi i32 [ 0, %16 ], [ -1, %8 ], [ -1, %18 ]
%23 = call i32 @fclose(i32* %6)
br label %24
24: ; preds = %2, %21
%25 = phi i32 [ %22, %21 ], [ -1, %2 ]
ret i32 %25
}