#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;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ close(int);
int do_waitpid(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int get_colocated_path(char const *, char *, char *, int);
int safe_write(int, char const *, scalar_t__);
int start_cmd_give_input(char const **, int *);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char const *);
int test_run_cmd_give_input(const char *argv0, const char *str) {
  int fd, pid, ret;
  char path[PATH_MAX];
  do {
    int __my_ret__ =
        get_colocated_path(argv0, "run_cmd_stdin_test", path, sizeof(path));
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 102,
              "get_colocated_path(argv0, \"run_cmd_stdin_test\", path, "
              "sizeof(path))");
      return __my_ret__;
    }
  } while (0);
  ;
  {
    const char *cvec[] = {path, str, NULL};
    fd = start_cmd_give_input(cvec, &pid);
  }
  if (fd < 0) {
    fprintf(stderr,
            "start_cmd_give_input failed with "
            "error %d\n",
            fd);
    return fd;
  }
  do {
    int __my_ret__ = safe_write(fd, str, strlen(str) + 1);
    if (__my_ret__) {
      fprintf(stderr, "failed on line %d: %s\n", 113,
              "safe_write(fd, str, strlen(str) + 1)");
      return __my_ret__;
    }
  } while (0);
  ;
  close(fd);
  ret = do_waitpid(pid);
  if (ret != 0) {
    fprintf(stderr, "do_waitpid returned %d\n", ret);
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_run_cmd_give_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x78(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x70(%rbp),%rdx
mov    -0x78(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    %eax,%ecx
lea    0xe38(%rip),%rsi
call   1050 <get_colocated_path@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     120f <test_run_cmd_give_input+0x9f>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
mov    $0x66,%edx
lea    0xe34(%rip),%rcx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    134f <test_run_cmd_give_input+0x1df>
jmp    1214 <test_run_cmd_give_input+0xa4>
jmp    1219 <test_run_cmd_give_input+0xa9>
mov    -0x70(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
lea    -0x60(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <start_cmd_give_input@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1277 <test_run_cmd_give_input+0x107>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    134f <test_run_cmd_give_input+0x1df>
jmp    127c <test_run_cmd_give_input+0x10c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x84(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1090 <safe_write@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
je     12ee <test_run_cmd_give_input+0x17e>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
mov    $0x71,%edx
lea    0xdc4(%rip),%rcx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    134f <test_run_cmd_give_input+0x1df>
jmp    12f3 <test_run_cmd_give_input+0x183>
jmp    12f8 <test_run_cmd_give_input+0x188>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1080 <do_waitpid@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1341 <test_run_cmd_give_input+0x1d1>
mov    0x2cbc(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    134f <test_run_cmd_give_input+0x1df>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_run_cmd_give_input>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rsp,%r12
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe57(%rip),%rsi
mov    %rbx,%rdx
call   1050 <get_colocated_path@plt>
test   %eax,%eax
je     11e0 <test_run_cmd_give_input+0x70>
mov    %eax,%r15d
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
lea    0xe5b(%rip),%rcx
mov    $0x66,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    128d <test_run_cmd_give_input+0x11d>
mov    %rbx,-0x40(%rbp)
mov    %r14,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1060 <start_cmd_give_input@plt>
mov    %eax,%r15d
test   %eax,%eax
js     124a <test_run_cmd_give_input+0xda>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r15d,%edi
mov    %r14,%rsi
call   1090 <safe_write@plt>
test   %eax,%eax
je     125c <test_run_cmd_give_input+0xec>
mov    %eax,%r14d
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xde1(%rip),%rsi
lea    0xe60(%rip),%rcx
mov    $0x71,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14d,%r15d
jmp    128d <test_run_cmd_give_input+0x11d>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
jmp    1283 <test_run_cmd_give_input+0x113>
mov    %r15d,%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1080 <do_waitpid@plt>
test   %eax,%eax
je     12a0 <test_run_cmd_give_input+0x130>
mov    %eax,%r15d
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r12,%rsp
mov    %r15d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r15d,%r15d
jmp    128d <test_run_cmd_give_input+0x11d>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [19 x i8] c"run_cmd_stdin_test\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"failed on line %d: %s\0A\00"
@.str.2 = [68 x i8] c"get_colocated_path(argv0, \22run_cmd_stdin_test\22, path, sizeof(path))\00"
@.str.3 = [43 x i8] c"start_cmd_give_input failed with error %d\0A\00"
@.str.4 = [37 x i8] c"safe_write(fd, str, strlen(str) + 1)\00"
@.str.5 = [24 x i8] c"do_waitpid returned %d\0A\00"
define i32 @test_run_cmd_give_input(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca [3 x i8*]
%14 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %9
%18 = alloca i8, i64 %16
store i64 %16, i64* %10
br label %19
19: ; preds = %2
%20 = load i8*, i8** %4
%21 = trunc i64 %16 to i32
%22 = call i32 @get_colocated_path(i8* %20, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %18, i32 %21)
store i32 %22, i32* %11
%23 = load i32, i32* %11
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 102, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %11
store i32 %28, i32* %3
store i32 1, i32* %12
br label %74
29: ; preds = %19
br label %30
30: ; preds = %29
br label %31
31: ; preds = %30
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %13, i64 0, i64 0
store i8* %18, i8** %32
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %5
store i8* %34, i8** %33
%35 = getelementptr inbounds i8*, i8** %33, i64 1
store i8* null, i8** %35
%36 = getelementptr inbounds [3 x i8*], [3 x i8*]* %13, i64 0, i64 0
%37 = call i32 @start_cmd_give_input(i8** %36, i32* %7)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %31
%41 = load i32, i32* @stderr
%42 = load i32, i32* %6
%43 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %6
store i32 %44, i32* %3
store i32 1, i32* %12
br label %74
45: ; preds = %31
br label %46
46: ; preds = %45
%47 = load i32, i32* %6
%48 = load i8*, i8** %5
%49 = load i8*, i8** %5
%50 = call i64 @strlen(i8* %49)
%51 = add nsw i64 %50, 1
%52 = call i32 @safe_write(i32 %47, i8* %48, i64 %51)
store i32 %52, i32* %14
%53 = load i32, i32* %14
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %46
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 113, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%58 = load i32, i32* %14
store i32 %58, i32* %3
store i32 1, i32* %12
br label %74
59: ; preds = %46
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* %7
%65 = call i32 @do_waitpid(i32 %64)
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %73
68: ; preds = %61
%69 = load i32, i32* @stderr
%70 = load i32, i32* %8
%71 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %70)
%72 = load i32, i32* %8
store i32 %72, i32* %3
store i32 1, i32* %12
br label %74
73: ; preds = %61
store i32 0, i32* %3
store i32 1, i32* %12
br label %74
74: ; preds = %73, %68, %55, %40, %25
%75 = load i8*, i8** %9
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [19 x i8] c"run_cmd_stdin_test\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"failed on line %d: %s\0A\00"
@.str.2 = [68 x i8] c"get_colocated_path(argv0, \22run_cmd_stdin_test\22, path, sizeof(path))\00"
@.str.3 = [43 x i8] c"start_cmd_give_input failed with error %d\0A\00"
@.str.4 = [37 x i8] c"safe_write(fd, str, strlen(str) + 1)\00"
@.str.5 = [24 x i8] c"do_waitpid returned %d\0A\00"
define i32 @test_run_cmd_give_input(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca [3 x i8*]
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @get_colocated_path(i8* %0, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %6)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 102, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
br label %41
15: ; preds = %2
%16 = bitcast [3 x i8*]* %4 to i8*
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
store i8* %9, i8** %17
%18 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 1
store i8* %1, i8** %18
%19 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 2
store i8* null, i8** %19
%20 = call i32 @start_cmd_give_input(i8** %17, i32* %3)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.3, i64 0, i64 0), i32 %20)
br label %41
25: ; preds = %15
%26 = call i64 @strlen(i8* dereferenceable(1) %1)
%27 = add nsw i64 %26, 1
%28 = call i32 @safe_write(i32 %20, i8* %1, i64 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 113, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %41
33: ; preds = %25
%34 = call i32 @close(i32 %20)
%35 = load i32, i32* %3
%36 = call i32 @do_waitpid(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %36)
br label %41
41: ; preds = %30, %12, %33, %38, %22
%42 = phi i32 [ %20, %22 ], [ %36, %38 ], [ %28, %30 ], [ %10, %12 ], [ 0, %33 ]
ret i32 %42
}