#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*/ fgets(char *, int, int /*<<< orphan*/ *);
char *strdup(char *);
int strlen(char *);
char *read_string(FILE *fp) {
  char buf[256];
  fgets(buf, 256, fp);
  if (strlen(buf) < 256) {
    if (strlen(buf))
      buf[strlen(buf) - 1] = 0;
    return strdup(buf);
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1040 <fgets@plt>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x100,%eax
jge    11ae <read_string+0x7e>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
je     1199 <read_string+0x69>
lea    -0x110(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11b6 <read_string+0x86>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0xff,%eax
jg     1182 <read_string+0x52>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     1178 <read_string+0x48>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rsp,%rdi
call   1050 <strdup@plt>
jmp    1184 <read_string+0x54>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i8* @read_string(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [256 x i8]
store i32* %0, i32** %3
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = load i32*, i32** %3
%7 = call i32 @fgets(i8* %5, i32 256, i32* %6)
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%9 = call i32 @strlen(i8* %8)
%10 = icmp slt i32 %9, 256
br i1 %10, label %11, label %24
11: ; preds = %1
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %11
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %15, %11
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%23 = call i8* @strdup(i8* %22)
store i8* %23, i8** %2
br label %25
24: ; preds = %1
store i8* null, i8** %2
br label %25
25: ; preds = %24, %21
%26 = load i8*, i8** %2
ret i8* %26
}

LLVM IR (-O2):
define noalias i8* @read_string(i32* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @fgets(i8* %3, i32 256, i32* %0)
%5 = call i32 @strlen(i8* %3)
%6 = icmp slt i32 %5, 256
br i1 %6, label %7, label %17
7: ; preds = %1
%8 = call i32 @strlen(i8* %3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %7
%11 = call i32 @strlen(i8* %3)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %10, %7
%16 = call i8* @strdup(i8* %3)
br label %17
17: ; preds = %1, %15
%18 = phi i8* [ %16, %15 ], [ null, %1 ]
ret i8* %18
}