#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*/ exit(int);
int fwrite(char *, int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main() {
  FILE *fp = popen("./scanf", "w");
  if (!fp) {
    perror("popen");
    exit(-1);
  }
  char buf[] = "hello world!";
  int ret = fwrite(buf, 1, strlen(buf), fp);
  if (ret < 0) {
    perror("fwrite");
    exit(-1);
  }
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a7 <main+0x47>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0xe62(%rip),%rax
mov    %rax,-0x1d(%rbp)
mov    0xe60(%rip),%eax
mov    %eax,-0x15(%rbp)
mov    0xe5b(%rip),%al
mov    %al,-0x11(%rbp)
lea    -0x1d(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x1d(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    120c <main+0xac>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe92(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     11d5 <main+0x75>
mov    %rax,%rbx
movabs $0x21646c726f7720,%rax
mov    %rax,0xd(%rsp)
movabs $0x6f77206f6c6c6568,%rax
20 77 6f
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
test   %eax,%eax
js     11de <main+0x7e>
mov    %rbx,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2e(%rip),%rdi
jmp    11e5 <main+0x85>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"./scanf\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"popen\00"
@__const.main.buf = [13 x i8] c"hello world!\00"
@.str.3 = [7 x i8] c"fwrite\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [13 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32* @popen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 -1)
unreachable
11: ; preds = %0
%12 = bitcast [13 x i8]* %3 to i8*
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%14 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = load i32*, i32** %2
%17 = call i32 @fwrite(i8* %13, i32 1, i32 %15, i32* %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %11
%24 = load i32*, i32** %2
%25 = call i32 @pclose(i32* %24)
ret i32 0
}

LLVM IR (-O2):
@.str = [8 x i8] c"./scanf\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"popen\00"
@__const.main.buf = [13 x i8] c"hello world!\00"
@.str.3 = [7 x i8] c"fwrite\00"
define i32 @main() {
%1 = alloca [13 x i8]
%2 = tail call i32* @popen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0
%8 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%9 = call i32 @strlen(i8* %8)
%10 = call i32 @fwrite(i8* %8, i32 1, i32 %9, i32* %2)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %7
%16 = call i32 @pclose(i32* %2)
ret i32 0
}