#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*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
scalar_t__ strlen(char *);
int strncpycmd(char *dest, const char *cmd, size_t size) {
  FILE *cmdfd;
  size_t offset = 0;
  cmdfd = popen(cmd, "r");
  if (cmdfd == NULL)
    return -1;
  while (fgets(dest + offset, size - offset, cmdfd) != NULL)
    offset += strlen(dest + offset);
  if (pclose(cmdfd) != 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<strncpycmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1187 <strncpycmd+0x47>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <strncpycmd+0xaf>
jmp    118c <strncpycmd+0x4c>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
sub    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11c9 <strncpycmd+0x89>
mov    -0x10(%rbp),%rdi
add    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    118c <strncpycmd+0x4c>
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
cmp    $0x0,%rax
je     11e8 <strncpycmd+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ef <strncpycmd+0xaf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strncpycmd>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rax
mov    %rdi,%r15
lea    0xea7(%rip),%rsi
mov    %rax,%rdi
call   1060 <popen@plt>
test   %rax,%rax
je     11c4 <strncpycmd+0x84>
mov    %rax,%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b5 <strncpycmd+0x75>
xor    %ebx,%ebx
mov    %r15,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1030 <strlen@plt>
add    %rax,%rbx
lea    (%r15,%rbx,1),%r13
mov    %r14,%rsi
sub    %rbx,%rsi
mov    %r13,%rdi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1190 <strncpycmd+0x50>
mov    %r12,%rdi
call   1040 <pclose@plt>
neg    %rax
sbb    %eax,%eax
jmp    11c9 <strncpycmd+0x89>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @strncpycmd(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %9
%10 = load i8*, i8** %6
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = load i32*, i32** %8
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %39
15: ; preds = %3
br label %16
16: ; preds = %26, %15
%17 = load i8*, i8** %5
%18 = load i64, i64* %9
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i64, i64* %7
%21 = load i64, i64* %9
%22 = sub i64 %20, %21
%23 = load i32*, i32** %8
%24 = call i32* @fgets(i8* %19, i64 %22, i32* %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %33
26: ; preds = %16
%27 = load i8*, i8** %5
%28 = load i64, i64* %9
%29 = getelementptr inbounds i8, i8* %27, i64 %28
%30 = call i64 @strlen(i8* %29)
%31 = load i64, i64* %9
%32 = add i64 %31, %30
store i64 %32, i64* %9
br label %16
33: ; preds = %16
%34 = load i32*, i32** %8
%35 = call i64 @pclose(i32* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 -1, i32* %4
br label %39
38: ; preds = %33
store i32 0, i32* %4
br label %39
39: ; preds = %38, %37, %14
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @strncpycmd(i8* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @popen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = tail call i32* @fgets(i8* %0, i64 %2, i32* %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %18, label %9
9: ; preds = %6, %9
%10 = phi i8* [ %14, %9 ], [ %0, %6 ]
%11 = phi i64 [ %13, %9 ], [ 0, %6 ]
%12 = tail call i64 @strlen(i8* dereferenceable(1) %10)
%13 = add i64 %12, %11
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = sub i64 %2, %13
%16 = tail call i32* @fgets(i8* %14, i64 %15, i32* %4)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %9
18: ; preds = %9, %6
%19 = tail call i64 @pclose(i32* %4)
%20 = icmp ne i64 %19, 0
%21 = sext i1 %20 to i32
br label %22
22: ; preds = %18, %3
%23 = phi i32 [ -1, %3 ], [ %21, %18 ]
ret i32 %23
}