#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 unsigned char uint32_t;

/* Variables and functions */
int read(int, unsigned char *, int);
int read_uint32(int fd, uint32_t *x) {
  unsigned char tmp[4];
  int n;
  n = read(fd, tmp, 4);
  if (n != 4)
    return -1;
  *x = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24);
  return 0;
}


x86 assembly (clang, -O0):
<read_uint32>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
je     1149 <read_uint32+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1177 <read_uint32+0x67>
movzbl -0x14(%rbp),%eax
movzbl -0x13(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movzbl -0x12(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movzbl -0x11(%rbp),%ecx
shl    $0x18,%ecx
or     %ecx,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_uint32>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x4,%ecx
jne    113b <read_uint32+0x2b>
mov    0xc(%rsp),%al
mov    %al,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @read_uint32(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i32, i32* %4
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 4)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %35
14: ; preds = %2
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = shl i32 %20, 8
%22 = or i32 %17, %21
%23 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = shl i32 %25, 16
%27 = or i32 %22, %26
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 3
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = shl i32 %30, 24
%32 = or i32 %27, %31
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %5
store i8 %33, i8* %34
store i32 0, i32* %3
br label %35
35: ; preds = %14, %13
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @read_uint32(i32 %0, i8* nocapture writeonly %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 4)
%6 = icmp eq i32 %5, 4
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i8, i8* %4
store i8 %8, i8* %1
br label %9
9: ; preds = %2, %7
%10 = phi i32 [ 0, %7 ], [ -1, %2 ]
ret i32 %10
}