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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char *, int);
void open_cfgfile(FILE **fileh, const int cfgfile) {
  char path[512];
  switch (cfgfile) {
  case 2:
    snprintf(path, sizeof(path), "%s/%s", "/etc/sysconfig/network-scripts",
             "ifcfg-eth0");
    break;
  case 1:
    strncpy(path, "/etc/resolv.conf", sizeof(path));
    break;
  default:
    fprintf(stderr, "Unknown conf_file id %d\n", cfgfile);
    exit(EXIT_FAILURE);
  }
  *fileh = fopen(path, "w");
  if (*fileh == NULL) {
    fprintf(stderr, "Unable to open config file %s for writing.\n", path);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<open_cfgfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x214(%rbp)
sub    $0x1,%eax
je     11b8 <open_cfgfile+0x68>
jmp    1179 <open_cfgfile+0x29>
mov    -0x214(%rbp),%eax
sub    $0x2,%eax
jne    11d5 <open_cfgfile+0x85>
jmp    118d <open_cfgfile+0x3d>
lea    -0x210(%rbp),%rdi
mov    $0x200,%esi
lea    0xe60(%rip),%rdx
lea    0xe5f(%rip),%rcx
lea    0xe77(%rip),%r8
call   1040 <snprintf@plt>
jmp    11fd <open_cfgfile+0xad>
lea    -0x210(%rbp),%rdi
lea    0xe6a(%rip),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
jmp    11fd <open_cfgfile+0xad>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    -0x210(%rbp),%rdi
lea    0xe4f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1254 <open_cfgfile+0x104>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    -0x210(%rbp),%rdx
lea    0xe1d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_cfgfile>:
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%rbx
cmp    $0x1,%esi
je     118b <open_cfgfile+0x3b>
mov    %esi,%edx
cmp    $0x2,%esi
jne    11db <open_cfgfile+0x8b>
lea    0xe92(%rip),%rdx
lea    0xe91(%rip),%rcx
lea    0xea9(%rip),%r8
mov    %rsp,%rdi
mov    $0x200,%esi
call   1040 <snprintf@plt>
jmp    119f <open_cfgfile+0x4f>
lea    0xe9e(%rip),%rsi
mov    %rsp,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    0xeb4(%rip),%rsi
mov    %rsp,%rdi
call   1060 <fopen@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11bf <open_cfgfile+0x6f>
add    $0x200,%rsp
pop    %rbx
ret
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11f2 <open_cfgfile+0xa2>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe56(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [31 x i8] c"/etc/sysconfig/network-scripts\00"
@.str.2 = [11 x i8] c"ifcfg-eth0\00"
@.str.3 = [17 x i8] c"/etc/resolv.conf\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Unknown conf_file id %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [44 x i8] c"Unable to open config file %s for writing.\0A\00"
define void @open_cfgfile(i32** %0, i32 %1) {
%3 = alloca i32**
%4 = alloca i32
%5 = alloca [512 x i8]
store i32** %0, i32*** %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
switch i32 %6, label %13 [
i32 2, label %7
i32 1, label %10
]
7: ; preds = %2
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %19
10: ; preds = %2
%11 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%12 = call i32 @strncpy(i8* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 512)
br label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* %4
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %10, %7
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%22 = load i32**, i32*** %3
store i32* %21, i32** %22
%23 = load i32**, i32*** %3
%24 = load i32*, i32** %23
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %32
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [31 x i8] c"/etc/sysconfig/network-scripts\00"
@.str.2 = [11 x i8] c"ifcfg-eth0\00"
@.str.3 = [17 x i8] c"/etc/resolv.conf\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"Unknown conf_file id %d\0A\00"
@EXIT_FAILURE = global i32 0
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [44 x i8] c"Unable to open config file %s for writing.\0A\00"
define void @open_cfgfile(i32** nocapture writeonly %0, i32 %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
switch i32 %1, label %9 [
i32 2, label %5
i32 1, label %7
]
5: ; preds = %2
%6 = call i32 @snprintf(i8* %4, i32 512, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %14
7: ; preds = %2
%8 = call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 512)
br label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %1)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7, %5
%15 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %15, i32** %0
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
ret void
}