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

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int strlen(char *);
void kvp_process_ipconfig_file(char *cmd, char *config_buf, unsigned int len,
                               int element_size, int offset) {
  char buf[256];
  char *p;
  char *x;
  FILE *file;
  file = popen(cmd, "r");
  if (file == NULL)
    return;
  if (offset == 0)
    memset(config_buf, 0, len);
  while ((p = fgets(buf, sizeof(buf), file)) != NULL) {
    if (len < strlen(config_buf) + element_size + 1)
      break;
    x = strchr(p, '\n');
    if (x)
      *x = '\0';
    strcat(config_buf, p);
    strcat(config_buf, ";");
  }
  pclose(file);
}


x86 assembly (clang, -O0):
<kvp_process_ipconfig_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    11b7 <kvp_process_ipconfig_file+0x47>
jmp    128b <kvp_process_ipconfig_file+0x11b>
cmpl   $0x0,-0x1c(%rbp)
jne    11cf <kvp_process_ipconfig_file+0x5f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
jmp    11d4 <kvp_process_ipconfig_file+0x64>
lea    -0x120(%rbp),%rdi
mov    -0x138(%rbp),%rdx
mov    $0x100,%esi
call   1070 <fgets@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     127f <kvp_process_ipconfig_file+0x10f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x13c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x13c(%rbp),%eax
add    -0x18(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jae    122a <kvp_process_ipconfig_file+0xba>
jmp    127f <kvp_process_ipconfig_file+0x10f>
mov    -0x128(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x130(%rbp)
cmpq   $0x0,-0x130(%rbp)
je     125a <kvp_process_ipconfig_file+0xea>
mov    -0x130(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    0xd8d(%rip),%rsi
call   1090 <strcat@plt>
jmp    11d4 <kvp_process_ipconfig_file+0x64>
mov    -0x138(%rbp),%rdi
call   1050 <pclose@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_process_ipconfig_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %r8d,%r12d
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%rbp
lea    0xe6c(%rip),%rsi
call   1080 <popen@plt>
test   %rax,%rax
je     1241 <kvp_process_ipconfig_file+0xd1>
mov    %rax,%rbx
test   %r12d,%r12d
jne    11b7 <kvp_process_ipconfig_file+0x47>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1060 <memset@plt>
mov    %rsp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1239 <kvp_process_ipconfig_file+0xc9>
mov    %rax,%r12
add    $0x1,%r15d
mov    %rsp,%r13
jmp    1212 <kvp_process_ipconfig_file+0xa2>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %rbp,%rdi
lea    0xe0d(%rip),%rsi
call   1090 <strcat@plt>
mov    %r13,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %rax,%r12
test   %rax,%rax
je     1239 <kvp_process_ipconfig_file+0xc9>
mov    %rbp,%rdi
call   1030 <strlen@plt>
add    %r15d,%eax
cmp    %r14d,%eax
ja     1239 <kvp_process_ipconfig_file+0xc9>
mov    %r12,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11e0 <kvp_process_ipconfig_file+0x70>
movb   $0x0,(%rax)
jmp    11e0 <kvp_process_ipconfig_file+0x70>
mov    %rbx,%rdi
call   1050 <pclose@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c";\00"
define void @kvp_process_ipconfig_file(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [256 x i8]
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i8*, i8** %6
%16 = call i32* @popen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %14
%17 = load i32*, i32** %14
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %5
br label %58
20: ; preds = %5
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i8*, i8** %7
%25 = load i32, i32* %8
%26 = call i32 @memset(i8* %24, i32 0, i32 %25)
br label %27
27: ; preds = %23, %20
br label %28
28: ; preds = %49, %27
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%30 = load i32*, i32** %14
%31 = call i8* @fgets(i8* %29, i32 256, i32* %30)
store i8* %31, i8** %12
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %55
33: ; preds = %28
%34 = load i32, i32* %8
%35 = load i8*, i8** %7
%36 = call i32 @strlen(i8* %35)
%37 = load i32, i32* %9
%38 = add nsw i32 %36, %37
%39 = add nsw i32 %38, 1
%40 = icmp ult i32 %34, %39
br i1 %40, label %41, label %42
41: ; preds = %33
br label %55
42: ; preds = %33
%43 = load i8*, i8** %12
%44 = call i8* @strchr(i8* %43, i8 signext 10)
store i8* %44, i8** %13
%45 = load i8*, i8** %13
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = load i8*, i8** %13
store i8 0, i8* %48
br label %49
49: ; preds = %47, %42
%50 = load i8*, i8** %7
%51 = load i8*, i8** %12
%52 = call i32 @strcat(i8* %50, i8* %51)
%53 = load i8*, i8** %7
%54 = call i32 @strcat(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %28
55: ; preds = %41, %28
%56 = load i32*, i32** %14
%57 = call i32 @pclose(i32* %56)
br label %58
58: ; preds = %55, %19
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c";\00"
define void @kvp_process_ipconfig_file(i8* nocapture %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%8 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %35, label %10
10: ; preds = %5
%11 = icmp eq i32 %4, 0
br i1 %11, label %12, label %14
12: ; preds = %10
%13 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
br label %14
14: ; preds = %12, %10
%15 = call i8* @fgets(i8* %7, i32 256, i32* %8)
%16 = icmp eq i8* %15, null
br i1 %16, label %33, label %17
17: ; preds = %14
%18 = add i32 %3, 1
br label %19
19: ; preds = %17, %28
%20 = phi i8* [ %15, %17 ], [ %31, %28 ]
%21 = call i32 @strlen(i8* %1)
%22 = add i32 %18, %21
%23 = icmp ugt i32 %22, %2
br i1 %23, label %33, label %24
24: ; preds = %19
%25 = call i8* @strchr(i8* %20, i8 signext 10)
%26 = icmp eq i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %24
store i8 0, i8* %25
br label %28
28: ; preds = %27, %24
%29 = call i32 @strcat(i8* %1, i8* %20)
%30 = call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%31 = call i8* @fgets(i8* %7, i32 256, i32* %8)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %19
33: ; preds = %28, %19, %14
%34 = call i32 @pclose(i32* %8)
br label %35
35: ; preds = %5, %33
ret void
}