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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int open(char *, int, int);
int pipe(int *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *args[]) {
  int pipeDes[2];
  char *argv[3];
  pid_t p;
  if (pipe(pipeDes) == -1) {
    return 1;
  }
  p = fork();
  if (p == -1) {
    return 1;
  }
  if (p == 0) {
    int des = open(args[2], O_WRONLY | O_CREAT | O_TRUNC, 0600);
    close(pipeDes[1]);
    argv[0] = "psql";
    argv[1] = "base";
    argv[2] = NULL;
    dup2(des, 1);
    close(des);
    dup2(pipeDes[0], 0);
    close(pipeDes[0]);
    if (execvp("psql", argv) == -1) {
      exit(255);
    }
  }
  int statut;
  close(pipeDes[0]);
  write(pipeDes[1], "\\i ", strlen("\\i "));
  write(pipeDes[1], args[1], strlen(args[1]));
  write(pipeDes[1], "\\q", strlen("\\q"));
  close(pipeDes[1]);
  wait(&statut);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d4 <main+0x34>
movl   $0x1,-0x4(%rbp)
jmp    132f <main+0x18f>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11f4 <main+0x54>
movl   $0x1,-0x4(%rbp)
jmp    132f <main+0x18f>
cmpl   $0x0,-0x34(%rbp)
jne    12a3 <main+0x103>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
or     (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xdc3(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xdbd(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x38(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
lea    -0x30(%rbp),%rsi
lea    0xd7a(%rip),%rdi
call   1090 <execvp@plt>
cmp    $0xffffffff,%eax
jne    129e <main+0xfe>
mov    $0xff,%edi
call   10a0 <exit@plt>
jmp    12a3 <main+0x103>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x50(%rbp)
lea    0xd52(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%edi
mov    %eax,%edx
lea    0xd41(%rip),%rsi
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
lea    0xd06(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%edi
mov    %eax,%edx
lea    0xcf5(%rip),%rsi
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    -0x3c(%rbp),%rdi
call   10b0 <wait@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
lea    0x4(%rsp),%rdi
call   1070 <pipe@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     12e7 <main+0x147>
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     12e7 <main+0x147>
test   %eax,%eax
jne    126f <main+0xcf>
mov    0x2de4(%rip),%rax
mov    0x2e0d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%r15),%rdi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xde8(%rip),%r14
mov    %r14,0x10(%rsp)
lea    0xde1(%rip),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1090 <execvp@plt>
cmp    $0xffffffff,%eax
je     12f4 <main+0x154>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%ebp
lea    0xd87(%rip),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%ebx
mov    0x8(%r15),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%ebx
lea    0xd50(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xc(%rsp),%rdi
call   10b0 <wait@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [5 x i8] c"psql\00"
@.str.1 = [5 x i8] c"base\00"
@.str.2 = [4 x i8] c"\\i \00"
@.str.3 = [3 x i8] c"\\q\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca [3 x i8*]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 1, i32* %3
br label %81
15: ; preds = %2
%16 = call i32 (...) @fork()
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 1, i32* %3
br label %81
20: ; preds = %15
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %55
23: ; preds = %20
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* @O_CREAT
%29 = or i32 %27, %28
%30 = load i32, i32* @O_TRUNC
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %26, i32 %31, i32 384)
store i32 %32, i32* %9
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %36
%37 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %37
%38 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 2
store i8* null, i8** %38
%39 = load i32, i32* %9
%40 = call i32 @dup2(i32 %39, i32 1)
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%44 = load i32, i32* %43
%45 = call i32 @dup2(i32 %44, i32 0)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [3 x i8*], [3 x i8*]* %7, i64 0, i64 0
%50 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %49)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %54
52: ; preds = %23
%53 = call i32 @exit(i32 255)
unreachable
54: ; preds = %23
br label %55
55: ; preds = %54, %20
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%60 = load i32, i32* %59
%61 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%62 = call i32 @write(i32 %60, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %61)
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%64 = load i32, i32* %63
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = call i32 @strlen(i8* %70)
%72 = call i32 @write(i32 %64, i8* %67, i32 %71)
%73 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%74 = load i32, i32* %73
%75 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%76 = call i32 @write(i32 %74, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %75)
%77 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%78 = load i32, i32* %77
%79 = call i32 @close(i32 %78)
%80 = call i32 @wait(i32* %10)
br label %81
81: ; preds = %55, %19, %14
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str = [5 x i8] c"psql\00"
@.str.1 = [5 x i8] c"base\00"
@.str.2 = [4 x i8] c"\\i \00"
@.str.3 = [3 x i8] c"\\q\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca [3 x i8*]
%5 = alloca i32
%6 = bitcast [2 x i32]* %3 to i8*
%7 = bitcast [3 x i8*]* %4 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %57, label %11
11: ; preds = %2
%12 = call i32 (...) @fork()
switch i32 %12, label %38 [
i32 -1, label %57
i32 0, label %13
]
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_CREAT
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %15, i32 %20, i32 384)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %25
%26 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8** %26
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 2
store i8* null, i8** %27
%28 = call i32 @dup2(i32 %21, i32 1)
%29 = call i32 @close(i32 %21)
%30 = load i32, i32* %8
%31 = call i32 @dup2(i32 %30, i32 0)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
%34 = call i32 @execvp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %25)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %13
%37 = call i32 @exit(i32 255)
unreachable
38: ; preds = %13, %11
%39 = bitcast i32* %5 to i8*
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @write(i32 %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %42
%47 = getelementptr inbounds i8*, i8** %1, i64 1
%48 = load i8*, i8** %47
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @write(i32 %46, i8* %48, i32 %49)
%51 = load i32, i32* %42
%52 = call i32 @strlen(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @write(i32 %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %42
%55 = call i32 @close(i32 %54)
%56 = call i32 @wait(i32* %5)
br label %57
57: ; preds = %11, %2, %38
%58 = phi i32 [ 0, %38 ], [ 1, %2 ], [ 1, %11 ]
ret i32 %58
}