#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*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
void read_stdin(char **string) {
  char *c = malloc(100);
  if (!c) {
    fprintf(stderr, "Allocation failure for string from stdin\n");
    exit(1);
  }
  if (!fgets(c, 100, stdin)) {
    perror("fgets (stdin)");
    exit(1);
  }
  *string = c;
  c = strchr(*string, '\n');
  if (c)
    *c = 0;
}


x86 assembly (clang, -O0):
<read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x64,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a4 <read_stdin+0x44>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1040 <fgets@plt>
cmp    $0x0,%eax
jne    11da <read_stdin+0x7a>
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     120c <read_stdin+0xac>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_stdin>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x64,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b3 <read_stdin+0x53>
mov    %rax,%rbx
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1040 <fgets@plt>
test   %eax,%eax
je     11d2 <read_stdin+0x72>
mov    %rbx,(%r14)
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11ab <read_stdin+0x4b>
movb   $0x0,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"Allocation failure for string from stdin\0A\00"
@stdin = global i32 0
@.str.1 = [14 x i8] c"fgets (stdin)\00"
define void @read_stdin(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
store i8** %0, i8*** %2
%4 = call i8* @malloc(i32 100)
store i8* %4, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* @stdin
%14 = call i32 @fgets(i8* %12, i32 100, i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = load i8**, i8*** %2
store i8* %20, i8** %21
%22 = load i8**, i8*** %2
%23 = load i8*, i8** %22
%24 = call i8* @strchr(i8* %23, i8 signext 10)
store i8* %24, i8** %3
%25 = load i8*, i8** %3
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = load i8*, i8** %3
store i8 0, i8* %28
br label %29
29: ; preds = %27, %19
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"Allocation failure for string from stdin\0A\00"
@stdin = global i32 0
@.str.1 = [14 x i8] c"fgets (stdin)\00"
define void @read_stdin(i8** nocapture writeonly %0) {
%2 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %8
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %1
%9 = load i32, i32* @stdin
%10 = tail call i32 @fgets(i8* %2, i32 100, i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %8
store i8* %2, i8** %0
%16 = tail call i8* @strchr(i8* %2, i8 signext 10)
%17 = icmp eq i8* %16, null
br i1 %17, label %19, label %18
18: ; preds = %15
store i8 0, i8* %16
br label %19
19: ; preds = %18, %15
ret void
}