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

/* Variables and functions */
int EOF;
int conf_copy_file(char const *, char const *);
int conf_get_setting_line(char *, int *, int *, int /*<<< orphan*/ *);
int conf_update_checksum(int /*<<< orphan*/ *);
int conf_update_port(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strncmp(char *, char *, int);
int conf_process_settings(const char *src_file, const char *out_file,
                          const char *settings_file) {
  int ret = 1;
  if ((ret = conf_copy_file(src_file, out_file)) != 0) {
    fprintf(stderr, "Could not copy original binary file\n");
    return ret;
  }
  FILE *settings = NULL;
  FILE *out = NULL;
  settings = fopen(settings_file, "r");
  if (settings == NULL) {
    fprintf(stderr, "Failed to open settings file %s: %s\n", settings_file,
            strerror(errno));
    goto cleanup;
  }
  out = fopen(out_file, "rb+");
  if (out == NULL) {
    fprintf(stderr, "Failed to open output file %s: %s\n", out_file,
            strerror(errno));
    goto cleanup;
  }
  char line[1024];
  uint16_t delim_start, delim_end;
  while ((ret = conf_get_setting_line(line, &delim_start, &delim_end,
                                      settings)) == 0) {
    if (strncmp(line, "port", delim_start) == 0) {
      if ((ret = conf_update_port(out, &line[delim_end + 1])) != 0)
        goto cleanup;
    }
  }
  if (ret != EOF) {
    fprintf(stderr, "Error while reading settings from file %s\n",
            settings_file);
    goto cleanup;
  }
  ret = conf_update_checksum(out);
cleanup:
  fclose(settings);
  fclose(out);
  return ret;
}


x86 assembly (clang, -O0):
<conf_process_settings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10b0 <conf_copy_file@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11e9 <conf_process_settings+0x59>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13ae <conf_process_settings+0x21e>
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1263 <conf_process_settings+0xd3>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x454(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rdx
mov    %rax,%rcx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1396 <conf_process_settings+0x206>
mov    -0x18(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12cd <conf_process_settings+0x13d>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x464(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x464(%rbp),%edi
mov    -0x460(%rbp),%rdx
mov    %rax,%rcx
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1396 <conf_process_settings+0x206>
jmp    12d2 <conf_process_settings+0x142>
lea    -0x440(%rbp),%rdi
mov    -0x30(%rbp),%rcx
lea    -0x444(%rbp),%rsi
lea    -0x448(%rbp),%rdx
call   1090 <conf_get_setting_line@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jne    1358 <conf_process_settings+0x1c8>
lea    -0x440(%rbp),%rdi
mov    -0x444(%rbp),%edx
lea    0xd63(%rip),%rsi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1353 <conf_process_settings+0x1c3>
mov    -0x38(%rbp),%rdi
mov    -0x448(%rbp),%eax
add    $0x1,%eax
cltq
lea    -0x440(%rbp),%rsi
add    %rax,%rsi
call   1030 <conf_update_port@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     134e <conf_process_settings+0x1be>
jmp    1396 <conf_process_settings+0x206>
jmp    1353 <conf_process_settings+0x1c3>
jmp    12d2 <conf_process_settings+0x142>
mov    -0x24(%rbp),%eax
mov    0x2c86(%rip),%rcx
cmp    (%rcx),%eax
je     138a <conf_process_settings+0x1fa>
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xcfa(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1396 <conf_process_settings+0x206>
mov    -0x38(%rbp),%rdi
call   1060 <conf_update_checksum@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<conf_process_settings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,%rbp
mov    %rsi,%r13
call   10b0 <conf_copy_file@plt>
test   %eax,%eax
je     11ce <conf_process_settings+0x3e>
mov    %eax,%ebx
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1330 <conf_process_settings+0x1a0>
lea    0xe50(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
je     128b <conf_process_settings+0xfb>
lea    0xe5c(%rip),%rsi
mov    %r13,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12b4 <conf_process_settings+0x124>
mov    %rax,%r15
lea    0x10(%rsp),%r13
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
mov    %r13,%rdi
mov    %r12,%rcx
call   1090 <conf_get_setting_line@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    12ef <conf_process_settings+0x15f>
mov    %rbp,0x8(%rsp)
lea    0x4(%rsp),%r14
mov    %rsp,%rbp
jmp    1259 <conf_process_settings+0xc9>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %r12,%rcx
call   1090 <conf_get_setting_line@plt>
test   %eax,%eax
jne    12e8 <conf_process_settings+0x158>
mov    0x4(%rsp),%edx
mov    %r13,%rdi
lea    0xe0c(%rip),%rsi
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1240 <conf_process_settings+0xb0>
movslq (%rsp),%rax
lea    (%rax,%r13,1),%rsi
add    $0x1,%rsi
mov    %r15,%rdi
call   1030 <conf_update_port@plt>
test   %eax,%eax
je     1240 <conf_process_settings+0xb0>
jmp    1302 <conf_process_settings+0x172>
mov    0x2d46(%rip),%rax
mov    (%rax),%r14d
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd7d(%rip),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %rbp,%rdx
jmp    12d9 <conf_process_settings+0x149>
mov    0x2d1d(%rip),%rax
mov    (%rax),%ebp
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd7e(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r13,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %r15d,%r15d
jmp    1320 <conf_process_settings+0x190>
mov    %eax,%ebx
mov    0x8(%rsp),%rbp
mov    0x2cf2(%rip),%rax
cmp    (%rax),%ebx
jne    1306 <conf_process_settings+0x176>
mov    %r15,%rdi
call   1060 <conf_update_checksum@plt>
mov    %eax,%ebx
jmp    1320 <conf_process_settings+0x190>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"Could not copy original binary file\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [37 x i8] c"Failed to open settings file %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"rb+\00"
@.str.4 = [35 x i8] c"Failed to open output file %s: %s\0A\00"
@.str.5 = [5 x i8] c"port\00"
@EOF = global i32 0
@.str.6 = [43 x i8] c"Error while reading settings from file %s\0A\00"
define i32 @conf_process_settings(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca [1024 x i8]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 1, i32* %8
%14 = load i8*, i8** %5
%15 = load i8*, i8** %6
%16 = call i32 @conf_copy_file(i8* %14, i8* %15)
store i32 %16, i32* %8
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %8
store i32 %21, i32* %4
br label %83
22: ; preds = %3
store i32* null, i32** %9
store i32* null, i32** %10
%23 = load i8*, i8** %7
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %24, i32** %9
%25 = load i32*, i32** %9
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i32, i32* @stderr
%29 = load i8*, i8** %7
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* %31)
br label %77
33: ; preds = %22
%34 = load i8*, i8** %6
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %10
%36 = load i32*, i32** %10
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %6
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %40, i8* %42)
br label %77
44: ; preds = %33
br label %45
45: ; preds = %65, %44
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%47 = load i32*, i32** %9
%48 = call i32 @conf_get_setting_line(i8* %46, i32* %12, i32* %13, i32* %47)
store i32 %48, i32* %8
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %66
50: ; preds = %45
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%52 = load i32, i32* %12
%53 = call i64 @strncmp(i8* %51, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %50
%56 = load i32*, i32** %10
%57 = load i32, i32* %13
%58 = add nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 %59
%61 = call i32 @conf_update_port(i32* %56, i8* %60)
store i32 %61, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %55
br label %77
64: ; preds = %55
br label %65
65: ; preds = %64, %50
br label %45
66: ; preds = %45
%67 = load i32, i32* %8
%68 = load i32, i32* @EOF
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %7
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %72)
br label %77
74: ; preds = %66
%75 = load i32*, i32** %10
%76 = call i32 @conf_update_checksum(i32* %75)
store i32 %76, i32* %8
br label %77
77: ; preds = %74, %70, %63, %38, %27
%78 = load i32*, i32** %9
%79 = call i32 @fclose(i32* %78)
%80 = load i32*, i32** %10
%81 = call i32 @fclose(i32* %80)
%82 = load i32, i32* %8
store i32 %82, i32* %4
br label %83
83: ; preds = %77, %18
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"Could not copy original binary file\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [37 x i8] c"Failed to open settings file %s: %s\0A\00"
@errno = global i32 0
@.str.3 = [4 x i8] c"rb+\00"
@.str.4 = [35 x i8] c"Failed to open output file %s: %s\0A\00"
@.str.5 = [5 x i8] c"port\00"
@EOF = global i32 0
@.str.6 = [43 x i8] c"Error while reading settings from file %s\0A\00"
define i32 @conf_process_settings(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32 @conf_copy_file(i8* %0, i8* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %60
12: ; preds = %3
%13 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %18)
br label %55
20: ; preds = %12
%21 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %20
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%25 = call i32 @conf_get_setting_line(i8* %24, i32* %5, i32* %6, i32* %13)
%26 = icmp eq i32 %25, 0
br i1 %26, label %32, label %46
27: ; preds = %20
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %1, i8* %30)
br label %55
32: ; preds = %23, %43
%33 = load i32, i32* %5
%34 = call i64 @strncmp(i8* %24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %32
%37 = load i32, i32* %6
%38 = add nsw i32 %37, 1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %39
%41 = call i32 @conf_update_port(i32* %21, i8* %40)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %55
43: ; preds = %36, %32
%44 = call i32 @conf_get_setting_line(i8* %24, i32* %5, i32* %6, i32* %13)
%45 = icmp eq i32 %44, 0
br i1 %45, label %32, label %46
46: ; preds = %43, %23
%47 = phi i32 [ %25, %23 ], [ %44, %43 ]
%48 = load i32, i32* @EOF
%49 = icmp eq i32 %47, %48
br i1 %49, label %53, label %50
50: ; preds = %46
%51 = load i32, i32* @stderr
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.6, i64 0, i64 0), i8* %2)
br label %55
53: ; preds = %46
%54 = call i32 @conf_update_checksum(i32* %21)
br label %55
55: ; preds = %36, %53, %50, %27, %15
%56 = phi i32 [ 0, %15 ], [ 0, %27 ], [ %47, %50 ], [ %54, %53 ], [ %41, %36 ]
%57 = phi i32* [ null, %15 ], [ null, %27 ], [ %21, %50 ], [ %21, %53 ], [ %21, %36 ]
%58 = call i32 @fclose(i32* %13)
%59 = call i32 @fclose(i32* %57)
br label %60
60: ; preds = %55, %9
%61 = phi i32 [ %7, %9 ], [ %56, %55 ]
ret i32 %61
}