#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error_exit(char *, ...);
int fstat(int, struct stat *);
int open_file(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *strchr(char *, char);
char *strdup(char *);
char *read_password(char *file) {
  char buffer[128], *pw, *lf;
  struct stat buf;
  int fd = open_file(file, O_RDONLY), rc;
  if (fstat(fd, &buf) == -1)
    error_exit("fstat(%s) failed", file);
  rc = read(fd, buffer, sizeof(buffer) - 1);
  if (rc == -1)
    error_exit("error reading password");
  buffer[rc] = 0x00;
  lf = strchr(buffer, '\n');
  if (lf)
    *lf = 0x00;
  close(fd);
  pw = strdup(buffer);
  if (!pw)
    error_exit("strdup() failed");
  return pw;
}


x86 assembly (clang, -O0):
<read_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
call   1030 <open_file@plt>
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%edi
lea    -0xa8(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11c4 <read_password+0x54>
mov    -0x8(%rbp),%rsi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1060 <error_exit@plt>
mov    -0xac(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x7f,%edx
call   1070 <read@plt>
mov    %eax,-0xb0(%rbp)
cmpl   $0xffffffff,-0xb0(%rbp)
jne    11fc <read_password+0x8c>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1060 <error_exit@plt>
movslq -0xb0(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0xa0(%rbp)
cmpq   $0x0,-0xa0(%rbp)
je     123b <read_password+0xcb>
mov    -0xa0(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xac(%rbp),%edi
call   1050 <close@plt>
lea    -0x90(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    1275 <read_password+0x105>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1060 <error_exit@plt>
mov    -0x98(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_password>:
push   %rbp
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1030 <open_file@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11ae <read_password+0x3e>
lea    0xe5c(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <error_exit@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x7f,%edx
call   1070 <read@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11d4 <read_password+0x64>
lea    0xe44(%rip),%rdi
xor    %eax,%eax
call   1060 <error_exit@plt>
movslq %ebx,%rax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11f3 <read_password+0x83>
movb   $0x0,(%rax)
mov    %ebp,%edi
call   1050 <close@plt>
lea    0x10(%rsp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    121a <read_password+0xaa>
lea    0xe15(%rip),%rdi
xor    %eax,%eax
call   1060 <error_exit@plt>
mov    %rbx,%rax
add    $0x98,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [17 x i8] c"fstat(%s) failed\00"
@.str.1 = [23 x i8] c"error reading password\00"
@.str.2 = [16 x i8] c"strdup() failed\00"
define i8* @read_password(i8* %0) {
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open_file(i8* %9, i32 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = call i32 @fstat(i32 %12, %struct.stat* %6)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %1
%19 = load i32, i32* %7
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 127)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %28
store i8 0, i8* %29
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%31 = call i8* @strchr(i8* %30, i8 signext 10)
store i8* %31, i8** %5
%32 = load i8*, i8** %5
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %26
%35 = load i8*, i8** %5
store i8 0, i8* %35
br label %36
36: ; preds = %34, %26
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%40 = call i8* @strdup(i8* %39)
store i8* %40, i8** %4
%41 = load i8*, i8** %4
%42 = icmp ne i8* %41, null
br i1 %42, label %45, label %43
43: ; preds = %36
%44 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %36
%46 = load i8*, i8** %4
ret i8* %46
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [17 x i8] c"fstat(%s) failed\00"
@.str.1 = [23 x i8] c"error reading password\00"
@.str.2 = [16 x i8] c"strdup() failed\00"
define noalias i8* @read_password(i8* %0) {
%2 = alloca [128 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open_file(i8* %0, i32 %6)
%8 = call i32 @fstat(i32 %7, %struct.stat* %3)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
12: ; preds = %10, %1
%13 = call i32 @read(i32 %7, i8* %4, i32 127)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = sext i32 %13 to i64
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i8* @strchr(i8* %4, i8 signext 10)
%21 = icmp eq i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %17
store i8 0, i8* %20
br label %23
23: ; preds = %22, %17
%24 = call i32 @close(i32 %7)
%25 = call i8* @strdup(i8* %4)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @error_exit(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
ret i8* %25
}