#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 BUFSIZ;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ strcmp(char *, char const *);
char *strdup(char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *strsep(char **, char *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int url_from_config(const char *config_path, const char *key, char **value) {
  FILE *fp = NULL;
  fp = fopen(config_path, "r");
  if (fp == NULL) {
    syslog(LOG_ERR, "load_config(): %s", strerror(errno));
    return -1;
  }
  char *cmd;
  char url[BUFSIZ];
  int loaded = 0;
  char *url_ptr;
  while ((fgets(url, BUFSIZ, fp)) != NULL) {
    url_ptr = url;
    cmd = strsep(&url_ptr, " ");
    if (cmd == NULL) {
      break;
    }
    if (strcmp(cmd, key) == 0) {
      *value = strdup(url_ptr);
      fclose(fp);
      return 0;
    }
  }
  fclose(fp);
  return -1;
}


x86 assembly (clang, -O0):
<url_from_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f2 <url_from_config+0x72>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x58(%rbp),%edi
mov    %eax,%edx
lea    0xe21(%rip),%rsi
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12da <url_from_config+0x15a>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x60(%rbp),%rdi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12bc <url_from_config+0x13c>
mov    -0x60(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xdbc(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1271 <url_from_config+0xf1>
jmp    12bc <url_from_config+0x13c>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12b7 <url_from_config+0x137>
mov    -0x50(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    12d3 <url_from_config+0x153>
jmp    1225 <url_from_config+0xa5>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_from_config>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
lea    0xe62(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1242 <url_from_config+0xc2>
mov    %rax,%r12
mov    %rbx,-0x38(%rbp)
mov    %rsp,-0x40(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x30(%rbp),%r14
mov    $0xffffffff,%r13d
nopl   0x0(%rax,%rax,1)
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1234 <url_from_config+0xb4>
mov    %rbx,-0x30(%rbp)
mov    %r14,%rdi
lea    0xe0d(%rip),%rsi
call   1060 <strsep@plt>
test   %rax,%rax
je     1234 <url_from_config+0xb4>
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11e0 <url_from_config+0x60>
mov    -0x30(%rbp),%rdi
call   1090 <strdup@plt>
mov    -0x38(%rbp),%rcx
mov    %rax,(%rcx)
xor    %r13d,%r13d
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    -0x40(%rbp),%rsp
jmp    126f <url_from_config+0xef>
mov    0x2d7f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda2(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1070 <syslog@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@LOG_ERR = global i32 0
@.str.1 = [18 x i8] c"load_config(): %s\00"
@errno = global i32 0
@BUFSIZ = global i32 0
@.str.2 = [2 x i8] c" \00"
define i32 @url_from_config(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i32* null, i32** %8
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %8
%17 = load i32*, i32** %8
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %3
%20 = load i32, i32* @LOG_ERR
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @syslog(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %22)
store i32 -1, i32* %4
br label %56
24: ; preds = %3
%25 = load i32, i32* @BUFSIZ
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %10
%28 = alloca i8, i64 %26
store i64 %26, i64* %11
store i32 0, i32* %12
br label %29
29: ; preds = %50, %24
%30 = load i32, i32* @BUFSIZ
%31 = load i32*, i32** %8
%32 = call i32* @fgets(i8* %28, i32 %30, i32* %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %51
34: ; preds = %29
store i8* %28, i8** %13
%35 = call i8* @strsep(i8** %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %9
%36 = load i8*, i8** %9
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %39
38: ; preds = %34
br label %51
39: ; preds = %34
%40 = load i8*, i8** %9
%41 = load i8*, i8** %6
%42 = call i64 @strcmp(i8* %40, i8* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %39
%45 = load i8*, i8** %13
%46 = call i8* @strdup(i8* %45)
%47 = load i8**, i8*** %7
store i8* %46, i8** %47
%48 = load i32*, i32** %8
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %4
store i32 1, i32* %14
br label %54
50: ; preds = %39
br label %29
51: ; preds = %38, %29
%52 = load i32*, i32** %8
%53 = call i32 @fclose(i32* %52)
store i32 -1, i32* %4
store i32 1, i32* %14
br label %54
54: ; preds = %51, %44
%55 = load i8*, i8** %10
br label %56
56: ; preds = %54, %19
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@LOG_ERR = global i32 0
@.str.1 = [18 x i8] c"load_config(): %s\00"
@errno = global i32 0
@BUFSIZ = global i32 0
@.str.2 = [2 x i8] c" \00"
define i32 @url_from_config(i8* nocapture %0, i8* %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i32, i32* @LOG_ERR
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @syslog(i32 %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %34
12: ; preds = %3
%13 = load i32, i32* @BUFSIZ
%14 = zext i32 %13 to i64
%15 = tail call i8* @llvm.stacksave()
%16 = alloca i8, i64 %14
%17 = bitcast i8** %4 to i8*
br label %18
18: ; preds = %25, %12
%19 = load i32, i32* @BUFSIZ
%20 = call i32* @fgets(i8* %16, i32 %19, i32* %5)
%21 = icmp eq i32* %20, null
br i1 %21, label %31, label %22
22: ; preds = %18
store i8* %16, i8** %4
%23 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i8* %23, null
br i1 %24, label %31, label %25
25: ; preds = %22
%26 = call i64 @strcmp(i8* %23, i8* %1)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %18
28: ; preds = %25
%29 = load i8*, i8** %4
%30 = call i8* @strdup(i8* %29)
store i8* %30, i8** %2
br label %31
31: ; preds = %18, %22, %28
%32 = phi i32 [ 0, %28 ], [ -1, %22 ], [ -1, %18 ]
%33 = call i32 @fclose(i32* %5)
br label %34
34: ; preds = %31, %7
%35 = phi i32 [ -1, %7 ], [ %32, %31 ]
ret i32 %35
}