#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/);
int /*<<< orphan*/ fdopen(int /*<<< orphan*/, char *);
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int main() {
  int oldStdin = dup(0);
  freopen(
      "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
      "2022-01-22/02-19-57/repos/smarr/Truffle/refs/heads/debugger/"
      "truffle-update/sulong/tests/com.oracle.truffle.llvm.tests.libc.native/"
      "libc/freopen/freopen-redirect-stdin.c",
      "r", stdin);
  char buf[200];
  while (fgets(buf, 200, stdin) == buf) {
    printf("%s\n", buf);
  }
  fclose(stdin);
  dup2(oldStdin, 0);
  close(oldStdin);
  stdin = fdopen(0, "r");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1060 <dup@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
lea    0xe54(%rip),%rdi
lea    0xf35(%rip),%rsi
call   1070 <freopen@plt>
lea    -0xd0(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
mov    $0xc8,%esi
call   1090 <fgets@plt>
lea    -0xd0(%rbp),%rcx
cmp    %rcx,%rax
jne    11fa <main+0x7a>
lea    -0xd0(%rbp),%rsi
lea    0xefa(%rip),%rdi
call   1050 <printf@plt>
jmp    11b8 <main+0x38>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
lea    0xec5(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %eax,%ecx
mov    0x2db7(%rip),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xc8,%rsp
xor    %edi,%edi
call   1060 <dup@plt>
mov    %eax,%r14d
mov    0x2e4a(%rip),%r15
mov    (%r15),%edx
lea    0xe58(%rip),%rdi
lea    0xf39(%rip),%rsi
call   1070 <freopen@plt>
mov    (%r15),%edx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc8,%esi
call   1090 <fgets@plt>
cmp    %rbx,%rax
jne    1200 <main+0x80>
lea    0xf17(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0xc8,%esi
call   1090 <fgets@plt>
cmp    %rbx,%rax
je     11e0 <main+0x60>
mov    (%r15),%edi
call   1030 <fclose@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %r14d,%edi
call   1080 <close@plt>
lea    0xec7(%rip),%rsi
xor    %edi,%edi
call   10a0 <fdopen@plt>
mov    %eax,(%r15)
xor    %eax,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [232 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/smarr/Truffle/refs/heads/debugger/truffle-update/sulong/tests/com.oracle.truffle.llvm.tests.libc.native/libc/freopen/freopen-redirect-stdin.c\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [200 x i8]
store i32 0, i32* %1
%4 = call i32 @dup(i32 0)
store i32 %4, i32* %2
%5 = load i32, i32* @stdin
%6 = call i32 @freopen(i8* getelementptr inbounds ([232 x i8], [232 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %7
7: ; preds = %13, %0
%8 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i8* @fgets(i8* %8, i32 200, i32 %9)
%11 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %7
16: ; preds = %7
%17 = load i32, i32* @stdin
%18 = call i32 @fclose(i32 %17)
%19 = load i32, i32* %2
%20 = call i32 @dup2(i32 %19, i32 0)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = call i32 @fdopen(i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %23, i32* @stdin
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
@.str = [232 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/smarr/Truffle/refs/heads/debugger/truffle-update/sulong/tests/com.oracle.truffle.llvm.tests.libc.native/libc/freopen/freopen-redirect-stdin.c\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [200 x i8]
%2 = tail call i32 @dup(i32 0)
%3 = load i32, i32* @stdin
%4 = tail call i32 @freopen(i8* getelementptr inbounds ([232 x i8], [232 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %1, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i8* @fgets(i8* %5, i32 200, i32 %6)
%8 = icmp eq i8* %7, %5
br i1 %8, label %9, label %14
9: ; preds = %0, %9
%10 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%11 = load i32, i32* @stdin
%12 = call i8* @fgets(i8* %5, i32 200, i32 %11)
%13 = icmp eq i8* %12, %5
br i1 %13, label %9, label %14
14: ; preds = %9, %0
%15 = load i32, i32* @stdin
%16 = call i32 @fclose(i32 %15)
%17 = call i32 @dup2(i32 %2, i32 0)
%18 = call i32 @close(i32 %2)
%19 = call i32 @fdopen(i32 0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %19, i32* @stdin
ret i32 0
}