#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char **, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strdup(char *);
char **getservices() {
  FILE *fp = fopen("/etc/discoveryd/myservices", "r");
  char **services = (char **)malloc(sizeof(char *) * 1024);
  memset(services, 0, sizeof(char *) * 1024);
  char **ptr = services;
  if (fp) {
    char buf[1024];
    size_t len = fread(buf, 1, 1024, fp);
    buf[len] = 0;
    char line[512];
    size_t k = 0, i = 0;
    for (i = 0; i <= len; i++) {
      if (buf[i] == '\n' || buf[i] == '\0') {
        line[k] = '\0';
        k = 0;
        if (!strcmp(line, ""))
          continue;
        *(ptr++) = strdup(line);
        *(ptr) = NULL;
      } else {
        line[k++] = buf[i];
      }
    }
    fclose(fp);
  }
  return services;
}


x86 assembly (clang, -O0):
<getservices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x640,%rsp
lea    0xe7e(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    $0x2000,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1313 <getservices+0x1a3>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %rax,-0x428(%rbp)
mov    -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
movq   $0x0,-0x638(%rbp)
movq   $0x0,-0x640(%rbp)
movq   $0x0,-0x640(%rbp)
mov    -0x640(%rbp),%rax
cmp    -0x428(%rbp),%rax
ja     130a <getservices+0x19a>
mov    -0x640(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     1258 <getservices+0xe8>
mov    -0x640(%rbp),%rax
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0x0,%eax
jne    12c4 <getservices+0x154>
mov    -0x638(%rbp),%rax
movb   $0x0,-0x630(%rbp,%rax,1)
movq   $0x0,-0x638(%rbp)
lea    -0x630(%rbp),%rdi
lea    0xd9c(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1293 <getservices+0x123>
jmp    12f3 <getservices+0x183>
lea    -0x630(%rbp),%rdi
call   1090 <strdup@plt>
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    12ee <getservices+0x17e>
mov    -0x640(%rbp),%rax
mov    -0x420(%rbp,%rax,1),%cl
mov    -0x638(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x638(%rbp)
mov    %cl,-0x630(%rbp,%rax,1)
jmp    12f3 <getservices+0x183>
mov    -0x640(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x640(%rbp)
jmp    1214 <getservices+0xa4>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
add    $0x640,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getservices>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x608,%rsp
lea    0xe88(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
mov    $0x2000,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1050 <memset@plt>
test   %r14,%r14
je     1231 <getservices+0xd1>
lea    0x200(%rsp),%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rax,%r13
movb   $0x0,0x200(%rsp,%rax,1)
xor    %ebx,%ebx
mov    %rsp,%r12
xor    %eax,%eax
mov    %r15,%rbp
jmp    11f0 <getservices+0x90>
nopl   (%rax)
mov    %cl,(%rsp,%rax,1)
add    $0x1,%rax
add    $0x1,%rbx
cmp    %r13,%rbx
ja     1229 <getservices+0xc9>
movzbl 0x200(%rsp,%rbx,1),%ecx
cmp    $0xa,%cl
je     1201 <getservices+0xa1>
test   %cl,%cl
jne    11e0 <getservices+0x80>
movb   $0x0,(%rsp,%rax,1)
cmpb   $0x0,(%rsp)
je     1225 <getservices+0xc5>
mov    %r12,%rdi
call   1080 <strdup@plt>
cltq
mov    %rax,0x0(%rbp)
movq   $0x0,0x8(%rbp)
add    $0x8,%rbp
xor    %eax,%eax
jmp    11e7 <getservices+0x87>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rax
add    $0x608,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"/etc/discoveryd/myservices\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [1 x i8] zeroinitializer
define i8** @getservices() {
%1 = alloca i32*
%2 = alloca i8**
%3 = alloca i8**
%4 = alloca [1024 x i8]
%5 = alloca i64
%6 = alloca [512 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = call i32* @fopen(i8* getelementptr inbounds ([27 x i8], [27 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** %1
%10 = call i64 @malloc(i32 8192)
%11 = inttoptr i64 %10 to i8**
store i8** %11, i8*** %2
%12 = load i8**, i8*** %2
%13 = call i32 @memset(i8** %12, i32 0, i32 8192)
%14 = load i8**, i8*** %2
store i8** %14, i8*** %3
%15 = load i32*, i32** %1
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %68
17: ; preds = %0
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%19 = load i32*, i32** %1
%20 = call i64 @fread(i8* %18, i32 1, i32 1024, i32* %19)
store i64 %20, i64* %5
%21 = load i64, i64* %5
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %21
store i8 0, i8* %22
store i64 0, i64* %7
store i64 0, i64* %8
store i64 0, i64* %8
br label %23
23: ; preds = %62, %17
%24 = load i64, i64* %8
%25 = load i64, i64* %5
%26 = icmp ule i64 %24, %25
br i1 %26, label %27, label %65
27: ; preds = %23
%28 = load i64, i64* %8
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 10
br i1 %32, label %39, label %33
33: ; preds = %27
%34 = load i64, i64* %8
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %33, %27
%40 = load i64, i64* %7
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %40
store i8 0, i8* %41
store i64 0, i64* %7
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%43 = call i32 @strcmp(i8* %42, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%44 = icmp ne i32 %43, 0
br i1 %44, label %46, label %45
45: ; preds = %39
br label %62
46: ; preds = %39
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%48 = call i32 @strdup(i8* %47)
%49 = sext i32 %48 to i64
%50 = inttoptr i64 %49 to i8*
%51 = load i8**, i8*** %3
%52 = getelementptr inbounds i8*, i8** %51, i32 1
store i8** %52, i8*** %3
store i8* %50, i8** %51
%53 = load i8**, i8*** %3
store i8* null, i8** %53
br label %61
54: ; preds = %33
%55 = load i64, i64* %8
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = load i64, i64* %7
%59 = add i64 %58, 1
store i64 %59, i64* %7
%60 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 %58
store i8 %57, i8* %60
br label %61
61: ; preds = %54, %46
br label %62
62: ; preds = %61, %45
%63 = load i64, i64* %8
%64 = add i64 %63, 1
store i64 %64, i64* %8
br label %23
65: ; preds = %23
%66 = load i32*, i32** %1
%67 = call i32 @fclose(i32* %66)
br label %68
68: ; preds = %65, %0
%69 = load i8**, i8*** %2
ret i8** %69
}

LLVM IR (-O2):
@.str = [27 x i8] c"/etc/discoveryd/myservices\00"
@.str.1 = [2 x i8] c"r\00"
define i8** @getservices() {
%1 = alloca [1024 x i8]
%2 = alloca [512 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i64 @malloc(i32 8192)
%5 = inttoptr i64 %4 to i8**
%6 = tail call i32 @memset(i8** %5, i32 0, i32 8192)
%7 = icmp eq i32* %3, null
br i1 %7, label %38, label %8
8: ; preds = %0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%10 = call i64 @fread(i8* %9, i32 1, i32 1024, i32* %3)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %10
store i8 0, i8* %11
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
br label %13
13: ; preds = %8, %31
%14 = phi i64 [ 0, %8 ], [ %34, %31 ]
%15 = phi i64 [ 0, %8 ], [ %33, %31 ]
%16 = phi i8** [ %5, %8 ], [ %32, %31 ]
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %14
%18 = load i8, i8* %17
switch i8 %18, label %28 [
i8 10, label %19
i8 0, label %19
]
19: ; preds = %13, %13
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %15
store i8 0, i8* %20
%21 = load i8, i8* %12
%22 = icmp eq i8 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %19
%24 = call i32 @strdup(i8* %12)
%25 = sext i32 %24 to i64
%26 = inttoptr i64 %25 to i8*
%27 = getelementptr inbounds i8*, i8** %16, i64 1
store i8* %26, i8** %16
store i8* null, i8** %27
br label %31
28: ; preds = %13
%29 = add i64 %15, 1
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %15
store i8 %18, i8* %30
br label %31
31: ; preds = %23, %28, %19
%32 = phi i8** [ %27, %23 ], [ %16, %19 ], [ %16, %28 ]
%33 = phi i64 [ 0, %23 ], [ 0, %19 ], [ %29, %28 ]
%34 = add i64 %14, 1
%35 = icmp ugt i64 %34, %10
br i1 %35, label %36, label %13
36: ; preds = %31
%37 = call i32 @fclose(i32* %3)
br label %38
38: ; preds = %36, %0
ret i8** %5
}