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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ system(char *);
int main() {
  FILE *pp;
  char gpo[] = "./raspi_gpo 4";
  char cmd[sizeof(gpo) + 3];
  int in = 0;
  char s[5];
  printf("Practice 07\n");
  while (1) {
    fputs("in > ", stdout);
    fgets(s, 5, stdin);
    in = atoi(s);
    printf("in=%d ", in);
    if (in < 0 || in > 1) {
      break;
    }
    sprintf(cmd, "%s %d", gpo, in);
    pp = popen(cmd, "r");
    if (pp == NULL) {
      printf("ERROR\n");
      return -1;
    }
    fgets(s, 5, pp);
    in = atoi(s);
    printf("ret=%d\n", in);
    pclose(pp);
  }
  sprintf(cmd, "%s -1", gpo);
  system(cmd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x1e(%rbp)
mov    0xe68(%rip),%eax
mov    %eax,-0x16(%rbp)
mov    0xe62(%rip),%ax
mov    %ax,-0x12(%rbp)
movl   $0x0,-0x34(%rbp)
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   1060 <fputs@plt>
lea    -0x39(%rbp),%rdi
mov    0x2e05(%rip),%rax
mov    (%rax),%rdx
mov    $0x5,%esi
call   1070 <fgets@plt>
lea    -0x39(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jl     1221 <main+0xa1>
cmpl   $0x1,-0x34(%rbp)
jle    1226 <main+0xa6>
jmp    12b5 <main+0x135>
lea    -0x30(%rbp),%rdi
lea    -0x1e(%rbp),%rdx
mov    -0x34(%rbp),%ecx
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x30(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1278 <main+0xf8>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <main+0x15b>
lea    -0x39(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x5,%esi
call   1070 <fgets@plt>
lea    -0x39(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <pclose@plt>
jmp    11c3 <main+0x43>
lea    -0x30(%rbp),%rdi
lea    -0x1e(%rbp),%rdx
lea    0xd7b(%rip),%rsi
mov    $0x0,%al
call   10a0 <sprintf@plt>
lea    -0x30(%rbp),%rdi
call   1030 <system@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movabs $0x34206f70675f69,%rax
mov    %rax,0x16(%rsp)
movabs $0x5f69707361722f2e,%rax
70 69 5f
mov    %rax,0x10(%rsp)
lea    0xe6e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
lea    0xe36(%rip),%rdi
call   1070 <fputs@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%rdx
lea    0xb(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
lea    0xe09(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%r14d
ja     12ed <main+0x15d>
lea    0x20(%rsp),%r13
lea    0xb(%rsp),%rbx
lea    0xddf(%rip),%r15
lea    0xdde(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
lea    0xdd1(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %r13,%rdi
lea    0xdbe(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1314 <main+0x184>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x5,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
lea    0xd90(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1060 <pclose@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <fputs@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%rdx
mov    %rbx,%rdi
mov    $0x5,%esi
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x2,%r14d
jb     1240 <main+0xb0>
lea    0xd37(%rip),%rsi
lea    0x20(%rsp),%rbx
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <system@plt>
xor    %eax,%eax
jmp    1325 <main+0x195>
lea    0xd22(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.gpo = [14 x i8] c"./raspi_gpo 4\00"
@.str = [13 x i8] c"Practice 07\0A\00"
@.str.1 = [6 x i8] c"in > \00"
@stdout = global i32 0
@stdin = global i32* null
@.str.2 = [7 x i8] c"in=%d \00"
@.str.3 = [6 x i8] c"%s %d\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [7 x i8] c"ERROR\0A\00"
@.str.6 = [8 x i8] c"ret=%d\0A\00"
@.str.7 = [6 x i8] c"%s -1\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [14 x i8]
%4 = alloca [17 x i8]
%5 = alloca i32
%6 = alloca [5 x i8]
store i32 0, i32* %1
%7 = bitcast [14 x i8]* %3 to i8*
store i32 0, i32* %5
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %0, %36
%10 = load i32, i32* @stdout
%11 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%13 = load i32*, i32** @stdin
%14 = call i32 @fgets(i8* %12, i32 5, i32* %13)
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %9
%22 = load i32, i32* %5
%23 = icmp sgt i32 %22, 1
br i1 %23, label %24, label %25
24: ; preds = %21, %9
br label %46
25: ; preds = %21
%26 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%27 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* %5
%29 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %26, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %27, i32 %28)
%30 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%31 = call i32* @popen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %31, i32** %2
%32 = load i32*, i32** %2
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %25
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %1
br label %52
36: ; preds = %25
%37 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%38 = load i32*, i32** %2
%39 = call i32 @fgets(i8* %37, i32 5, i32* %38)
%40 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%41 = call i32 @atoi(i8* %40)
store i32 %41, i32* %5
%42 = load i32, i32* %5
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = load i32*, i32** %2
%45 = call i32 @pclose(i32* %44)
br label %9
46: ; preds = %24
%47 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%48 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%49 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %48)
%50 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%51 = call i32 @system(i8* %50)
store i32 0, i32* %1
br label %52
52: ; preds = %46, %34
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@__const.main.gpo = [14 x i8] c"./raspi_gpo 4\00"
@.str.1 = [6 x i8] c"in > \00"
@stdout = global i32 0
@stdin = global i32* null
@.str.2 = [7 x i8] c"in=%d \00"
@.str.3 = [6 x i8] c"%s %d\00"
@.str.4 = [2 x i8] c"r\00"
@.str.6 = [8 x i8] c"ret=%d\0A\00"
@.str.7 = [6 x i8] c"%s -1\00"
@str = [12 x i8] c"Practice 07\00"
@str.8 = [6 x i8] c"ERROR\00"
define i32 @main() {
%1 = alloca [14 x i8]
%2 = alloca [17 x i8]
%3 = alloca [5 x i8]
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%8 = load i32, i32* @stdout
%9 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = load i32*, i32** @stdin
%11 = call i32 @fgets(i8* %6, i32 5, i32* %10)
%12 = call i32 @atoi(i8* %6)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = icmp ugt i32 %12, 1
br i1 %14, label %34, label %15
15: ; preds = %0, %22
%16 = phi i32 [ %31, %22 ], [ %12, %0 ]
%17 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8* %4, i32 %16)
%18 = call i32* @popen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
br label %37
22: ; preds = %15
%23 = call i32 @fgets(i8* %6, i32 5, i32* %18)
%24 = call i32 @atoi(i8* %6)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %24)
%26 = call i32 @pclose(i32* %18)
%27 = load i32, i32* @stdout
%28 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32*, i32** @stdin
%30 = call i32 @fgets(i8* %6, i32 5, i32* %29)
%31 = call i32 @atoi(i8* %6)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = icmp ugt i32 %31, 1
br i1 %33, label %34, label %15
34: ; preds = %22, %0
%35 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%36 = call i32 @system(i8* %5)
br label %37
37: ; preds = %34, %20
%38 = phi i32 [ 0, %34 ], [ -1, %20 ]
ret i32 %38
}